class ForemanAP::Workqueue

Allow jobs to be placed on a workqueue and processed later.

Public Class Methods

new(tube_name = 'foreman-vm') click to toggle source
# 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

Public Instance Methods

clear() click to toggle source

Remove all jobs from the queue

# File lib/foreman_vm/workqueue.rb, line 31
def clear
  @tube.clear
  true
end
dequeue() click to toggle source

Remove a job from the queue, and return the body

# File lib/foreman_vm/workqueue.rb, line 23
def dequeue
  job = @tube.reserve
  result = JSON.parse(job.body)
  job.delete
  result
end
enqueue(item) click to toggle source

Add an item to the queue

# File lib/foreman_vm/workqueue.rb, line 18
def enqueue(item)
  @tube.put JSON.pretty_generate(item)
end
jobs() click to toggle source

Return a list of all jobs

(TODO: try to avoid leaking beanstalkd details)
# 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() { |parse| ... } click to toggle source

Process all jobs

# 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?() click to toggle source

Check if there is a worker waiting for jobs

# File lib/foreman_vm/workqueue.rb, line 54
def worker?
  @tube.stats.current_watching > 0
end