Class: Waves::Worker
Overview
“Workers” are just dedicated processes. Managers, Servers, and Monitors are all examples of Workers. This class just encapsulates the common features across all Workers: daemonization, signal traps, console support, logging, only-ness, etc.
Direct Known Subclasses
Server
Instance Attribute Summary
Attributes inherited from Runtime
#options
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Runtime
#config, #debug?, #initialize, #log, #mode, #reload, #synchronize, #synchronize?
Constructor Details
This class inherits a constructor from Waves::Runtime
Class Method Details
make this the one-and-only
17
|
# File 'lib/runtime/worker.rb', line 17
def self.instance ; @instance ; end
|
.run(options) ⇒ Object
10
11
12
13
14
|
# File 'lib/runtime/worker.rb', line 10
def self.run( options )
@instance ||= new( options )
Kernel.load( options[:startup] || 'startup.rb' )
@instance.start
end
|
Instance Method Details
#daemonize ⇒ Object
41
42
43
44
45
46
|
# File 'lib/runtime/worker.rb', line 41
def daemonize
pwd = Dir.pwd ; pid = fork ; return pid if pid ; Dir.chdir( pwd )
File.umask 0000 ; STDIN.reopen( '/dev/null') ;
STDOUT.reopen( '/dev/null', 'a' ) ; STDERR.reopen( STDOUT )
nil
end
|
39
|
# File 'lib/runtime/worker.rb', line 39
def restart ; stop ; start ; end
|
#set_traps ⇒ Object
48
49
50
51
|
# File 'lib/runtime/worker.rb', line 48
def set_traps
safe_trap( 'HUP' ) { restart }
safe_trap( 'TERM','INT' ) { stop }
end
|
returns the PID of the new process
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/runtime/worker.rb', line 22
def start
pid = daemonize if options[ :daemon ]
return pid if pid
start_logger ; Waves::Logger.info "#{self.class} starting ..."
start_debugger if debug? unless Kernel.engine == 'jruby'
set_traps ; start_console ; start_drb
start_tasks.join
end
|
#start_console ⇒ Object
58
59
60
61
62
63
|
# File 'lib/runtime/worker.rb', line 58
def start_console
if config.console
@console = config.console ; @console.start
Waves::Logger.info "Console started on port #{config.console.port}"
end
end
|
#start_debugger ⇒ Object
65
66
67
68
69
|
# File 'lib/runtime/worker.rb', line 65
def start_debugger
require 'ruby-debug' ; Debugger.start
Debugger.settings[:autoeval] = true if Debugger.respond_to?(:settings)
Waves::Logger.info "ruby-debug enabled"
end
|
#start_logger ⇒ Object
53
54
55
56
|
# File 'lib/runtime/worker.rb', line 53
def start_logger
Waves::Logger.start
Waves::Logger.info "Logger started."
end
|
33
34
35
36
37
|
# File 'lib/runtime/worker.rb', line 33
def stop
Waves::Logger.info "#{self.class} shutting down ..."
@console.stop if @console
stop_tasks
end
|