Sha256: 99096f524bc2f949ab88b368a4be002163c7ed9c0e8891745912c3efe71e4981

Contents?: true

Size: 856 Bytes

Versions: 11

Compression:

Stored size: 856 Bytes

Contents

require 'active_support/concern'

module RocketJob
  module Plugins
    # Prevent this job from being saved if another is running, queued, or paused.
    module Singleton
      extend ActiveSupport::Concern

      included do
        validate :rocket_job_singleton_check
      end

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

      private

      # Validation prevents a new job from being saved while one is already running
      def rocket_job_singleton_check
        return unless (running? || queued? || paused?) && rocket_job_singleton_active?

        errors.add(:state, "Another instance of #{self.class.name} is already running, queued, or paused")
      end

    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
rocketjob-5.0.0.beta4 lib/rocket_job/plugins/singleton.rb
rocketjob-5.0.0.beta3 lib/rocket_job/plugins/singleton.rb
rocketjob-5.0.0.beta2 lib/rocket_job/plugins/singleton.rb
rocketjob-5.0.0.beta lib/rocket_job/plugins/singleton.rb
rocketjob-4.3.0.beta2 lib/rocket_job/plugins/singleton.rb
rocketjob-4.3.0.beta lib/rocket_job/plugins/singleton.rb
rocketjob-4.2.0 lib/rocket_job/plugins/singleton.rb
rocketjob-4.1.1 lib/rocket_job/plugins/singleton.rb
rocketjob-4.1.0 lib/rocket_job/plugins/singleton.rb
rocketjob-4.0.0 lib/rocket_job/plugins/singleton.rb
rocketjob-3.5.2 lib/rocket_job/plugins/singleton.rb