Class: Proxy::OpenBolt::Job

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/smart_proxy_openbolt/job.rb

Direct Known Subclasses

TaskJob

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parameters, options) ⇒ Job

Valid statuses are

:pending - waiting to run
:running - in progress
:success - job finished as was completely successful
:failure - job finished and had one or more failures
:exception - command exited with an unexpected code


17
18
19
20
21
22
23
24
# File 'lib/smart_proxy_openbolt/job.rb', line 17

def initialize(name, parameters, options)
  @id         = nil
  @name       = name
  @parameters = parameters
  @options    = options
  @status     = :pending
  @mutex      = Mutex.new
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/smart_proxy_openbolt/job.rb', line 7

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/smart_proxy_openbolt/job.rb', line 8

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/smart_proxy_openbolt/job.rb', line 8

def options
  @options
end

#parametersObject (readonly)

Returns the value of attribute parameters.



8
9
10
# File 'lib/smart_proxy_openbolt/job.rb', line 8

def parameters
  @parameters
end

#statusObject (readonly)

Returns the value of attribute status.



8
9
10
# File 'lib/smart_proxy_openbolt/job.rb', line 8

def status
  @status
end

Instance Method Details

#executeObject

Raises:

  • (NotImplementedError)


26
27
28
# File 'lib/smart_proxy_openbolt/job.rb', line 26

def execute
  raise NotImplementedError, "You must call #execute on a subclass of Job"
end

#processObject

Called by worker. The ‘execute’ function should return a Proxy::OpenBolt::Result object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/smart_proxy_openbolt/job.rb', line 32

def process
  update_status(:running)
  begin
    result = execute
    update_status(result.status)
    store_result(result)
  rescue Exception => e # rubocop:disable Lint/RescueException
    # Catch everything including non-StandardError exceptions (e.g.
    # ScriptError) so the job always gets a terminal status.
    update_status(:exception)
    logger.error("Job #{@id} failed (#{e.class}): #{e.message}")
    logger.debug(e.backtrace.join("\n")) if e.backtrace
    begin
      store_result({ message: e.full_message, backtrace: e.backtrace })
    rescue StandardError => store_error
      logger.error("Job #{@id}: failed to store error result: #{store_error.message}")
      logger.debug(store_error.backtrace.join("\n")) if store_error.backtrace
    end
  end
end

#resultObject

Read the result back from disk as a raw JSON string. We avoid parsing and re-serializing since the file is already valid JSON.



63
64
65
# File 'lib/smart_proxy_openbolt/job.rb', line 63

def result
  File.read(Proxy::OpenBolt.result_file_path(@id))
end

#store_result(value) ⇒ Object



57
58
59
# File 'lib/smart_proxy_openbolt/job.rb', line 57

def store_result(value)
  File.write(Proxy::OpenBolt.result_file_path(@id), value.to_json)
end

#update_status(value) ⇒ Object



53
54
55
# File 'lib/smart_proxy_openbolt/job.rb', line 53

def update_status(value)
  @mutex.synchronize { @status = value }
end