Class: Executo::Worker

Inherits:
Object
  • Object
show all
Includes:
TaggedLogger, Sidekiq::Worker
Defined in:
lib/executo/worker.rb

Direct Known Subclasses

EncryptedWorker

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#flushable_outputObject (readonly)

Returns the value of attribute flushable_output.



8
9
10
# File 'lib/executo/worker.rb', line 8

def flushable_output
  @flushable_output
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/executo/worker.rb', line 8

def options
  @options
end

#outputObject (readonly)

Returns the value of attribute output.



8
9
10
# File 'lib/executo/worker.rb', line 8

def output
  @output
end

Instance Method Details

#perform(command, params = [], options = {}) ⇒ Object

Parameters:

  • command (String)
  • params (Array) (defaults to: [])
  • options (Hash) (defaults to: {})


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/executo/worker.rb', line 13

def perform(command, params = [], options = {})
  @options = options
  @output ||= Hash.new([])
  @flushable_output ||= Hash.new([])
  @started_at = Time.current
  @flushed_at = Time.current

  logger_add_tag('Worker')
  logger_add_tag(options.dig('feedback', 'id'))
  logger_add_tag(command)

  logger.debug "params: #{params}"
  logger.debug "options: #{options}"

  send_feedback(state: 'started')

  status = execute(command, params, options)
  send_feedback(
    state: status.success? ? 'completed' : 'failed',
    exitstatus: status.exitstatus.to_i,
    stdout: output[:stdout],
    stderr: output[:stderr],
    pid: status.pid
  )
rescue StandardError => e
  logger.error "Exception: #{e.class} - #{e.message}"
  logger.error e.backtrace.join("\n")
  send_feedback(state: 'failed')
ensure
  send_feedback(state: 'finished')
end