module ScheduledJob::ScheduledJobClassMethods

Public Instance Methods

can_schedule_job?(job = nil) click to toggle source
# File lib/scheduled_job.rb, line 99
def can_schedule_job?(job = nil)
  conditions = 'failed_at IS NULL'
  conditions << " AND id != #{job.id}" unless job.blank?

  jobs = Delayed::Job.where(conditions).find_all do |dj|
    dj.handler.split[1][/(?<=:)[A-Z][A-z0-9:]+/] == self.name
  end

  job_count = jobs.count

  intended_job_count = 1

  if ScheduledJob.config.jobs && ScheduledJob.config.jobs[self.name]
    intended_job_count = ScheduledJob.config.jobs[self.name][:count]
  end

  job_count < intended_job_count
end
queue_name() click to toggle source
# File lib/scheduled_job.rb, line 122
def queue_name
  Delayed::Worker.default_queue_name
end
random_minutes(base, random_delta) click to toggle source
# File lib/scheduled_job.rb, line 94
def random_minutes(base, random_delta)
  random_delta *= -1 if random_delta < 0
  (base + Random.new.rand((-1 * random_delta)..random_delta)).minutes
end
run_duration_threshold() click to toggle source
# File lib/scheduled_job.rb, line 118
def run_duration_threshold
  self.const_defined?(:RUN_DURATION_THRESHOLD) ? self::RUN_DURATION_THRESHOLD : nil
end
schedule_job(job = nil) click to toggle source

This method should be called when scheduling a recurring job as it checks to ensure no other instances of the job are already running.

# File lib/scheduled_job.rb, line 83
def schedule_job(job = nil)
  if can_schedule_job?(job)
    callback = ScheduledJob.config.fast_mode
    in_fast_mode = callback ? callback.call(self) : false

    run_at = in_fast_mode ? Time.now.utc + 1 : time_to_recur(Time.now.utc)

    Delayed::Job.enqueue(new, :run_at => run_at, :queue => queue_name)
  end
end