Sha256: 6edc4da8fbe5887777d5ffe3a3a3d961457b6e8249da2be3e8b7ba5a5a07a76e

Contents?: true

Size: 1.98 KB

Versions: 2

Compression:

Stored size: 1.98 KB

Contents

#
# PerfectQueue
#
# Copyright (C) 2012 FURUHASHI Sadayuki
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
#

module PerfectQueue
  module Backend
    class RDBCompatBackend

      # This module is mostly copied from rdb_backend.rb
      # and should be maintained with it.

      module ActiveRecordMixin
        def self.included(mod)
          mod.extend(ClassMethods)
        end

        class ClassMethods
          def submit!(key, type, data, options)
            now = (options[:now] || Time.now).to_i
            run_at = (options[:run_at] || now).to_i
            user = options[:user]
            max_running = options[:max_running]
            data = data ? data.dup : {}
            data['type'] = type

            create!({
              :id => key,
              :timeout => run_at,
              :data => data.to_json,
              :created_at => now,
              :resource => user,
              :max_running => max_running,
            })

          rescue ActiveRecord::RecordNotUnique
            raise IdempotentAlreadyExistsError, "task key=#{key} already exists"
          end

          def cancel_request!(key, options)
            now = (options[:now] || Time.now).to_i

            o = find_by_id(key)
            if o == nil || o.created_at == nil
              raise AlreadyFinishedError, "task key=#{key} does not exist or already finished."
            end

            o.created_at = -1
            o.save!
          end
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
perfectqueue-0.7.24 lib/perfectqueue/backend/rdb_compat/active_record_mixin.rb
perfectqueue-0.7.23 lib/perfectqueue/backend/rdb_compat/active_record_mixin.rb