class JobState::Base
Public Class Methods
Source
# 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
Like find, you tend to call this on the subclass, not JobState::Base
itself.
Source
# File lib/job_state/base.rb, line 14 def self.find(job_uuid) self.new(job_uuid) end
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.
Source
# 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
Allows you to locate specific resque jobs based on the job parameters that were sent when the job was created.
Source
# File lib/job_state/base.rb, line 4 def initialize(uuid) @job_uuid = uuid # uuid from resque-status end
Private Class Methods
Source
# File lib/job_state/base.rb, line 143 def self.filter_out(csv_import_job_state) false end
Override this in your subclass to determine which jobs are available to the find and all methods.
Public Instance Methods
Source
# File lib/job_state/base.rb, line 104 def error? h = status_hash h.failed? || h.killed? end
Source
# 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
to be sent up to JavaScript land as JSON
Source
# File lib/job_state/base.rb, line 86 def get_job_param(name) job_params[name] end
Source
# File lib/job_state/base.rb, line 62 def get_progress_metric(name) progress_metrics[name.to_s] end
Source
# File lib/job_state/base.rb, line 82 def job_params HashWithIndifferentAccess.new(status_hash.options || {}) end
**********************************************************************
JOB PARAMS This is the info you originally passed to the Resque job when it was created.
Source
# File lib/job_state/base.rb, line 130 def kill Resque::Plugins::Status::Hash.kill(@job_uuid) end
**********************************************************************
KILLING This method kills the job
Source
# File lib/job_state/base.rb, line 58 def progress_metrics HashWithIndifferentAccess.new(status_hash['progress'] || {}) end
**********************************************************************
PROGRESS METRICS This is how you communicate progress status updates to your Rails app.
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
Source
# File lib/job_state/base.rb, line 100 def success? status_hash.completed? end
**********************************************************************
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
Source
# File lib/job_state/base.rb, line 117 def to_param @job_uuid end
to hook up easily with Rails’ resource-oriented architecture
Source
# File lib/job_state/base.rb, line 122 def uuid @job_uuid end
the uuid from resque-status
Private Instance Methods
Source
# File lib/job_state/base.rb, line 136 def status_hash # get a fresh version of the job status Resque::Plugins::Status::Hash.get(@job_uuid) end