class MotherBrain::Job

A Celluloid actor representing an active job. Jobs are handled by the {JobManager} and should not be returned to a consumer or user from the Public API.

Jobs start in the ‘pending’ state and can only be in any one state at a given time. A Job is completed when in the ‘success’ or ‘failure’ state.

The progress of a Job is recorded by the {JobManager} as a {JobRecord}. API consumers should reference the status of a running Job by it’s {JobRecord}.

Returning a {JobTicket} from the Public API will give a consumer or user an easy way to check the status of a job by polling a Job’s {JobRecord}.

@example running a job and checking it’s status

job = Job.new('example_job')
ticket = job.ticket

ticket.completed? => false
ticket.state => :pending

job.transition(:success, 'done!')

ticket.completed? => true
ticket.state => :success

@api private

Attributes

id[R]
machine[R]
result[R]
time_end[RW]

Set when the state of the Job changes from ‘running’ to ‘sucess’ or ‘failure’

@note do not modify outside of the state machine

@return [Time]

time_start[RW]

Set when the state of the Job changes from ‘pending’ to ‘running’

@note do not modify outside of the state machine

@return [Time]

type[R]

Public Class Methods

new(type) click to toggle source

@param [#to_s] type

# File lib/mb/job.rb, line 62
def initialize(type)
  @machine = StateMachine.new
  @type    = type.to_s
  @id      = job_manager.uuid
  @result  = nil
  job_manager.add(Actor.current)
end

Public Instance Methods

inspect()
Alias for: to_s
report_boolean(boolean, result = nil, options = {}) click to toggle source

@param [Boolean] boolean

a boolean value representing a success or failure

@param [#to_json] result

a result which can be converted to JSON

@param [Hash] options

options to pass to the state machine transition

@return [Job]

# File lib/mb/job.rb, line 122
def report_boolean(boolean, result = nil, options = {})
  if boolean
    report_success(result, options)
  else
    report_failure(result, options)
  end
end
report_failure(result = nil, options = {}) click to toggle source

@param [#to_json] result

a result which can be converted to JSON

@param [Hash] options

options to pass to the state machine transition

@return [Job]

# File lib/mb/job.rb, line 76
def report_failure(result = nil, options = {})
  log.fatal { "Job (#{id}) failure: #{result}" }
  transition(:failure, result, options)
end
report_pending(result = nil, options = {}) click to toggle source

@param [#to_json] result

a result which can be converted to JSON

@param [Hash] options

options to pass to the state machine transition

@return [Job]

# File lib/mb/job.rb, line 87
def report_pending(result = nil, options = {})
  log.info { "Job (#{id}) pending: #{result}" }
  transition(:pending, result, options)
end
report_running(result = nil, options = {}) click to toggle source

@param [#to_json] result

a result which can be converted to JSON

@param [Hash] options

options to pass to the state machine transition

@return [Job]

# File lib/mb/job.rb, line 98
def report_running(result = nil, options = {})
  log.info { "Job (#{id}) running: #{result}" }
  transition(:running, result, options)
end
report_success(result = nil, options = {}) click to toggle source

@param [#to_json] result

a result which can be converted to JSON

@param [Hash] options

options to pass to the state machine transition

@return [Job]

# File lib/mb/job.rb, line 109
def report_success(result = nil, options = {})
  log.info { "Job (#{id}) success: #{result}" }
  transition(:success, result, options)
end
save() click to toggle source

@return [self]

# File lib/mb/job.rb, line 131
def save
  job_manager.update(Actor.current)
end
set_status(string)
Alias for: status=
status() click to toggle source
# File lib/mb/job.rb, line 135
def status
  @status || state.to_s.capitalize
end
status=(string) click to toggle source
# File lib/mb/job.rb, line 139
def status=(string)
  log.info { "Job (#{id}) status: #{string}" }
  @status = string
  status_buffer << string
  save
end
Also aliased as: set_status
status_buffer() click to toggle source
# File lib/mb/job.rb, line 147
def status_buffer
  @status_buffer ||= []
end
ticket() click to toggle source

@return [JobTicket]

# File lib/mb/job.rb, line 152
def ticket
  @ticket ||= JobTicket.new(id)
end
to_s() click to toggle source
# File lib/mb/job.rb, line 170
def to_s
  "#<Job @type=#{type.inspect} @machine.state=#{state.inspect}>"
end
Also aliased as: inspect
transition(state, result = nil, options = {}) click to toggle source

@param [Symbol] state

the state to transition to in the Job's state machine

@param [#cause, to_s] result

a result which can be converted to JSON

@param [Hash] options

options to pass to the state machine transition

@return [Job]

# File lib/mb/job.rb, line 164
def transition(state, result = nil, options = {})
  @result = result.respond_to?(:cause) ? result.cause : result
  machine.transition(state, options)
  Actor.current
end

Private Instance Methods

finalize_callback() click to toggle source
# File lib/mb/job.rb, line 179
def finalize_callback
  job_manager.async.complete_job(Actor.current)
end