Class: ForemanAP::Workqueue

Inherits:
Object
  • Object
show all
Defined in:
lib/foreman_vm/workqueue.rb

Overview

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

Instance Method Summary collapse

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

#clearObject

Remove all jobs from the queue



31
32
33
34
# File 'lib/foreman_vm/workqueue.rb', line 31

def clear
  @tube.clear
  true
end

#dequeueObject

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

#jobsObject

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_jobsObject

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

Returns:

  • (Boolean)


54
55
56
# File 'lib/foreman_vm/workqueue.rb', line 54

def worker?
  @tube.stats.current_watching > 0
end