Class: ForemanAP::Workqueue
- Inherits:
-
Object
- Object
- ForemanAP::Workqueue
- Defined in:
- lib/foreman_vm/workqueue.rb
Overview
Allow jobs to be placed on a workqueue and processed later.
Instance Method Summary collapse
-
#clear ⇒ Object
Remove all jobs from the queue.
-
#dequeue ⇒ Object
Remove a job from the queue, and return the body.
-
#enqueue(item) ⇒ Object
Add an item to the queue.
-
#initialize(tube_name = 'foreman-vm') ⇒ Workqueue
constructor
A new instance of Workqueue.
-
#jobs ⇒ Object
Return a list of all jobs (TODO: try to avoid leaking beanstalkd details).
-
#process_all_jobs ⇒ Object
Process all jobs.
-
#worker? ⇒ Boolean
Check if there is a worker waiting for jobs.
Constructor Details
#initialize(tube_name = 'foreman-vm') ⇒ Workqueue
Returns a new instance of Workqueue.
11 12 13 14 15 |
# File 'lib/foreman_vm/workqueue.rb', line 11 def initialize(tube_name = 'foreman-vm') @tube_name = tube_name @beanstalk = Beaneater::Pool.new(['localhost:11300']) @tube = @beanstalk.tubes[tube_name] end |
Instance Method Details
#clear ⇒ Object
Remove all jobs from the queue
31 32 33 34 |
# File 'lib/foreman_vm/workqueue.rb', line 31 def clear @tube.clear true end |
#dequeue ⇒ Object
Remove a job from the queue, and return the body
23 24 25 26 27 28 |
# File 'lib/foreman_vm/workqueue.rb', line 23 def dequeue job = @tube.reserve result = JSON.parse(job.body) job.delete result end |
#enqueue(item) ⇒ Object
Add an item to the queue
18 19 20 |
# File 'lib/foreman_vm/workqueue.rb', line 18 def enqueue(item) @tube.put JSON.pretty_generate(item) end |
#jobs ⇒ Object
Return a list of all jobs
(TODO: try to avoid leaking beanstalkd details)
38 39 40 41 42 |
# File 'lib/foreman_vm/workqueue.rb', line 38 def jobs buf = "job stats:\n\n" buf += @tube.stats.inspect + "\n\n\n" buf end |
#process_all_jobs ⇒ Object
Process all jobs
45 46 47 48 49 50 51 |
# File 'lib/foreman_vm/workqueue.rb', line 45 def process_all_jobs @beanstalk.jobs.register(@tube_name) do |job| yield JSON.parse(job.body) end @beanstalk.jobs.process! end |
#worker? ⇒ Boolean
Check if there is a worker waiting for jobs
54 55 56 |
# File 'lib/foreman_vm/workqueue.rb', line 54 def worker? @tube.stats.current_watching > 0 end |