Class: Waves::Worker

Inherits:
Runtime show all
Defined in:
lib/runtime/worker.rb

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

.instanceObject

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

#daemonizeObject



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 # return nil for child process, just like fork does
end

#restartObject



39
# File 'lib/runtime/worker.rb', line 39

def restart ; stop ; start ; end

#set_trapsObject



48
49
50
51
# File 'lib/runtime/worker.rb', line 48

def set_traps
  safe_trap( 'HUP' ) { restart }
  safe_trap( 'TERM','INT' ) { stop }
end

#startObject

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
  # from here on in, we're in the daemon
  start_logger ; Waves::Logger.info "#{self.class} starting ..."
  start_debugger if debug? unless Kernel.engine == 'jruby'
  # various ways to talk to a worker
  set_traps ; start_console ; start_drb
  start_tasks.join
end

#start_consoleObject



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_debuggerObject



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_loggerObject



53
54
55
56
# File 'lib/runtime/worker.rb', line 53

def start_logger
  Waves::Logger.start
  Waves::Logger.info "Logger started."
end

#stopObject



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