Sha256: 297479a2d01afb813d18bf609580c820e0abb97aa8c17c8ea5fa3da39dae5952

Contents?: true

Size: 1.18 KB

Versions: 5

Compression:

Stored size: 1.18 KB

Contents

class Marty::PromiseRubyJob < Struct.new(:promise,
                                         :title,
                                         :module_name,
                                         :method_name,
                                         :method_args,
                                         :hook,
                                        )

  def enqueue(job)
    config = Rails.configuration.marty
    hooks = config.promise_job_enqueue_hooks

    return if hooks.blank?

    hooks.each do |hook|
      hook.call(job)
    end
  end

  def perform
    promise.set_start

    begin
      Mcfly.whodunnit = promise.user

      ENV['__promise_id'] = promise.id.to_s
      mod = module_name.constantize
      res = { 'result' => mod.send(method_name, *method_args) }
    rescue StandardError => e
      res = ::Marty::Promise.exception_to_result(promise: promise, exception: e)
    end

    promise.set_result(res)
    process_hook(res)
    ENV.delete('__promise_id')
  end

  def process_hook(res)
    return unless hook

    hook.run(params: method_args, result: res)
  rescue StandardError => e
    Marty::Util.logger.error "promise hook failed: #{e}"
  end

  def max_attempts
    1
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
marty-8.5.0 lib/marty/promise_ruby_job.rb
marty-8.4.1 lib/marty/promise_ruby_job.rb
marty-8.3.1 lib/marty/promise_ruby_job.rb
marty-8.2.0 lib/marty/promise_ruby_job.rb
marty-8.0.0 lib/marty/promise_ruby_job.rb