Class: Marty::PromiseJob

Inherits:
Struct
  • Object
show all
Defined in:
lib/marty/promise_job.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attrsObject

Returns the value of attribute attrs

Returns:

  • (Object)

    the current value of attrs



3
4
5
# File 'lib/marty/promise_job.rb', line 3

def attrs
  @attrs
end

#hookObject

Returns the value of attribute hook

Returns:

  • (Object)

    the current value of hook



3
4
5
# File 'lib/marty/promise_job.rb', line 3

def hook
  @hook
end

#max_run_timeObject

Returns the value of attribute max_run_time

Returns:

  • (Object)

    the current value of max_run_time



3
4
5
# File 'lib/marty/promise_job.rb', line 3

def max_run_time
  @max_run_time
end

#nodeObject

Returns the value of attribute node

Returns:

  • (Object)

    the current value of node



3
4
5
# File 'lib/marty/promise_job.rb', line 3

def node
  @node
end

#paramsObject

Returns the value of attribute params

Returns:

  • (Object)

    the current value of params



3
4
5
# File 'lib/marty/promise_job.rb', line 3

def params
  @params
end

#promiseObject

Returns the value of attribute promise

Returns:

  • (Object)

    the current value of promise



3
4
5
# File 'lib/marty/promise_job.rb', line 3

def promise
  @promise
end

#snameObject

Returns the value of attribute sname

Returns:

  • (Object)

    the current value of sname



3
4
5
# File 'lib/marty/promise_job.rb', line 3

def sname
  @sname
end

#tagObject

Returns the value of attribute tag

Returns:

  • (Object)

    the current value of tag



3
4
5
# File 'lib/marty/promise_job.rb', line 3

def tag
  @tag
end

#titleObject

Returns the value of attribute title

Returns:

  • (Object)

    the current value of title



3
4
5
# File 'lib/marty/promise_job.rb', line 3

def title
  @title
end

Instance Method Details

#enqueue(job) ⇒ Object

def log(msg)

open('/tmp/dj.out', 'a') { |f| f.puts msg }

end



17
18
19
20
21
22
23
24
25
26
# File 'lib/marty/promise_job.rb', line 17

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

#max_attemptsObject



70
71
72
# File 'lib/marty/promise_job.rb', line 70

def max_attempts
  1
end

#performObject



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
53
54
55
56
57
58
59
60
# File 'lib/marty/promise_job.rb', line 28

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

#process_hook(res) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/marty/promise_job.rb', line 62

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