Class: LevelUp::Job
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- LevelUp::Job
- Defined in:
- app/models/level_up/job.rb
Instance Attribute Summary collapse
-
#next_task ⇒ Object
Returns the value of attribute next_task.
Class Method Summary collapse
- .job(&block) ⇒ Object
- .schema ⇒ Object
- .task(name, options = {}) ⇒ Object
- .task_classes ⇒ Object
- .tasks ⇒ Object
- .transitions(task_name) ⇒ Object
Instance Method Summary collapse
- #boot! ⇒ Object
- #boot_async!(event_name = nil, options = {}) ⇒ Object
- #cancellable? ⇒ Boolean
- #clear!(event_name) ⇒ Object
- #event!(event_name, allow_transition = true, allow_retry = true) ⇒ Object
- #manual_task!(description) ⇒ Object
- #move_to!(task_name) ⇒ Object
- #queued? ⇒ Boolean
- #retry! ⇒ Object
- #retry_in!(delay, error = nil) ⇒ Object
- #schema ⇒ Object
- #step!(event_name, allow_transition, allow_retry) ⇒ Object
- #task?(name) ⇒ Boolean
- #task_transitions ⇒ Object
- #tasks ⇒ Object
- #transitions(task_name) ⇒ Object
- #unqueue! ⇒ Object
Instance Attribute Details
#next_task ⇒ Object
Returns the value of attribute next_task.
11 12 13 |
# File 'app/models/level_up/job.rb', line 11 def next_task @next_task end |
Class Method Details
.job(&block) ⇒ Object
33 34 35 36 37 38 |
# File 'app/models/level_up/job.rb', line 33 def job(&block) if block_given? class_eval(&block) schema[:end] = [] end end |
.schema ⇒ Object
21 22 23 |
# File 'app/models/level_up/job.rb', line 21 def schema @schema ||= {} end |
.task(name, options = {}) ⇒ Object
40 41 42 43 44 45 |
# File 'app/models/level_up/job.rb', line 40 def task(name, = {}) .reverse_merge!({transitions: []}) transitions = [:transitions].kind_of?(Symbol) ? Array([:transitions]) : [:transitions] schema[name] = transitions task_classes[name] = [:class_name] if .key?(:class_name) end |
.task_classes ⇒ Object
25 26 27 |
# File 'app/models/level_up/job.rb', line 25 def task_classes @task_classes ||= {} end |
.tasks ⇒ Object
29 30 31 |
# File 'app/models/level_up/job.rb', line 29 def tasks self.schema.keys end |
.transitions(task_name) ⇒ Object
47 48 49 50 51 52 53 |
# File 'app/models/level_up/job.rb', line 47 def transitions(task_name) if self.schema.has_key?(task_name) self.schema[task_name] else raise TaskNotFound, task_name end end |
Instance Method Details
#boot! ⇒ Object
56 57 58 |
# File 'app/models/level_up/job.rb', line 56 def boot! event!(nil) end |
#boot_async!(event_name = nil, options = {}) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 |
# File 'app/models/level_up/job.rb', line 99 def boot_async!(event_name=nil, ={}) begin Delayed::Job.transaction do self.delayed_job = delay().event!(event_name) save end true rescue false end end |
#cancellable? ⇒ Boolean
124 125 126 |
# File 'app/models/level_up/job.rb', line 124 def cancellable? self.timer? or self.error? or self.delayed_job.nil? end |
#clear!(event_name) ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'app/models/level_up/job.rb', line 72 def clear!(event_name) clear_timer_attributes clear_error_attributes clear_task_attributes self.next_task = nil self.retry_at = nil self.task = event_name if event_name save end |
#event!(event_name, allow_transition = true, allow_retry = true) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'app/models/level_up/job.rb', line 60 def event!(event_name, allow_transition=true, allow_retry=true) event_name = event_name.to_s if event_name clear!(event_name) step!(event_name, allow_transition, allow_retry) if next_task event!(next_task, allow_transition, allow_retry) elsif retry_at retry! end end |
#manual_task!(description) ⇒ Object
156 157 158 |
# File 'app/models/level_up/job.rb', line 156 def manual_task!(description) throw :manual_task, description end |
#move_to!(task_name) ⇒ Object
148 149 150 |
# File 'app/models/level_up/job.rb', line 148 def move_to!(task_name) throw :move_to, task_name end |
#queued? ⇒ Boolean
128 129 130 |
# File 'app/models/level_up/job.rb', line 128 def queued? !self.delayed_job.nil? end |
#retry! ⇒ Object
93 94 95 96 97 |
# File 'app/models/level_up/job.rb', line 93 def retry! set_timer save boot_async!(nil, run_at: retry_at) end |
#retry_in!(delay, error = nil) ⇒ Object
152 153 154 |
# File 'app/models/level_up/job.rb', line 152 def retry_in!(delay, error=nil) throw :retry_in, delay: delay, error: error end |
#schema ⇒ Object
144 145 146 |
# File 'app/models/level_up/job.rb', line 144 def schema self.class.schema end |
#step!(event_name, allow_transition, allow_retry) ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'app/models/level_up/job.rb', line 83 def step!(event_name, allow_transition, allow_retry) begin run_task(event_name, allow_transition, allow_retry) rescue => ex set_error(ex) ensure save end end |
#task?(name) ⇒ Boolean
120 121 122 |
# File 'app/models/level_up/job.rb', line 120 def task?(name) self.task == name.to_s end |
#task_transitions ⇒ Object
140 141 142 |
# File 'app/models/level_up/job.rb', line 140 def task_transitions self.transitions(self.task.to_sym) end |
#tasks ⇒ Object
132 133 134 |
# File 'app/models/level_up/job.rb', line 132 def tasks self.class.tasks end |
#transitions(task_name) ⇒ Object
136 137 138 |
# File 'app/models/level_up/job.rb', line 136 def transitions(task_name) self.class.transitions(task_name) end |
#unqueue! ⇒ Object
111 112 113 114 115 116 117 118 |
# File 'app/models/level_up/job.rb', line 111 def unqueue! if self.delayed_job self.delayed_job.destroy self.delayed_job = nil end clear_timer_attributes save end |