class JobState::Base

Public Class Methods

all() click to toggle source

Like find, you tend to call this on the subclass, not JobState::Base itself.

# File lib/job_state/base.rb, line 20
def self.all
  statuses = Resque::Plugins::Status::Hash.statuses
  statuses.select!{|s| s.options && s.options['job_kind'] == kind}
  statuses.map!{|s| self.new(s.uuid)}
  statuses.reject!{|s| filter_out(s)}
  statuses
end
find(job_uuid) click to toggle source

You generally call this on a subclass, for example

job_state = JobState::RunMarathon.find(uuid)

instead of

job_state = JobState::Base.find(uuid)

so that you can take advantage of the extra behavior of that particular job state class.

# File lib/job_state/base.rb, line 14
def self.find(job_uuid)
  self.new(job_uuid)
end
find_all_by(job_params) click to toggle source

Allows you to locate specific resque jobs based on the job parameters that were sent when the job was created.

# File lib/job_state/base.rb, line 30
def self.find_all_by(job_params)
  all.select do |job_state|
    keep = true
    job_params.each do |name, value|
      keep = false if job_state.get_job_param(name).to_s != value.to_s
    end
    keep
  end
end
new(uuid) click to toggle source
# File lib/job_state/base.rb, line 4
def initialize(uuid)
  @job_uuid = uuid  # uuid from resque-status
end

Private Class Methods

filter_out(csv_import_job_state) click to toggle source

Override this in your subclass to determine which jobs are available to the find and all methods.

# File lib/job_state/base.rb, line 135
def self.filter_out(csv_import_job_state)
  false
end
kind() click to toggle source
# File lib/job_state/base.rb, line 140
def self.kind
  self.to_s.split('::').last
end

Public Instance Methods

error?() click to toggle source
# File lib/job_state/base.rb, line 104
def error?
  h = status_hash
  h.failed? || h.killed?
end
full_info() click to toggle source

to be sent up to JavaScript land as JSON

# File lib/job_state/base.rb, line 41
def full_info
  hash = progress_metrics
  if success?
    hash[:job_state] = 'success'
  elsif error?
    hash[:job_state] = 'error'
  else
    hash[:job_state] = 'working'
  end
  hash
end
get_job_param(name) click to toggle source
# File lib/job_state/base.rb, line 86
def get_job_param(name)
  job_params[name]
end
get_progress_metric(name) click to toggle source
# File lib/job_state/base.rb, line 62
def get_progress_metric(name)
  progress_metrics[name.to_s]
end
job_params() click to toggle source

**********************************************************************

JOB PARAMS

This is the info you originally passed to the Resque job when it was
created.
# File lib/job_state/base.rb, line 82
def job_params
  HashWithIndifferentAccess.new(status_hash.options || {})
end
progress_metrics() click to toggle source

**********************************************************************

PROGRESS METRICS

This is how you communicate progress status updates to your Rails app.
# File lib/job_state/base.rb, line 58
def progress_metrics
  HashWithIndifferentAccess.new(status_hash['progress'] || {})
end
set_progress_metric(name, value) click to toggle source
# File lib/job_state/base.rb, line 66
def set_progress_metric(name, value)
  progress_hash = progress_metrics
  progress_hash[name.to_s] = value.to_s
  # This is how we set a new value on a Status::Hash object.
  # This is not documented well in the resque-status gem's README.
  hash = Resque::Plugins::Status::Hash.get(@job_uuid)
  hash.merge!({ 'progress' => progress_hash })
  Resque::Plugins::Status::Hash.set(@job_uuid, hash.status, hash)
end
success?() click to toggle source

**********************************************************************

JOB PROCESS STATUS

This wraps the process status info that comes from resque-status into
a more simpler state that notes whether the job is in progress,
completed successfully, or crashed.

The resque-status gem has 5 statuses:
  queued working completed failed killed
# File lib/job_state/base.rb, line 100
def success?
  status_hash.completed?
end
to_param() click to toggle source

to hook up easily with Rails’ resource-oriented architecture

# File lib/job_state/base.rb, line 117
def to_param
  @job_uuid
end
uuid() click to toggle source

the uuid from resque-status

# File lib/job_state/base.rb, line 122
def uuid
  @job_uuid
end
working?() click to toggle source
# File lib/job_state/base.rb, line 109
def working?
  !success? && !error?
end

Private Instance Methods

status_hash() click to toggle source
# File lib/job_state/base.rb, line 128
def status_hash
  # get a fresh version of the job status
  Resque::Plugins::Status::Hash.get(@job_uuid)
end