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!(event_name = nil, allow_transition = true, allow_retry = true) ⇒ Object
- #boot_async!(event_name = nil, options = {}) ⇒ Object
- #cancellable? ⇒ Boolean
- #manual_task!(description) ⇒ Object
- #move_to!(task_name) ⇒ Object
- #queued? ⇒ Boolean
- #retry! ⇒ Object
- #retry_in!(delay, error = nil) ⇒ Object
- #schema ⇒ 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!(event_name = nil, allow_transition = true, allow_retry = true) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/models/level_up/job.rb', line 56 def boot!(event_name=nil, 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 boot!(next_task, allow_transition, allow_retry) elsif retry_at retry! end end |
#boot_async!(event_name = nil, options = {}) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'app/models/level_up/job.rb', line 74 def boot_async!(event_name=nil, ={}) begin Delayed::Job.transaction do self.delayed_job = delay().boot!(event_name) save end true rescue false end end |
#cancellable? ⇒ Boolean
99 100 101 |
# File 'app/models/level_up/job.rb', line 99 def cancellable? self.timer? or self.error? or self.delayed_job.nil? end |
#manual_task!(description) ⇒ Object
131 132 133 |
# File 'app/models/level_up/job.rb', line 131 def manual_task!(description) throw :manual_task, description end |
#move_to!(task_name) ⇒ Object
123 124 125 |
# File 'app/models/level_up/job.rb', line 123 def move_to!(task_name) throw :move_to, task_name end |
#queued? ⇒ Boolean
103 104 105 |
# File 'app/models/level_up/job.rb', line 103 def queued? !self.delayed_job.nil? end |
#retry! ⇒ Object
68 69 70 71 72 |
# File 'app/models/level_up/job.rb', line 68 def retry! set_timer save boot_async!(nil, run_at: retry_at) end |
#retry_in!(delay, error = nil) ⇒ Object
127 128 129 |
# File 'app/models/level_up/job.rb', line 127 def retry_in!(delay, error=nil) throw :retry_in, delay: delay, error: error end |
#schema ⇒ Object
119 120 121 |
# File 'app/models/level_up/job.rb', line 119 def schema self.class.schema end |
#task?(name) ⇒ Boolean
95 96 97 |
# File 'app/models/level_up/job.rb', line 95 def task?(name) self.task == name.to_s end |
#task_transitions ⇒ Object
115 116 117 |
# File 'app/models/level_up/job.rb', line 115 def task_transitions self.transitions(self.task.to_sym) end |
#tasks ⇒ Object
107 108 109 |
# File 'app/models/level_up/job.rb', line 107 def tasks self.class.tasks end |
#transitions(task_name) ⇒ Object
111 112 113 |
# File 'app/models/level_up/job.rb', line 111 def transitions(task_name) self.class.transitions(task_name) end |
#unqueue! ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'app/models/level_up/job.rb', line 86 def unqueue! if self.delayed_job self.delayed_job.destroy self.delayed_job = nil end clear_timer_attributes save end |