Class: Operations::Task

Inherits:
ApplicationRecord
  • Object
show all
Includes:
HasAttributes, Index, Plan, Testing
Defined in:
app/models/operations/task.rb

Defined Under Namespace

Modules: Index, Plan, Testing Classes: Runner

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Plan

#in?

Class Method Details

.call(task_status: "active", **attributes) ⇒ Object Also known as: perform_now



72
73
74
75
76
# File 'app/models/operations/task.rb', line 72

def call(task_status: "active", **attributes)
  create!(attributes.merge(task_status: task_status, current_state: initial_state).merge(default_times)).tap do |t|
    t.call
  end
end

.delete_oldObject



94
95
96
97
98
# File 'app/models/operations/task.rb', line 94

def delete_old
  Task.ready_to_delete.find_each do |t|
    Operations::DeleteOldTaskJob.perform_later(t)
  end
end

.later(**attributes) ⇒ Object Also known as: perform_later



79
# File 'app/models/operations/task.rb', line 79

def later(**attributes) = call(task_status: "waiting", **attributes)

.wake_sleepingObject



82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/operations/task.rb', line 82

def wake_sleeping
  adapter = Operations::WakeTaskJob.queue_adapter
  begin
    Task.ready_to_wake.find_each do |task|
      Operations::WakeTaskJob.queue_adapter = task.class.queue_adapter
      Operations::WakeTaskJob.perform_later task
    end
  ensure
    Operations::WakeTaskJob.queue_adapter = adapter
  end
end

Instance Method Details

#call(immediate: false) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/operations/task.rb', line 29

def call(immediate: false)
  state = ""
  while active? && (state != current_state)
    state = current_state
    Rails.logger.debug { "--- #{self}: #{current_state}" }
    (immediate || state_is_immediate?(current_state)) ? call_handler : go_to_sleep!
  end
rescue => ex
  record_error! ex
  raise ex
end

#call_handlerObject



51
# File 'app/models/operations/task.rb', line 51

def call_handler = handler_for(current_state).call(self)

#go_to(next_state) ⇒ Object



41
42
43
# File 'app/models/operations/task.rb', line 41

def go_to(next_state)
  update! current_state: next_state, task_status: (state_is_immediate?(next_state) ? "active" : "waiting")
end

#record_error!(exception) ⇒ Object



49
# File 'app/models/operations/task.rb', line 49

def record_error!(exception) = update!(task_status: "failed", exception_class: exception.class.to_s, exception_message: exception.message.to_s, exception_backtrace: exception.backtrace)

#start(task_class, **attributes) ⇒ Object



47
# File 'app/models/operations/task.rb', line 47

def start(task_class, **attributes) = task_class.later(**attributes.merge(parent: self))

#wake_up!Object



45
# File 'app/models/operations/task.rb', line 45

def wake_up! = timeout_expired? ? call_timeout_handler : activate_and_call