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



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

def attrs
  @attrs
end

#hookObject

Returns the value of attribute hook



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

def hook
  @hook
end

#nodeObject

Returns the value of attribute node



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

def node
  @node
end

#paramsObject

Returns the value of attribute params



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

def params
  @params
end

#promiseObject

Returns the value of attribute promise



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

def promise
  @promise
end

#snameObject

Returns the value of attribute sname



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

def sname
  @sname
end

#tagObject

Returns the value of attribute tag



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

def tag
  @tag
end

#titleObject

Returns the value of attribute 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



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

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



60
61
62
# File 'lib/marty/promise_job.rb', line 60

def max_attempts
  1
end

#performObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/marty/promise_job.rb', line 27

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 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



52
53
54
55
56
57
58
# File 'lib/marty/promise_job.rb', line 52

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