Class: Pubby::SimpleAsync

Inherits:
Object
  • Object
show all
Defined in:
lib/pubby/simple_async.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pubby, thread_count = 1) ⇒ SimpleAsync

you probably want the pubby used here to be threadsafe if there’s more than one thread…



9
10
11
12
13
14
15
16
# File 'lib/pubby/simple_async.rb', line 9

def initialize(pubby, thread_count = 1)
  @pubby = pubby
  @thread_count = thread_count
  @queue = Queue.new
  @threads = []
  
  start_threads!
end

Instance Attribute Details

#pubbyObject (readonly)

Returns the value of attribute pubby.



3
4
5
# File 'lib/pubby/simple_async.rb', line 3

def pubby
  @pubby
end

#queueObject (readonly)

Returns the value of attribute queue.



5
6
7
# File 'lib/pubby/simple_async.rb', line 5

def queue
  @queue
end

#thread_countObject (readonly)

Returns the value of attribute thread_count.



4
5
6
# File 'lib/pubby/simple_async.rb', line 4

def thread_count
  @thread_count
end

#threadsObject (readonly)

Returns the value of attribute threads.



6
7
8
# File 'lib/pubby/simple_async.rb', line 6

def threads
  @threads
end

Instance Method Details

#kill!Object



30
31
32
# File 'lib/pubby/simple_async.rb', line 30

def kill!
  @threads.each { |t| t.kill; t.join }
end

#publish(channel, message) ⇒ Object



18
19
20
# File 'lib/pubby/simple_async.rb', line 18

def publish(channel, message)
  @queue << [channel, message]
end

#shutdown!Object



22
23
24
25
26
27
28
# File 'lib/pubby/simple_async.rb', line 22

def shutdown!
  until @queue.empty?
    sleep 0.05
  end
  
  kill!
end