class Clockwork::DatabaseEventSyncPerformer
Public Class Methods
new(manager, model, proc)
click to toggle source
# File lib/clockwork/manager_with_database_tasks.rb, line 22 def initialize(manager, model, proc) @manager = manager @model = model @block = proc @events = {} end
Public Instance Methods
add_event(e, task_id)
click to toggle source
store events by task_id in array (array is needed as there is 1 event per At)
# File lib/clockwork/manager_with_database_tasks.rb, line 50 def add_event(e, task_id) @events[task_id] ||= [] @events[task_id] << e end
clockwork_events()
click to toggle source
# File lib/clockwork/manager_with_database_tasks.rb, line 45 def clockwork_events @events.values.flatten end
sync()
click to toggle source
Ensure clockwork events reflect database tasks Adds any new tasks, modifies updated ones, and delets removed ones
# File lib/clockwork/manager_with_database_tasks.rb, line 31 def sync model_ids_that_exist = [] @model.all.each do |db_task| model_ids_that_exist << db_task.id if !event_exists_for_task(db_task) || task_has_changed(db_task) recreate_event_for_database_task(db_task) end end remove_deleted_database_tasks(model_ids_that_exist) end
Protected Instance Methods
array_of_ats_for(task, opts={})
click to toggle source
# File lib/clockwork/manager_with_database_tasks.rb, line 99 def array_of_ats_for(task, opts={}) if task.at.to_s.empty? opts[:nil_if_empty] ? nil : [] else task.at.split(',').map(&:strip) end end
array_of_ats_from_event(task_id)
click to toggle source
# File lib/clockwork/manager_with_database_tasks.rb, line 95 def array_of_ats_from_event(task_id) @events[task_id].collect{|clockwork_event| clockwork_event.instance_variable_get(:@at) }.compact end
event_exists_for_task(db_task)
click to toggle source
# File lib/clockwork/manager_with_database_tasks.rb, line 70 def event_exists_for_task(db_task) @events[db_task.id] end
recreate_event_for_database_task(db_task)
click to toggle source
# File lib/clockwork/manager_with_database_tasks.rb, line 57 def recreate_event_for_database_task(db_task) @events[db_task.id] = nil options = { :from_database => true, :db_task_id => db_task.id, :performer => self, :at => array_of_ats_for(db_task, :nil_if_empty => true) } @manager.every db_task.frequency, db_task.name, options, &@block end
remove_deleted_database_tasks(model_ids_that_exist)
click to toggle source
# File lib/clockwork/manager_with_database_tasks.rb, line 74 def remove_deleted_database_tasks(model_ids_that_exist) @events.reject!{|db_task_id, _| !model_ids_that_exist.include?(db_task_id) } end
task_has_changed(task)
click to toggle source
# File lib/clockwork/manager_with_database_tasks.rb, line 78 def task_has_changed(task) events = @events[task.id] event = @events[task.id].first # all events will have same frequency/name, just different ats ats_for_task = array_of_ats_for(task) ats_from_event = array_of_ats_from_event(task.id) name_has_changed = task.name != event.job frequency_has_changed = task.frequency != event.instance_variable_get(:@period) at_has_changed = ats_for_task.length != ats_from_event.length at_has_changed ||= ats_for_task.inject(false) do |memo, at| memo ||= !ats_from_event.include?(At.parse(at)) end name_has_changed || frequency_has_changed || at_has_changed end