Sha256: 8324bfa6b9f5aa815aec5dad9342fbc63fcb64da753c1076c37ac65d6d8f14bf

Contents?: true

Size: 921 Bytes

Versions: 24

Compression:

Stored size: 921 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.with(read: {mode: :primary}) do |conn|
          conn.where(:state.in => %i[running queued], :id.ne => id).exists?
        end
      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

24 entries across 24 versions & 1 rubygems

Version Path
rocketjob-6.3.1 lib/rocket_job/plugins/singleton.rb
rocketjob-6.3.0 lib/rocket_job/plugins/singleton.rb
rocketjob-6.2.0 lib/rocket_job/plugins/singleton.rb
rocketjob-6.1.1 lib/rocket_job/plugins/singleton.rb
rocketjob-6.1.0 lib/rocket_job/plugins/singleton.rb
rocketjob-6.0.3 lib/rocket_job/plugins/singleton.rb
rocketjob-6.0.2 lib/rocket_job/plugins/singleton.rb
rocketjob-6.0.1 lib/rocket_job/plugins/singleton.rb
rocketjob-6.0.0 lib/rocket_job/plugins/singleton.rb
rocketjob-6.0.0.rc3 lib/rocket_job/plugins/singleton.rb
rocketjob-6.0.0.rc2 lib/rocket_job/plugins/singleton.rb
rocketjob-6.0.0.rc1 lib/rocket_job/plugins/singleton.rb
rocketjob-5.4.1 lib/rocket_job/plugins/singleton.rb
rocketjob-5.4.0 lib/rocket_job/plugins/singleton.rb
rocketjob-5.4.0.beta2 lib/rocket_job/plugins/singleton.rb
rocketjob-5.4.0.beta1 lib/rocket_job/plugins/singleton.rb
rocketjob-5.3.3 lib/rocket_job/plugins/singleton.rb
rocketjob-5.3.2 lib/rocket_job/plugins/singleton.rb
rocketjob-5.3.1 lib/rocket_job/plugins/singleton.rb
rocketjob-5.3.0 lib/rocket_job/plugins/singleton.rb