Sha256: 2c0f2030f6f2c356c79599a64fefcdffc431fe4c5d61453743527c797301ea7e

Contents?: true

Size: 1.92 KB

Versions: 7

Compression:

Stored size: 1.92 KB

Contents

require 'delorean_lang'

class Marty::PromiseJob < Struct.new(:promise,
                                     :title,
                                     :sname,
                                     :tag,
                                     :node,
                                     :params,
                                     :attrs,
                                     :hook,
                                     :max_run_time
                                    )
  # def log(msg)
  #   open('/tmp/dj.out', 'a') { |f| f.puts msg }
  # end
  #
  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
    # log "PERF #{Process.pid} #{title}"

    promise.set_start

    begin
      # in case the job writes to the the database
      Mcfly.whodunnit = promise.user

      engine = Marty::ScriptSet.new(tag).get_engine(sname)

      attrs_eval = engine.evaluate(node, attrs, params)
      res = attrs.zip(attrs_eval).each_with_object({}) do |(attr, val), h|
        h[attr] = val
      end

      # log "DONE #{Process.pid} #{promise.id} #{Time.now.to_f} #{res}"
    rescue ::Delayed::WorkerTimeout => e
      msg = ::Marty::Promise.timeout_message(promise)
      timeout_error = StandardError.new(
        "#{msg} (Triggered by #{e.class})"
      )
      timeout_error.set_backtrace(e.backtrace)

      res = Delorean::Engine.grok_runtime_exception(timeout_error)
    rescue StandardError => e
      res = Delorean::Engine.grok_runtime_exception(e)
      # log "ERR- #{Process.pid} #{promise.id} #{Time.now.to_f} #{e}"
    end

    promise.set_result(res)
    process_hook(res)
  end

  def process_hook(res)
      return unless hook

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

  def max_attempts
    1
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
marty-14.3.0 lib/marty/promise_job.rb
marty-14.0.0 lib/marty/promise_job.rb
marty-13.0.2 lib/marty/promise_job.rb
marty-11.0.0 lib/marty/promise_job.rb
marty-10.0.3 lib/marty/promise_job.rb
marty-10.0.2 lib/marty/promise_job.rb
marty-10.0.0 lib/marty/promise_job.rb