Class: Roqua::Scheduling::Schedule

Inherits:
Object
  • Object
show all
Defined in:
lib/roqua/scheduling/schedule.rb

Constant Summary collapse

BEGINNING_OF_EVERY_DAY =
proc { Time.now.beginning_of_day + 1.day }
BEGINNING_OF_EVERY_HOUR =
proc { Time.now.beginning_of_hour + 1.hour }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSchedule

Returns a new instance of Schedule.



7
8
9
# File 'lib/roqua/scheduling/schedule.rb', line 7

def initialize
  @tasks = {}
end

Instance Attribute Details

#tasksObject (readonly)

Returns the value of attribute tasks.



2
3
4
# File 'lib/roqua/scheduling/schedule.rb', line 2

def tasks
  @tasks
end

Class Method Details

.current_scheduleObject



22
23
24
# File 'lib/roqua/scheduling/schedule.rb', line 22

def self.current_schedule
  @schedule ||= Roqua::Scheduling::Schedule.new
end

.setupObject



15
16
17
18
19
20
# File 'lib/roqua/scheduling/schedule.rb', line 15

def self.setup
  @schedule = Roqua::Scheduling::Schedule.new.tap do |new_schedule|
    yield(new_schedule)
    new_schedule.initialize_cronjob_table
  end
end

Instance Method Details

#add_task(name, options, &block) ⇒ Object



11
12
13
# File 'lib/roqua/scheduling/schedule.rb', line 11

def add_task(name, options, &block)
  @tasks[name] = Roqua::Scheduling::Task.new(name, options, block)
end

#initialize_cronjob_tableObject



26
27
28
29
30
31
32
33
# File 'lib/roqua/scheduling/schedule.rb', line 26

def initialize_cronjob_table
  Roqua::Scheduling::CronJob.where('name NOT IN (?)', tasks.values.map(&:name)).delete_all

  tasks.each_value do |task|
    cron_job = Roqua::Scheduling::CronJob.find_or_initialize_by(name: task.name)
    cron_job.update next_run_at: task.next_run_at unless cron_job.persisted?
  end
end