module RocketJob::Plugins::Singleton

Prevent this job from being saved if another is running, queued, or paused.

Public Instance Methods

rocket_job_singleton_active?() click to toggle source

Returns [true|false] whether another instance of this job is already active

# File lib/rocket_job/plugins/singleton.rb, line 14
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 Instance Methods

rocket_job_singleton_check() click to toggle source

Validation prevents a new job from being saved while one is already running

# File lib/rocket_job/plugins/singleton.rb, line 23
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