Class: Marty::PromiseRubyJob

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hookObject

Returns the value of attribute hook

Returns:

  • (Object)

    the current value of hook



1
2
3
# File 'lib/marty/promise_ruby_job.rb', line 1

def hook
  @hook
end

#max_run_timeObject

Returns the value of attribute max_run_time

Returns:

  • (Object)

    the current value of max_run_time



1
2
3
# File 'lib/marty/promise_ruby_job.rb', line 1

def max_run_time
  @max_run_time
end

#method_argsObject

Returns the value of attribute method_args

Returns:

  • (Object)

    the current value of method_args



1
2
3
# File 'lib/marty/promise_ruby_job.rb', line 1

def method_args
  @method_args
end

#method_nameObject

Returns the value of attribute method_name

Returns:

  • (Object)

    the current value of method_name



1
2
3
# File 'lib/marty/promise_ruby_job.rb', line 1

def method_name
  @method_name
end

#module_nameObject

Returns the value of attribute module_name

Returns:

  • (Object)

    the current value of module_name



1
2
3
# File 'lib/marty/promise_ruby_job.rb', line 1

def module_name
  @module_name
end

#promiseObject

Returns the value of attribute promise

Returns:

  • (Object)

    the current value of promise



1
2
3
# File 'lib/marty/promise_ruby_job.rb', line 1

def promise
  @promise
end

#titleObject

Returns the value of attribute title

Returns:

  • (Object)

    the current value of title



1
2
3
# File 'lib/marty/promise_ruby_job.rb', line 1

def title
  @title
end

Instance Method Details

#enqueue(job) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/marty/promise_ruby_job.rb', line 10

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



55
56
57
# File 'lib/marty/promise_ruby_job.rb', line 55

def max_attempts
  1
end

#performObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/marty/promise_ruby_job.rb', line 21

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 ::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 = ::Marty::Promise.exception_to_result(promise: promise, exception: e)
  end

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

#process_hook(res) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/marty/promise_ruby_job.rb', line 47

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