Class: Skiplock::Cron

Inherits:
Object
  • Object
show all
Defined in:
lib/skiplock/cron.rb

Class Method Summary collapse

Class Method Details

.next_schedule_at(cron) ⇒ Object



24
25
26
27
28
29
# File 'lib/skiplock/cron.rb', line 24

def self.next_schedule_at(cron)
  time = CronParser.new(cron).next
  time = time + (time <= Time.now ? 60 : Time.now.sec)
  time.to_f  
rescue
end

.setupObject



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