Class: StalkClimber::Job
- Inherits:
-
Object
- Object
- StalkClimber::Job
- Defined in:
- lib/stalk_climber/job.rb
Constant Summary collapse
- STATS_ATTRIBUTES =
%w[age buries delay kicks pri releases reserves state time-left timeouts ttr tube]
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Instance Method Summary collapse
-
#body ⇒ Object
Returns or fetches the body of the job obtained via the peek command.
-
#connection ⇒ Object
Returns the connection provided by the job data given to the initialize method.
-
#delete ⇒ Object
Deletes the job from beanstalk.
-
#exists? ⇒ Boolean
Determines if a job exists by retrieving stats for the job.
-
#initialize(job_data) ⇒ Job
constructor
Initializes a Job instance using
job_data
which should be the Beaneater response to either a put, peek, or stats-job command. -
#stats(force_refresh = false) ⇒ Object
Returns or retrieves stats for the job.
Constructor Details
#initialize(job_data) ⇒ Job
Initializes a Job instance using job_data
which should be the Beaneater response to either a put, peek, or stats-job command. Other Beaneater responses are not supported.
No single beanstalk command provides all the data an instance might need, so as more information is required, additional calls are made to beanstalk. For example, accessing both a job’s tube and its body requires both a peek and stats-job call.
Put provides only the ID of the job and as such yields the least informed instance. Both a peek and stats-job call may be required to retrieve anything but the ID of the instance
Peek provides the ID and body of the job. A stats-job call may be required to access anything but the ID or body of the job.
Stats-job provides the most information about the job, but lacks the crtical component of the job body. As such, a peek call would be required to access the body of the job.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/stalk_climber/job.rb', line 74 def initialize(job_data) case job_data[:status] when 'INSERTED' # put @id = job_data[:id].to_i @body = @stats = nil when 'FOUND' # peek @id = job_data[:id].to_i @body = job_data[:body] @stats = nil when 'OK' # stats-job @body = nil @stats = job_data.delete(:body) @id = @stats.delete('id').to_i else raise RuntimeError, "Unexpected job status: #{job_data[:status]}" end @status = job_data[:status] @connection = job_data[:connection] end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
7 8 9 |
# File 'lib/stalk_climber/job.rb', line 7 def id @id end |
Instance Method Details
#body ⇒ Object
Returns or fetches the body of the job obtained via the peek command
17 18 19 |
# File 'lib/stalk_climber/job.rb', line 17 def body return @body ||= connection.transmit("peek #{id}")[:body] end |
#connection ⇒ Object
Returns the connection provided by the job data given to the initialize method
23 24 25 |
# File 'lib/stalk_climber/job.rb', line 23 def connection return @connection end |
#delete ⇒ Object
Deletes the job from beanstalk. If the job is not found it is assumed that it has already been otherwise deleted.
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/stalk_climber/job.rb', line 30 def delete return true if @status == 'DELETED' begin @connection.transmit("delete #{id}") rescue Beaneater::NotFoundError end @status = 'DELETED' @stats = nil @body = nil return true end |
#exists? ⇒ Boolean
Determines if a job exists by retrieving stats for the job. If Beaneater can’t find the jobm then it does not exist and false is returned. The stats command is used because it will return a response of a near constant size, whereas, depending on the job, the peek command could return a much larger response. Rather than waste the trip to the server, stats are updated each time the method is called.
48 49 50 51 52 53 54 55 56 |
# File 'lib/stalk_climber/job.rb', line 48 def exists? return false if @status == 'DELETED' begin stats(:force_refresh) return true rescue Beaneater::NotFoundError return false end end |
#stats(force_refresh = false) ⇒ Object
Returns or retrieves stats for the job. Optionally, a retrieve may be forced by passing a non-false value for force_refresh
97 98 99 100 101 102 |
# File 'lib/stalk_climber/job.rb', line 97 def stats(force_refresh = false) return @stats unless @stats.nil? || force_refresh @stats = connection.transmit("stats-job #{id}")[:body] @stats.delete('id') return @stats end |