Module: Proco::MT::Threaded

Includes:
Logger, Base
Included in:
Dispatcher, Worker
Defined in:
lib/proco/mt/threaded.rb

Instance Method Summary collapse

Methods included from Base

#broadcast, #do_when, #signal, #synchronize, #try_when, #wait_until

Methods included from Logger

#logger

Instance Method Details

#exitObject



17
18
19
20
21
22
# File 'lib/proco/mt/threaded.rb', line 17

def exit
  broadcast do
    @running = false
  end
  @thread.join
end

#initializeObject



8
9
10
11
# File 'lib/proco/mt/threaded.rb', line 8

def initialize
  super
  @running = false
end

#killObject



24
25
26
27
# File 'lib/proco/mt/threaded.rb', line 24

def kill
  @running = false
  Thread.kill @thread if @thread
end

#running?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/proco/mt/threaded.rb', line 13

def running?
  @running
end

#spawn(&block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/proco/mt/threaded.rb', line 29

def spawn &block
  @thread = Thread.new do
    debug "#{Thread.current} started (#{self})"
    broadcast do
      @running = true
    end

    begin
      block.call
    rescue Exception => e
      error "[#{Thread.current}] #{e}"
      raise
    ensure
      debug "#{Thread.current} exited (#{self})"
    end
  end
  wait_until { running? }

  @thread
end