Class: LevelUp::Job

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/level_up/job.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#next_taskObject

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

.schemaObject



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, options = {})
  options.reverse_merge!({transitions: []})
  transitions = options[:transitions].kind_of?(Symbol) ? Array(options[:transitions]) : options[:transitions]
  schema[name] = transitions
  task_classes[name] = options[:class_name] if options.key?(:class_name)
end

.task_classesObject



25
26
27
# File 'app/models/level_up/job.rb', line 25

def task_classes
  @task_classes ||= {}
end

.tasksObject



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, options={})
  begin
    Delayed::Job.transaction do
      self.delayed_job = delay(options).boot!(event_name)
      save
    end
    true
  rescue
    false
  end
end

#cancellable?Boolean

Returns:

  • (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

Returns:

  • (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

#schemaObject



119
120
121
# File 'app/models/level_up/job.rb', line 119

def schema
  self.class.schema
end

#task?(name) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'app/models/level_up/job.rb', line 95

def task?(name)
  self.task == name.to_s
end

#task_transitionsObject



115
116
117
# File 'app/models/level_up/job.rb', line 115

def task_transitions
  self.transitions(self.task.to_sym)
end

#tasksObject



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