Class: Hive::Worker

Inherits:
Object
  • Object
show all
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

Scheduler

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Worker

Returns a new instance of Worker.



28
29
30
# File 'lib/hive/worker.rb', line 28

def initialize( options )
  @options = options
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



26
27
28
# File 'lib/hive/worker.rb', line 26

def logger
  @logger
end

#optionsObject

Returns the value of attribute options.



26
27
28
# File 'lib/hive/worker.rb', line 26

def options
  @options
end

Class Method Details

.instanceObject

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( options = {} )
  @instance ||= new( options )
  @instance.start
end

Instance Method Details

#daemonizeObject



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

#restartObject



55
# File 'lib/hive/worker.rb', line 55

def restart ; stop ; start ; end

#set_trapsObject



75
76
77
78
# File 'lib/hive/worker.rb', line 75

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

#startObject

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 options[ :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 options[: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_consoleObject



80
81
82
# File 'lib/hive/worker.rb', line 80

def start_console
  # TODO: add live console support
end

#start_debuggerObject



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_drbObject



84
85
86
# File 'lib/hive/worker.rb', line 84

def start_drb
  # TODO: add DRb support
end

#start_loggerObject



94
95
96
97
98
99
100
101
# File 'lib/hive/worker.rb', line 94

def start_logger
  if options[ :logger ]
    @logger = options[ :logger ]
  else
    require 'logger'
    @logger = Logger.new( 'log.out' )
  end
end

#stopObject



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