Class: Serially::TaskRunner

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/serially/task_runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(task_run_observer = nil) ⇒ TaskRunner

Returns a new instance of TaskRunner.



7
8
9
# File 'lib/serially/task_runner.rb', line 7

def initialize(task_run_observer = nil)
  add_observer(task_run_observer) if task_run_observer
end

Instance Method Details

#run!(item_class, item_id = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/serially/task_runner.rb', line 11

def run!(item_class, item_id = nil)
  last_run = []
  instance = item_id.blank? ? item_class.send(:create_instance) : item_class.send(:create_instance, item_id)
  Serially::TaskManager[item_class].each do |task|
    # if task.async?
    #   started_async_task = SerialTasksManager.begin_task(task)
    #   # if started async task successfully, exit the loop, otherwise go to next task
    #   break if started_async_task
    # else
    #started_async_task = false

    success, msg, result_obj = task.run!(instance)
    error_handled = task.on_error!(instance, msg, result_obj) if !success
    last_run = [task, success, msg, result_obj]

    # write result log to DB
    changed
    notify_observers(task, instance, success, msg, result_obj, error_handled)
    # if task didn't complete successfully, and error handler didn't return true, exit
    break if !success && !error_handled
  end
  # if started_async_task
  #   msg = "SerialTasksManager: started async task for #{item_class}/#{item_id}. Worker is exiting..."
  # else
  #   msg = "SerialTasksManager: no available tasks found for #{item_class}/#{item_id}. Worker is exiting..."
  # end

  # If we are here, it means that no more tasks were found
  success = last_run[1]
  msg = success ? "Serially: finished all tasks for #{item_class}/#{item_id}. Serially::Job is stopping..." :
                  "Serially: task '#{last_run[0]}' for #{item_class}/#{item_id} finished with success: #{last_run[1]}, message: #{last_run[2]}"
  msg
end