Class: Hive::Worker
- Inherits:
-
Object
- Object
- Hive::Worker
- Defined in:
- lib/hive/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
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
-
.instance ⇒ Object
make this the one-and-only.
- .run(options = {}) ⇒ Object
Instance Method Summary collapse
- #daemonize ⇒ Object
-
#initialize(options) ⇒ Worker
constructor
A new instance of Worker.
- #restart ⇒ Object
- #set_traps ⇒ Object
-
#start ⇒ Object
returns the PID of the new process.
- #start_console ⇒ Object
- #start_debugger ⇒ Object
- #start_drb ⇒ Object
- #start_logger ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(options) ⇒ Worker
Returns a new instance of Worker.
28 29 30 |
# File 'lib/hive/worker.rb', line 28 def initialize( ) @options = end |
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
26 27 28 |
# File 'lib/hive/worker.rb', line 26 def logger @logger end |
#options ⇒ Object
Returns the value of attribute options.
26 27 28 |
# File 'lib/hive/worker.rb', line 26 def @options end |
Class Method Details
.instance ⇒ Object
make this the one-and-only
22 |
# File 'lib/hive/worker.rb', line 22 def self.instance ; @instance ; end |
.run(options = {}) ⇒ Object
16 17 18 19 |
# File 'lib/hive/worker.rb', line 16 def self.run( = {} ) @instance ||= new( ) @instance.start end |
Instance Method Details
#daemonize ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/hive/worker.rb', line 57 def daemonize pwd = Dir.pwd ; pid = fork ; return pid if pid ; Dir.chdir( pwd ) # Make sure all file descriptors are closed ObjectSpace.each_object( IO ) do | io | unless [ STDIN, STDOUT, STDERR ].include?(io) begin unless io.closed? io.close end rescue ::Exception end end end 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 |
#restart ⇒ Object
55 |
# File 'lib/hive/worker.rb', line 55 def restart ; stop ; start ; end |
#set_traps ⇒ Object
75 76 77 78 |
# File 'lib/hive/worker.rb', line 75 def set_traps safe_trap( 'HUP' ) { restart } safe_trap( 'TERM','INT' ) { stop } end |
#start ⇒ Object
returns the PID of the new process
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/hive/worker.rb', line 33 def start pid = daemonize if [ :daemon ] puts "#{self.class.name} process #{pid} started ..." if pid return pid if pid begin # from here on in, we're in the daemon start_logger ; 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 rescue Exception => e logger.error e.to_s rescue nil end end |
#start_console ⇒ Object
80 81 82 |
# File 'lib/hive/worker.rb', line 80 def start_console # TODO: add live console support end |
#start_debugger ⇒ Object
88 89 90 91 92 |
# File 'lib/hive/worker.rb', line 88 def start_debugger require 'ruby-debug' ; Debugger.start Debugger.settings[:autoeval] = true if Debugger.respond_to?(:settings) logger.info "ruby-debug enabled" end |
#start_drb ⇒ Object
84 85 86 |
# File 'lib/hive/worker.rb', line 84 def start_drb # TODO: add DRb support end |
#start_logger ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/hive/worker.rb', line 94 def start_logger if [ :logger ] @logger = [ :logger ] else require 'logger' @logger = Logger.new( 'log.out' ) end end |
#stop ⇒ Object
49 50 51 52 53 |
# File 'lib/hive/worker.rb', line 49 def stop logger.info "#{self.class} shutting down ..." @console.stop if @console stop_tasks end |