Class: Opee::Queue

Inherits:
Actor
  • Object
show all
Defined in:
lib/opee/queue.rb

Overview

Implements a queue Actor that will distribute jobs to Actors that volunteer to complete those jobs. The primary use is to distribute work or jobs across multiple workers.

Direct Known Subclasses

AskQueue, WorkQueue

Constant Summary

Constant Summary

Constants inherited from Actor

Actor::CLOSING, Actor::RUNNING, Actor::STEP, Actor::STOPPED

Instance Attribute Summary

Attributes inherited from Actor

#name, #state

Instance Method Summary (collapse)

Methods inherited from Actor

#ask_timeout, #backed_up, #close, #max_queue_count, #method_missing, #on_idle, #priority_ask, #proc_count, #start, #state_string, #step, #stop, #timeout_ask, #wakeup

Constructor Details

- (Queue) initialize(options = {})

Returns a new instance of Queue



8
9
10
11
12
13
14
15
# File 'lib/opee/queue.rb', line 8

def initialize(options={})
  @workers = []
  @work_queue = []
  @max_job_count = 0
  @job_timeout = 3.0
  @add_thread = nil
  super(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Opee::Actor

Instance Method Details

- (Object) ask(op, *args)

Verifies that additional jobs can be added to the work queue before allowing an #add() to be called.

See Also:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/opee/queue.rb', line 46

def ask(op, *args)
  if :add == op && 0 < @max_job_count && (@work_queue.size() + @queue.size()) >= @max_job_count
    unless 0.0 >= @job_timeout
      @add_thread = Thread.current
      give_up_at = Time.now + @job_timeout
      until Time.now > give_up_at || (@work_queue.size() + @queue.size()) < @max_job_count
        sleep(@job_timeout)
      end
      @add_thread = nil
    end
    raise BusyError.new() unless @work_queue.size() < @max_job_count
  end
  super
end

- (true|false) busy?

Returns the true if any requests are queued, a request is being processed, or if there are jobs waiting on the work request queue.

Returns:

  • (true|false)

    true if busy, false otherwise



39
40
41
# File 'lib/opee/queue.rb', line 39

def busy?
  !@work_queue.empty? || super
end

- (Fixnum) queue_count

Returns the number of requests on all three request queues, the normal, priority, and idle queues as well as the work queue.

Returns:

  • (Fixnum)

    number of queued requests



32
33
34
# File 'lib/opee/queue.rb', line 32

def queue_count()
  @work_queue.size + super()
end

- (Fixnum) work_queue_size

Returns the number of jobs currently on the work queue.

Returns:

  • (Fixnum)

    number of waiting jobs



19
20
21
# File 'lib/opee/queue.rb', line 19

def work_queue_size()
  @work_queue.size
end

- (Fixnum) worker_count

Returns the number of worker Actors waiting to process jobs.

Returns:

  • (Fixnum)

    number of waiting workers



25
26
27
# File 'lib/opee/queue.rb', line 25

def worker_count()
  @workers.size
end