Class: OsCtld::Eventd::Worker

Inherits:
Object
  • Object
show all
Includes:
Lockable
Defined in:
lib/osctld/eventd/worker.rb

Instance Method Summary collapse

Methods included from Lockable

#exclusively, included, #inclusively, #init_lock, #lock, #unlock

Constructor Details

#initializeWorker

Returns a new instance of Worker.



5
6
7
8
9
# File 'lib/osctld/eventd/worker.rb', line 5

def initialize
  init_lock
  @queue = Queue.new
  @subscribers = []
end

Instance Method Details

#report(event) ⇒ Object

Parameters:



44
45
46
# File 'lib/osctld/eventd/worker.rb', line 44

def report(event)
  @queue << event
end

#run_workerObject (protected)



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/osctld/eventd/worker.rb', line 50

def run_worker
  loop do
    event = @queue.pop
    break if event == :stop

    @subscribers.each do |queue|
      queue << event
    end
  end

  @subscribers.clear
end

#sizeInteger

Returns:

  • (Integer)


39
40
41
# File 'lib/osctld/eventd/worker.rb', line 39

def size
  inclusively { @subscribers.length }
end

#startObject



11
12
13
14
# File 'lib/osctld/eventd/worker.rb', line 11

def start
  @thread = Thread.new { run_worker }
  nil
end

#stopObject



16
17
18
19
20
21
22
23
24
# File 'lib/osctld/eventd/worker.rb', line 16

def stop
  if @thread
    @queue << :stop
    @thread.join
    @thread = nil
  end

  nil
end

#subscribe(queue) ⇒ Object

Parameters:

  • queue (OsCtl::Lib::Queue)


27
28
29
30
# File 'lib/osctld/eventd/worker.rb', line 27

def subscribe(queue)
  exclusively { @subscribers << queue }
  nil
end

#unsubscribe(queue) ⇒ Object

Parameters:

  • queue (OsCtl::Lib::Queue)


33
34
35
36
# File 'lib/osctld/eventd/worker.rb', line 33

def unsubscribe(queue)
  exclusively { @subscribers.delete(queue) }
  nil
end