Class: Threatstack::Jobs::JobQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/jobs/job_queue.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJobQueue

Returns a new instance of JobQueue.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/jobs/job_queue.rb', line 11

def initialize
  @stopped = 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)
      Thread.stop if @stopped
      job[:action].call(job[:args])
    end
    @stopped = true
    Thread.stop
  end
end

Instance Attribute Details

#job_queueObject (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_threadObject (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

#stoppedObject (readonly)

Returns the value of attribute stopped.



9
10
11
# File 'lib/jobs/job_queue.rb', line 9

def stopped
  @stopped
end

Instance Method Details

#add_job(args = nil, &block) ⇒ Object



25
26
27
28
29
# File 'lib/jobs/job_queue.rb', line 25

def add_job(args = nil, &block)
  raise 'Invalid proc provided' unless block_given?

  @job_queue.push(:action => block, :args => args)
end

#stopObject



31
32
33
34
35
# File 'lib/jobs/job_queue.rb', line 31

def stop
  @stopped = true
  # push nil to stop the while loop
  add_job nil
end