Class: SuckerPunch::Queue

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

Defined Under Namespace

Classes: MaxWorkersExceeded, NotEnoughWorkers

Constant Summary collapse

DEFAULT_OPTIONS =
{ workers: 2 }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Queue


17
18
19
20
21
# File 'lib/sucker_punch/queue.rb', line 17

def initialize(klass)
  @klass = klass
  @pool = nil
  @mutex = Mutex.new
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass


5
6
7
# File 'lib/sucker_punch/queue.rb', line 5

def klass
  @klass
end

#poolObject

Returns the value of attribute pool


6
7
8
# File 'lib/sucker_punch/queue.rb', line 6

def pool
  @pool
end

Class Method Details

.find(klass) ⇒ Object


12
13
14
15
# File 'lib/sucker_punch/queue.rb', line 12

def self.find(klass)
  queue = self.new(klass)
  Celluloid::Actor[queue.name]
end

Instance Method Details

#nameObject


41
42
43
# File 'lib/sucker_punch/queue.rb', line 41

def name
  klass.to_s.underscore.to_sym
end

#register(num_workers = DEFAULT_OPTIONS[:workers]) ⇒ Object

Raises:


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sucker_punch/queue.rb', line 23

def register(num_workers = DEFAULT_OPTIONS[:workers])
  num_workers ||= DEFAULT_OPTIONS[:workers]
  raise MaxWorkersExceeded if num_workers > 100
  raise NotEnoughWorkers if num_workers < 1

  @mutex.synchronize {
    unless registered?
      initialize_celluloid_pool(num_workers)
      register_celluloid_pool
    end
  }
  self.class.find(klass)
end

#registered?Boolean


37
38
39
# File 'lib/sucker_punch/queue.rb', line 37

def registered?
  Celluloid::Actor.registered.include?(name.to_sym)
end