4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/skiplock/cron.rb', line 4
def self.setup
cronjobs = []
Rails.application.eager_load!
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, queue_name: j.queue_as, cron: cron, locale: I18n.locale, timezone: Time.zone.name)
time = self.next_schedule_at(cron)
if time
job.cron = cron
job.running = false
job.scheduled_at = Time.at(time) unless job.try(:executions).to_i > 0 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
rescue
end
|