Class: StateMachineJob::Macro::JobDSL

Inherits:
Object
  • Object
show all
Defined in:
lib/state_machine_job/macro.rb

Instance Method Summary collapse

Constructor Details

#initialize(job, state_machine) ⇒ JobDSL

Returns a new instance of JobDSL.



8
9
10
11
12
13
14
# File 'lib/state_machine_job/macro.rb', line 8

def initialize(job, state_machine)
  @job = job
  @state_machine = state_machine
  @payload = lambda do |*|
    {}
  end
end

Instance Method Details

#on_enter(state) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/state_machine_job/macro.rb', line 54

def on_enter(state)
  @on_enter_state = state

  @state_machine.after_transition @state_machine.any => state do |object|
    @job.perform_later(object, @payload.call(object))
  end
end

#payload(&block) ⇒ Object



62
63
64
# File 'lib/state_machine_job/macro.rb', line 62

def payload(&block)
  @payload = block
end

#result(job_result, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/state_machine_job/macro.rb', line 16

def result(job_result, options = {})
  if job_result.is_a?(Hash)
    if job_result.size > 1
      raise("Use an explicit :state option when passing additional options.\n\n      result :ok, :state => :done, :if => ...\n  NOT result :ok => :done, :if => ...\n\n")
    end

    return result(job_result.first.first, :state => job_result.first.last)
  end

  if options[:retry_if_state] && options[:retry_after]
    raise('Combining the :retry_after and :retry_on_state options is not supported at the moment.')
  end

  if options[:retry_if_state] && !@on_enter_state
    raise('The on_enter call must appear above any result using the :retry_if_state option.')
  end

  if options[:if] && options[:retry_after]
    raise('Combining the :retry_after and :if options is not supported at the moment.')
  end

  on_enter_state = @on_enter_state

  if options[:state]
    @state_machine.event(StateMachineJob.result_event_name(@job, job_result)) do
      if options[:retry_if_state]
        transition options[:retry_if_state] => on_enter_state
      end

      transition(all => options[:state], :if => options[:if])
    end
  elsif options[:retry_after]
    @state_machine.define_helper :instance, StateMachineJob.result_method_name(@job, job_result) do |_machine, object|
      @job.set(wait: options[:retry_after]).perform_later(object, @payload.call(object))
    end
  end
end