Sha256: 01bc4a223cacf30b503fadbbe5677c67681aca2f9862d8bc14057b82df4d340a

Contents?: true

Size: 874 Bytes

Versions: 7

Compression:

Stored size: 874 Bytes

Contents

# encoding: UTF-8
require 'active_support/concern'

module RocketJob
  module Plugins
    # Prevent more than one instance of this job class from running at a time
    module Singleton
      extend ActiveSupport::Concern

      included do
        # Validation prevents a new job from being saved while one is already running
        validates_each :state do |record, attr, value|
          if (record.running? || record.queued? || record.paused?) && record.rocket_job_singleton_active?
            record.errors.add(attr, "Another instance of #{record.class.name} is already queued or running")
          end
        end

        # Returns [true|false] whether another instance of this job is already active
        def rocket_job_singleton_active?
          self.class.where(state: [:running, :queued], _id: {'$ne' => id}).exists?
        end
      end

    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rocketjob-2.1.3 lib/rocket_job/plugins/singleton.rb
rocketjob-2.1.2 lib/rocket_job/plugins/singleton.rb
rocketjob-2.1.1 lib/rocket_job/plugins/singleton.rb
rocketjob-2.0.0 lib/rocket_job/plugins/singleton.rb
rocketjob-2.0.0.rc3 lib/rocket_job/plugins/singleton.rb
rocketjob-2.0.0.rc2 lib/rocket_job/plugins/singleton.rb
rocketjob-2.0.0.rc1 lib/rocket_job/plugins/singleton.rb