Class: Rack::App::Worker::Daemonizer

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

Constant Summary collapse

DEFAULT_KILL_SIGNAL =
'HUP'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(daemon_name) ⇒ Daemonizer

Returns a new instance of Daemonizer.



6
7
8
9
# File 'lib/rack/app/worker/daemonizer.rb', line 6

def initialize(daemon_name)
  @daemon_name = daemon_name.to_s
  @on_shutdown, @on_halt, @on_reload = proc {}, proc {}, proc {}
end

Instance Method Details

#bind(to_pid) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/rack/app/worker/daemonizer.rb', line 63

def bind(to_pid)
  Thread.new do
    sleep(1) while Rack::App::Worker::Utils.process_alive?(to_pid)

    at_shutdown
  end
end

#daemonizeObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rack/app/worker/daemonizer.rb', line 30

def daemonize
  case try_fork

    when NilClass #child
      subscribe_to_signals
      save_current_process_pid
      redirect

    else #parent
      Kernel.exit

  end
end

#has_running_process?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/rack/app/worker/daemonizer.rb', line 44

def has_running_process?
  pids.any? { |pid| Rack::App::Worker::Utils.process_alive?(pid) }
end

#idObject



11
12
13
# File 'lib/rack/app/worker/daemonizer.rb', line 11

def id
  @id ||= SecureRandom.uuid
end

#on_halt(&block) ⇒ Object



76
77
78
79
# File 'lib/rack/app/worker/daemonizer.rb', line 76

def on_halt(&block)
  raise('block not given!') unless block.is_a?(Proc)
  @on_halt = block
end

#on_reload(&block) ⇒ Object



81
82
83
84
# File 'lib/rack/app/worker/daemonizer.rb', line 81

def on_reload(&block)
  raise('block not given!') unless block.is_a?(Proc)
  @on_reload = block
end

#on_shutdown(&block) ⇒ Object



71
72
73
74
# File 'lib/rack/app/worker/daemonizer.rb', line 71

def on_shutdown(&block)
  raise('block not given!') unless block.is_a?(Proc)
  @on_shutdown = block
end

#process_title(new_title) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/rack/app/worker/daemonizer.rb', line 48

def process_title(new_title)
  if Process.respond_to?(:setproctitle)
    Process.setproctitle(new_title)
  else

    $0 = new_title
  end
end

#send_signal(signal, to_amount_of_worker = pids.length) ⇒ Object



57
58
59
60
61
# File 'lib/rack/app/worker/daemonizer.rb', line 57

def send_signal(signal, to_amount_of_worker=pids.length)
  pids.take(to_amount_of_worker).each do |pid|
    kill(signal, pid)
  end
end

#spawn(&block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rack/app/worker/daemonizer.rb', line 15

def spawn(&block)

  parent_pid = current_pid
  spawn_block = proc do
    subscribe_to_signals
    bind(parent_pid)
    save_current_process_pid
    redirect
    block.call(self)
  end

  try_fork(&spawn_block)

end

#subscribe_to_signalsObject



86
87
88
89
90
91
# File 'lib/rack/app/worker/daemonizer.rb', line 86

def subscribe_to_signals
  ::Signal.trap('INT'){ at_shutdown }
  ::Signal.trap('HUP'){ at_shutdown }
  ::Signal.trap('TERM'){ at_halt }
  ::Signal.trap('USR1'){ at_reload }
end