Class: LevelUp::Job
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- LevelUp::Job
- Defined in:
- app/models/level_up/job.rb
Instance Attribute Summary collapse
-
#next_state ⇒ Object
Returns the value of attribute next_state.
Class Method Summary collapse
- .job(&block) ⇒ Object
- .schema ⇒ Object
- .state(name, options = {}) ⇒ Object
- .states ⇒ Object
- .transitions(state) ⇒ 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(state_name) ⇒ Object
- #queued? ⇒ Boolean
- #retry! ⇒ Object
- #retry_in(delay, error = nil) ⇒ Object
- #schema ⇒ Object
- #state?(name) ⇒ Boolean
- #state_transitions ⇒ Object
- #states ⇒ Object
- #step!(event_name, allow_transition, allow_retry) ⇒ Object
- #transitions(state) ⇒ Object
- #unqueue! ⇒ Object
Instance Attribute Details
#next_state ⇒ Object
Returns the value of attribute next_state.
9 10 11 |
# File 'app/models/level_up/job.rb', line 9 def next_state @next_state end |
Class Method Details
.job(&block) ⇒ Object
27 28 29 30 31 32 |
# File 'app/models/level_up/job.rb', line 27 def job(&block) if block_given? class_eval(&block) schema[:end] = [] end end |
.schema ⇒ Object
19 20 21 |
# File 'app/models/level_up/job.rb', line 19 def schema @schema ||= {} end |
.state(name, options = {}) ⇒ Object
34 35 36 37 38 |
# File 'app/models/level_up/job.rb', line 34 def state(name, = {}) .reverse_merge!({moves_to: []}) transitions = [:moves_to].kind_of?(Symbol) ? Array([:moves_to]) : [:moves_to] schema[name] = transitions end |
.states ⇒ Object
23 24 25 |
# File 'app/models/level_up/job.rb', line 23 def states self.schema.keys end |
.transitions(state) ⇒ Object
40 41 42 43 44 45 46 |
# File 'app/models/level_up/job.rb', line 40 def transitions(state) if self.schema.has_key?(state) self.schema[state] else raise StateNotFound, state end end |
Instance Method Details
#boot! ⇒ Object
49 50 51 |
# File 'app/models/level_up/job.rb', line 49 def boot! event!(nil) end |
#boot_async!(event_name = nil, options = {}) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/models/level_up/job.rb', line 92 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
117 118 119 |
# File 'app/models/level_up/job.rb', line 117 def cancellable? self.timer? or self.error? or self.delayed_job.nil? end |
#clear!(event_name) ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'app/models/level_up/job.rb', line 65 def clear!(event_name) clear_timer_attributes clear_error_attributes clear_task_attributes self.next_state = nil self.retry_at = nil self.state = event_name if event_name save end |
#event!(event_name, allow_transition = true, allow_retry = true) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/models/level_up/job.rb', line 53 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_state event!(next_state, allow_transition, allow_retry) elsif retry_at retry! end end |
#manual_task(description) ⇒ Object
149 150 151 |
# File 'app/models/level_up/job.rb', line 149 def manual_task(description) throw :task, description end |
#move_to(state_name) ⇒ Object
141 142 143 |
# File 'app/models/level_up/job.rb', line 141 def move_to(state_name) throw :move_to, state_name end |
#queued? ⇒ Boolean
121 122 123 |
# File 'app/models/level_up/job.rb', line 121 def queued? !self.delayed_job.nil? end |
#retry! ⇒ Object
86 87 88 89 90 |
# File 'app/models/level_up/job.rb', line 86 def retry! set_timer save boot_async!(nil, run_at: retry_at) end |
#retry_in(delay, error = nil) ⇒ Object
145 146 147 |
# File 'app/models/level_up/job.rb', line 145 def retry_in(delay, error=nil) throw :retry_in, delay: delay, error: error end |
#schema ⇒ Object
137 138 139 |
# File 'app/models/level_up/job.rb', line 137 def schema self.class.schema end |
#state?(name) ⇒ Boolean
113 114 115 |
# File 'app/models/level_up/job.rb', line 113 def state?(name) self.state == name.to_s end |
#state_transitions ⇒ Object
133 134 135 |
# File 'app/models/level_up/job.rb', line 133 def state_transitions self.transitions(self.state.to_sym) end |
#states ⇒ Object
125 126 127 |
# File 'app/models/level_up/job.rb', line 125 def states self.class.states end |
#step!(event_name, allow_transition, allow_retry) ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'app/models/level_up/job.rb', line 76 def step!(event_name, allow_transition, allow_retry) begin run_state(event_name, allow_transition, allow_retry) rescue => ex set_error(ex) ensure save end end |
#transitions(state) ⇒ Object
129 130 131 |
# File 'app/models/level_up/job.rb', line 129 def transitions(state) self.class.transitions(state) end |
#unqueue! ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'app/models/level_up/job.rb', line 104 def unqueue! if self.delayed_job self.delayed_job.destroy self.delayed_job = nil end clear_timer_attributes save end |