Class: Clockwork::ManagerWithDatabaseTasks

Inherits:
Manager
  • Object
show all
Defined in:
lib/clockwork/manager_with_database_tasks.rb

Constant Summary collapse

SECOND_TO_RUN_DATABASE_SYNC_AT =
0

Instance Attribute Summary

Attributes inherited from Manager

#config

Instance Method Summary collapse

Methods inherited from Manager

#configure, #default_configuration, #error_handler, #every, #fire_callbacks, #handle_error, #handler, #log, #log_error, #on, #run, #thread_available?, #tick

Constructor Details

#initializeManagerWithDatabaseTasks

Returns a new instance of ManagerWithDatabaseTasks.



15
16
17
18
19
# File 'lib/clockwork/manager_with_database_tasks.rb', line 15

def initialize
  super
  @events_from_database = []
  @next_sync_database_tasks_identifier = 0
end

Instance Method Details

#sync_database_tasks(options = {}, &block) ⇒ Object

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/clockwork/manager_with_database_tasks.rb', line 21

def sync_database_tasks(options={}, &block)
  [:model, :every].each do |option|
    raise ArgumentError.new("requires :#{option} option") unless options.include?(option)
  end
  raise ArgumentError.new(":every must be greater or equal to 1.minute") if options[:every] < 1.minute

  model = options[:model]
  frequency = options[:every]
  sync_task_id = get_sync_task_id

  # Prevent database tasks from running in same cycle as the database sync,
  # as this can lead to the same task being run twice
  options_to_run_database_sync_in_own_cycle  = { :if => lambda { |t| t.sec == SECOND_TO_RUN_DATABASE_SYNC_AT } }

  # create event that syncs clockwork events with database events
  every frequency, "sync_database_tasks_for_model_#{model}", options_to_run_database_sync_in_own_cycle do
    reload_events_from_database sync_task_id, model, &block
  end
end