Sha256: 07e763e194b6b78f2b48740529dd5502fb10be6efbaf5a53c59d0b9b27640838

Contents?: true

Size: 922 Bytes

Versions: 4

Compression:

Stored size: 922 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

4 entries across 4 versions & 1 rubygems

Version Path
rocketjob-5.1.1 lib/rocket_job/plugins/singleton.rb
rocketjob-5.1.0 lib/rocket_job/plugins/singleton.rb
rocketjob-5.0.0 lib/rocket_job/plugins/singleton.rb
rocketjob-5.0.0.rc1 lib/rocket_job/plugins/singleton.rb