Class: Threatstack::Jobs::JobQueue
- Inherits:
-
Object
- Object
- Threatstack::Jobs::JobQueue
- Defined in:
- lib/jobs/job_queue.rb
Instance Attribute Summary collapse
-
#job_queue ⇒ Object
readonly
Returns the value of attribute job_queue.
-
#job_thread ⇒ Object
readonly
Returns the value of attribute job_thread.
-
#stopped ⇒ Object
readonly
Returns the value of attribute stopped.
-
#working ⇒ Object
readonly
Returns the value of attribute working.
Instance Method Summary collapse
- #add_job(*args, &block) ⇒ Object
-
#initialize ⇒ JobQueue
constructor
A new instance of JobQueue.
- #stop ⇒ Object
Constructor Details
#initialize ⇒ JobQueue
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/jobs/job_queue.rb', line 12 def initialize @stopped = false @working = false @job_queue = Queue.new @job_thread = Thread.new do # Fetch an item from the worker queue, or wait until one is available while (job = @job_queue.pop) if @stopped # clear the queue @job_queue.clear break end @working = true job[:action].call(*job[:args]) @working = false if @job_queue.empty? end @working = false @stopped = true Thread.stop end end |
Instance Attribute Details
#job_queue ⇒ Object (readonly)
Returns the value of attribute job_queue.
7 8 9 |
# File 'lib/jobs/job_queue.rb', line 7 def job_queue @job_queue end |
#job_thread ⇒ Object (readonly)
Returns the value of attribute job_thread.
8 9 10 |
# File 'lib/jobs/job_queue.rb', line 8 def job_thread @job_thread end |
#stopped ⇒ Object (readonly)
Returns the value of attribute stopped.
9 10 11 |
# File 'lib/jobs/job_queue.rb', line 9 def stopped @stopped end |
#working ⇒ Object (readonly)
Returns the value of attribute working.
10 11 12 |
# File 'lib/jobs/job_queue.rb', line 10 def working @working end |
Instance Method Details
#add_job(*args, &block) ⇒ Object
34 35 36 37 38 |
# File 'lib/jobs/job_queue.rb', line 34 def add_job(*args, &block) raise 'Invalid proc provided' unless block_given? @job_queue.push(:action => block, :args => args) end |
#stop ⇒ Object
40 41 42 43 44 |
# File 'lib/jobs/job_queue.rb', line 40 def stop @stopped = true # push empty job to stop the while loop add_job(nil) {} end |