Class: Proxy::Dynflow::Runner::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_proxy_dynflow/runner/base.rb

Overview

Runner is an object that is able to initiate some action and provide update data on refresh call.

Direct Known Subclasses

CommandRunner, Parent

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*_args, suspended_action: nil, id: nil) ⇒ Base

Returns a new instance of Base.



11
12
13
14
15
# File 'lib/smart_proxy_dynflow/runner/base.rb', line 11

def initialize(*_args, suspended_action: nil, id: nil)
  @suspended_action = suspended_action
  @id = id || SecureRandom.uuid
  initialize_continuous_outputs
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/smart_proxy_dynflow/runner/base.rb', line 8

def id
  @id
end

#loggerObject



17
18
19
# File 'lib/smart_proxy_dynflow/runner/base.rb', line 17

def logger
  @logger ||= Logger.new($stderr)
end

Instance Method Details

#closeObject



46
47
48
# File 'lib/smart_proxy_dynflow/runner/base.rb', line 46

def close
  # if cleanup is needed
end

#dispatch_exception(context, exception) ⇒ Object



77
78
79
# File 'lib/smart_proxy_dynflow/runner/base.rb', line 77

def dispatch_exception(context, exception)
  @continuous_output.add_exception(context, exception)
end

#external_event(_event) ⇒ Object

by default, external event just causes the refresh to be triggered: this allows the descendants of the Base to add custom logic to process the external events. Similarly as run_refresh, it’s expected to return updates to be dispatched.



30
31
32
# File 'lib/smart_proxy_dynflow/runner/base.rb', line 30

def external_event(_event)
  run_refresh
end

#generate_updatesObject



81
82
83
84
85
86
87
# File 'lib/smart_proxy_dynflow/runner/base.rb', line 81

def generate_updates
  return no_update if @continuous_output.empty? && @exit_status.nil?

  new_data = @continuous_output
  @continuous_output = Proxy::Dynflow::ContinuousOutput.new
  new_update(new_data, @exit_status)
end

#initialize_continuous_outputsObject



97
98
99
# File 'lib/smart_proxy_dynflow/runner/base.rb', line 97

def initialize_continuous_outputs
  @continuous_output = ::Proxy::Dynflow::ContinuousOutput.new
end

#killObject



42
43
44
# File 'lib/smart_proxy_dynflow/runner/base.rb', line 42

def kill
  # Override when you can kill the runner in the middle
end

#new_update(data, exit_status) ⇒ Object



93
94
95
# File 'lib/smart_proxy_dynflow/runner/base.rb', line 93

def new_update(data, exit_status)
  { @suspended_action => Runner::Update.new(data, exit_status, exit_status_timestamp: @exit_status_timestamp) }
end

#no_updateObject



89
90
91
# File 'lib/smart_proxy_dynflow/runner/base.rb', line 89

def no_update
  {}
end

#publish_dataObject



61
62
63
# File 'lib/smart_proxy_dynflow/runner/base.rb', line 61

def publish_data(...)
  @continuous_output.add_output(...)
end

#publish_exception(context, exception, fatal = true) ⇒ Object



65
66
67
68
69
70
# File 'lib/smart_proxy_dynflow/runner/base.rb', line 65

def publish_exception(context, exception, fatal = true)
  logger.error("#{context} - #{exception.class} #{exception.message}:\n" + \
               exception.backtrace.join("\n"))
  dispatch_exception context, exception
  publish_exit_status('EXCEPTION') if fatal
end

#publish_exit_status(status) ⇒ Object



72
73
74
75
# File 'lib/smart_proxy_dynflow/runner/base.rb', line 72

def publish_exit_status(status)
  @exit_status = status
  @exit_status_timestamp = Time.now.utc
end

#refreshObject

Raises:

  • (NotImplementedError)


38
39
40
# File 'lib/smart_proxy_dynflow/runner/base.rb', line 38

def refresh
  raise NotImplementedError
end

#run_refreshObject



21
22
23
24
25
# File 'lib/smart_proxy_dynflow/runner/base.rb', line 21

def run_refresh
  logger.debug('refreshing runner')
  refresh
  generate_updates
end

#run_refresh_outputObject



101
102
103
104
105
# File 'lib/smart_proxy_dynflow/runner/base.rb', line 101

def run_refresh_output
  logger.debug('refreshing runner on demand')
  refresh
  generate_updates
end

#startObject

Raises:

  • (NotImplementedError)


34
35
36
# File 'lib/smart_proxy_dynflow/runner/base.rb', line 34

def start
  raise NotImplementedError
end

#timeoutObject



50
51
52
53
54
# File 'lib/smart_proxy_dynflow/runner/base.rb', line 50

def timeout
  # Override when timeouts and regular kills should be handled differently
  publish_data('Timeout for execution passed, trying to stop the job', 'debug')
  kill
end

#timeout_intervalObject



56
57
58
59
# File 'lib/smart_proxy_dynflow/runner/base.rb', line 56

def timeout_interval
  # A number of seconds after which the runner should receive a #timeout
  #   or nil for no timeout
end