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
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]
Set when the state of the Job
changes from ‘pending’ to ‘running’
@note do not modify outside of the state machine
@return [Time]
Public Class Methods
@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
@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
@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
@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
@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
@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
@return [self]
# File lib/mb/job.rb, line 131 def save job_manager.update(Actor.current) end
# File lib/mb/job.rb, line 135 def status @status || state.to_s.capitalize end
# File lib/mb/job.rb, line 139 def status=(string) log.info { "Job (#{id}) status: #{string}" } @status = string status_buffer << string save end
# File lib/mb/job.rb, line 147 def status_buffer @status_buffer ||= [] end
@return [JobTicket]
# File lib/mb/job.rb, line 152 def ticket @ticket ||= JobTicket.new(id) end
# File lib/mb/job.rb, line 170 def to_s "#<Job @type=#{type.inspect} @machine.state=#{state.inspect}>" end
@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
# File lib/mb/job.rb, line 179 def finalize_callback job_manager.async.complete_job(Actor.current) end