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



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_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

#workingObject (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

#stopObject



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