Module: Dry::Transaction::Extra::Steps::Async::DSL

Defined in:
lib/dry/transaction/extra/steps/async.rb

Instance Method Summary collapse

Instance Method Details

#async(job, delay: nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dry/transaction/extra/steps/async.rb', line 32

def async(job, delay: nil)
  method_name = job.name.underscore.intern
  step method_name
  define_method method_name do |input = {}|
    job = job.set(wait: delay) if delay

    if (validator = job&.validator)
      result = validator.new.call(input)
      # If the validator failed, don't enqueue the job, but don't fail the step
      if result.success?
        job.perform_later(**result.to_h)
      else
        steps
          .detect { |step| step.name == method_name }
          .publish(:async_step_validation_failed,
                   step_name: method_name,
                   args: input,
                   value: result)
      end
    else
      job.perform_later(**input)
    end

    Success(input)
  end
end