Module: ScheduledJob::ScheduledJobClassMethods

Defined in:
lib/scheduled_job.rb

Instance Method Summary collapse

Instance Method Details

#can_schedule_job?(job = nil) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# 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_nameObject



122
123
124
# File 'lib/scheduled_job.rb', line 122

def queue_name
  Delayed::Worker.default_queue_name
end

#random_minutes(base, random_delta) ⇒ Object



94
95
96
97
# 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_thresholdObject



118
119
120
# 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) ⇒ Object

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



83
84
85
86
87
88
89
90
91
92
# 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