4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/skiplock/cron.rb', line 4
def self.setup
cronjobs = []
ActiveJob::Base.descendants.each do |j|
next unless j.const_defined?('CRON')
cron = j.const_get('CRON')
job = Job.find_by('job_class = ? AND cron IS NOT NULL', j.name) || Job.new(job_class: j.name, cron: cron)
time = self.next_schedule_at(cron)
if time
job.cron = cron
job.running = false
job.scheduled_at = Time.at(time)
job.save!
cronjobs << j.name
end
end
query = Job.where('cron IS NOT NULL')
query = query.where('job_class NOT IN (?)', cronjobs) if cronjobs.count > 0
query.delete_all
end
|