Class: Daemonizer::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/daemonizer/worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, pm, worker_id, &blk) ⇒ Worker

Returns a new instance of Worker.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
# File 'lib/daemonizer/worker.rb', line 7

def initialize(name, pm, worker_id, &blk)
  raise ArgumentError, "Need a worker block!" unless block_given?

  @name = name
  @pm = pm
  @worker_block = blk
  @worker_id = worker_id
  @process_name = "#{@name} worker: instance #{@worker_id}"
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/daemonizer/worker.rb', line 3

def name
  @name
end

#pidObject (readonly)

Returns the value of attribute pid.



5
6
7
# File 'lib/daemonizer/worker.rb', line 5

def pid
  @pid
end

#process_nameObject (readonly)

Returns the value of attribute process_name.



4
5
6
# File 'lib/daemonizer/worker.rb', line 4

def process_name
  @process_name
end

Instance Method Details

#runObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/daemonizer/worker.rb', line 21

def run
  return if shutdown?
  Daemonizer.logger.info "Forking..."
  @pid = Kernel.fork do
    Dir.chdir '/'
    File.umask 0000

    STDIN.reopen '/dev/null'
    STDOUT.reopen '/dev/null', 'a'
    STDERR.reopen STDOUT
    
    @pid = Process.pid
    Daemonizer.reopen_log_file
    Daemonizer.logger_context = "#{@pid}/#{@worker_id}"
    
    Daemonizer.logger.info "Log file reopened after fork"
    normal_exit = false
    begin
      $0 = "#{@process_name}\0"
      @worker_block.call(@worker_id)
      normal_exit = true
      exit(0)
    rescue Exception => e
      message = SystemExit === e ? "exit(#{e.status})" : e.to_s
      if SystemExit === e and e.success?
        if normal_exit
          Daemonizer.logger.info("Worker finished: normal return")
        else
          Daemonizer.logger.error("Worker exited: #{message} at #{e.backtrace.first}")
        end
      else
        Daemonizer.logger.error("Worker exited with error: #{message}\n  #{e.backtrace.join("\n  ")}")
      end
      Daemonizer.logger.debug("Terminating #{@name} worker: #{@pid}")
    end
  end
rescue Exception => e
  Daemonizer.logger.error("Exception from worker: #{e} at #{e.backtrace.first}")
end

#running?(verbose = false) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/daemonizer/worker.rb', line 61

def running?(verbose = false)
  return false unless @pid
  begin
    Process.waitpid(@pid, Process::WNOHANG)
    res = Process.kill(0, @pid)
    Daemonizer.logger.debug("KILL(#{@pid}) = #{res}") if verbose
    return true
  rescue Errno::ESRCH, Errno::ECHILD, Errno::EPERM => e
    Daemonizer.logger.error("Exception from kill: #{e} at #{e.backtrace.first}") if verbose
    return false
  end
end

#shutdown?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/daemonizer/worker.rb', line 17

def shutdown?
  @pm.shutdown?
end

#stop(force = false) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/daemonizer/worker.rb', line 74

def stop(force = false)
  begin
    sig = force ? 'SIGKILL' : 'SIGTERM'
    Daemonizer.logger.debug("Sending #{sig} to ##{@pid}")
    Process.kill(sig, @pid)
  rescue Errno::ESRCH, Errno::ECHILD, Errno::EPERM=> e
    Daemonizer.logger.error("Exception from kill: #{e} at #{e.backtrace.first}")
  end
end