Class: JobState::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/job_state/base.rb

Class Method Summary collapse

Instance Method Summary collapse

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

.allObject

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.options && s.options['job_kind'] == kind}
  statuses.map!{|s| self.new(s.uuid)}
  statuses.reject!{|s| filter_out(s)}
  statuses
end

.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.



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

Returns:

  • (Boolean)


104
105
106
107
# File 'lib/job_state/base.rb', line 104

def error?
  h = status_hash
  h.failed? || h.killed?
end

#full_infoObject

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_paramsObject

********************************************************************** 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.options || {})
end

#progress_metricsObject

********************************************************************** 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

Returns:

  • (Boolean)


100
101
102
# File 'lib/job_state/base.rb', line 100

def success?
  status_hash.completed?
end

#to_paramObject

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

#uuidObject

the uuid from resque-status



122
123
124
# File 'lib/job_state/base.rb', line 122

def uuid
  @job_uuid
end

#working?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/job_state/base.rb', line 109

def working?
  !success? && !error?
end