Class: MotherBrain::Job Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Celluloid, MB::Job::States, MB::Logging, MB::Mixin::Services
Defined in:
lib/mb/job.rb,
lib/mb/job/states.rb,
lib/mb/job/state_machine.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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.

Examples:

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

Defined Under Namespace

Modules: States Classes: StateMachine

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Job

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Job.

Parameters:



62
63
64
65
66
67
68
# 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

Instance Attribute Details

#idObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



39
40
41
# File 'lib/mb/job.rb', line 39

def id
  @id
end

#resultObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



41
42
43
# File 'lib/mb/job.rb', line 41

def result
  @result
end

#time_endTime

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

do not modify outside of the state machine

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

Returns:

  • (Time)


55
56
57
# File 'lib/mb/job.rb', line 55

def time_end
  @time_end
end

#time_startTime

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

do not modify outside of the state machine

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

Returns:

  • (Time)


48
49
50
# File 'lib/mb/job.rb', line 48

def time_start
  @time_start
end

#typeObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



40
41
42
# File 'lib/mb/job.rb', line 40

def type
  @type
end

Instance Method Details

#report_boolean(boolean, result = nil, options = {}) ⇒ Job

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • boolean (Boolean)

    a boolean value representing a success or failure

  • result (#to_json) (defaults to: nil)

    a result which can be converted to JSON

  • options (Hash) (defaults to: {})

    options to pass to the state machine transition

Returns:



122
123
124
125
126
127
128
# 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 = {}) ⇒ Job

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • result (#to_json) (defaults to: nil)

    a result which can be converted to JSON

  • options (Hash) (defaults to: {})

    options to pass to the state machine transition

Returns:



76
77
78
79
# 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 = {}) ⇒ Job

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • result (#to_json) (defaults to: nil)

    a result which can be converted to JSON

  • options (Hash) (defaults to: {})

    options to pass to the state machine transition

Returns:



87
88
89
90
# 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 = {}) ⇒ Job

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • result (#to_json) (defaults to: nil)

    a result which can be converted to JSON

  • options (Hash) (defaults to: {})

    options to pass to the state machine transition

Returns:



98
99
100
101
# 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 = {}) ⇒ Job

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • result (#to_json) (defaults to: nil)

    a result which can be converted to JSON

  • options (Hash) (defaults to: {})

    options to pass to the state machine transition

Returns:



109
110
111
112
# File 'lib/mb/job.rb', line 109

def report_success(result = nil, options = {})
  log.info { "Job (#{id}) success: #{result}" }
  transition(:success, result, options)
end

#saveself

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (self)


131
132
133
# File 'lib/mb/job.rb', line 131

def save
  job_manager.update(Actor.current)
end

#statusObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



135
136
137
# File 'lib/mb/job.rb', line 135

def status
  @status || state.to_s.capitalize
end

#status=(string) ⇒ Object Also known as: set_status

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



139
140
141
142
143
144
# File 'lib/mb/job.rb', line 139

def status=(string)
  log.info { "Job (#{id}) status: #{string}" }
  @status = string
  status_buffer << string
  save
end

#status_bufferObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



147
148
149
# File 'lib/mb/job.rb', line 147

def status_buffer
  @status_buffer ||= []
end

#ticketJobTicket

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



152
153
154
# File 'lib/mb/job.rb', line 152

def ticket
  @ticket ||= JobTicket.new(id)
end

#to_sObject Also known as: inspect

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



170
171
172
# File 'lib/mb/job.rb', line 170

def to_s
  "#<Job @type=#{type.inspect} @machine.state=#{state.inspect}>"
end

#transition(state, result = nil, options = {}) ⇒ Job

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • state (Symbol)

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

  • result (#cause, #to_s) (defaults to: nil)

    a result which can be converted to JSON

  • options (Hash) (defaults to: {})

    options to pass to the state machine transition

Returns:



164
165
166
167
168
# 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