Class: JobState::Base
- Inherits:
-
Object
- Object
- JobState::Base
- Defined in:
- lib/job_state/base.rb
Class Method Summary collapse
-
.all ⇒ Object
Like find, you tend to call this on the subclass, not JobState::Base itself.
-
.find(job_uuid) ⇒ Object
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.
-
.find_all_by(job_params) ⇒ Object
Allows you to locate specific resque jobs based on the job parameters that were sent when the job was created.
Instance Method Summary collapse
- #error? ⇒ Boolean
-
#full_info ⇒ Object
to be sent up to JavaScript land as JSON.
- #get_job_param(name) ⇒ Object
- #get_progress_metric(name) ⇒ Object
-
#initialize(uuid) ⇒ Base
constructor
A new instance of Base.
-
#job_params ⇒ Object
********************************************************************** JOB PARAMS.
-
#progress_metrics ⇒ Object
********************************************************************** PROGRESS METRICS.
- #set_progress_metric(name, value) ⇒ Object
-
#success? ⇒ Boolean
********************************************************************** JOB PROCESS STATUS.
-
#to_param ⇒ Object
to hook up easily with Rails’ resource-oriented architecture.
-
#uuid ⇒ Object
the uuid from resque-status.
- #working? ⇒ Boolean
Constructor Details
#initialize(uuid) ⇒ Base
Returns a new instance of Base.
4 5 6 |
# File 'lib/job_state/base.rb', line 4 def initialize(uuid) @job_uuid = uuid # uuid from resque-status end |
Class Method Details
.all ⇒ Object
Like find, you tend to call this on the subclass, not JobState::Base itself.
20 21 22 23 24 25 26 |
# File 'lib/job_state/base.rb', line 20 def self.all statuses = Resque::Plugins::Status::Hash.statuses statuses.select!{|s| s. && s.['job_kind'] == kind} statuses.map!{|s| self.new(s.uuid)} statuses.reject!{|s| filter_out(s)} statuses end |
.find(job_uuid) ⇒ Object
14 15 16 |
# File 'lib/job_state/base.rb', line 14 def self.find(job_uuid) self.new(job_uuid) end |
.find_all_by(job_params) ⇒ Object
Allows you to locate specific resque jobs based on the job parameters that were sent when the job was created.
30 31 32 33 34 35 36 37 38 |
# 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 |
Instance Method Details
#error? ⇒ Boolean
104 105 106 107 |
# File 'lib/job_state/base.rb', line 104 def error? h = status_hash h.failed? || h.killed? end |
#full_info ⇒ Object
to be sent up to JavaScript land as JSON
41 42 43 44 45 46 47 48 49 50 51 |
# 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) ⇒ Object
86 87 88 |
# File 'lib/job_state/base.rb', line 86 def get_job_param(name) job_params[name] end |
#get_progress_metric(name) ⇒ Object
62 63 64 |
# File 'lib/job_state/base.rb', line 62 def get_progress_metric(name) progress_metrics[name.to_s] end |
#job_params ⇒ Object
********************************************************************** JOB PARAMS
This is the info you originally passed to the Resque job when it was created.
82 83 84 |
# File 'lib/job_state/base.rb', line 82 def job_params HashWithIndifferentAccess.new(status_hash. || {}) end |
#progress_metrics ⇒ Object
********************************************************************** PROGRESS METRICS
This is how you communicate progress status updates to your Rails app.
58 59 60 |
# File 'lib/job_state/base.rb', line 58 def progress_metrics HashWithIndifferentAccess.new(status_hash['progress'] || {}) end |
#set_progress_metric(name, value) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# 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? ⇒ Boolean
********************************************************************** 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
100 101 102 |
# File 'lib/job_state/base.rb', line 100 def success? status_hash.completed? end |
#to_param ⇒ Object
to hook up easily with Rails’ resource-oriented architecture
117 118 119 |
# File 'lib/job_state/base.rb', line 117 def to_param @job_uuid end |
#uuid ⇒ Object
the uuid from resque-status
122 123 124 |
# File 'lib/job_state/base.rb', line 122 def uuid @job_uuid end |
#working? ⇒ Boolean
109 110 111 |
# File 'lib/job_state/base.rb', line 109 def working? !success? && !error? end |