Class: Process::Daemon::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/process/daemon/controller.rb

Overview

This module contains functionality related to starting and stopping the @daemon, and code for processing command line input.

Constant Summary collapse

STOP_PERIOD =

How long to wait between checking the daemon process when shutting down:

0.1
STOP_ATTEMPTS =

The number of attempts to stop the daemon using SIGTERM. On the last attempt, SIGKILL is used.

5
STOP_WAIT_FACTOR =

The factor which controls how long we sleep between attempts to kill the process. Only applies to processes which don’t stop immediately.

3.0

Instance Method Summary collapse

Constructor Details

#initialize(daemon, options = {}) ⇒ Controller

options` specifies where to write textual output describing what is going on.



31
32
33
34
35
36
37
38
# File 'lib/process/daemon/controller.rb', line 31

def initialize(daemon, options = {})
  @daemon = daemon
  
  @output = options[:output] || $stdout
  
  # How long to wait until sending SIGTERM and eventually SIGKILL to the daemon process group when asking it to stop:
  @stop_timeout = options[:stop_timeout] || 10.0
end

Instance Method Details

#daemonize(argv = ARGV) ⇒ Object

This function is called from the daemon executable. It processes ARGV and checks whether the user is asking for ‘start`, `stop`, `restart`, `status`.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/process/daemon/controller.rb', line 41

def daemonize(argv = ARGV)
  case (argv.shift || :default).to_sym
  when :start
    start
    show_status
  when :stop
    stop
    show_status
    ProcessFile.cleanup(@daemon)
  when :restart
    stop
    ProcessFile.cleanup(@daemon)
    start
    show_status
  when :status
    show_status
  else
    @output.puts Rainbow("Invalid command. Please specify start, restart, stop or status.").red
  end
end

#pidObject

The pid of the daemon if it is available. The pid may be invalid if the daemon has crashed.



158
159
160
# File 'lib/process/daemon/controller.rb', line 158

def pid
  ProcessFile.recall(@daemon)
end

#show_statusObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/process/daemon/controller.rb', line 138

def show_status
  case self.status
  when :running
    @output.puts Rainbow("Daemon status: running pid=#{ProcessFile.recall(@daemon)}").green
  when :unknown
    if @daemon.crashed?
      @output.puts Rainbow("Daemon status: crashed").red

      @output.flush
      @output.puts Rainbow("Dumping daemon crash log:").red
      @daemon.tail_log(@output)
    else
      @output.puts Rainbow("Daemon status: unknown").red
    end
  when :stopped
    @output.puts Rainbow("Daemon status: stopped").blue
  end
end

#spawnObject

Fork a child process, detatch it and run the daemon code.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/process/daemon/controller.rb', line 63

def spawn
  @daemon.prefork
  @daemon.mark_log

  fork do
    Process.setsid
    exit if fork

    ProcessFile.store(@daemon, Process.pid)

    File.umask 0000
    Dir.chdir @daemon.working_directory

    $stdin.reopen '/dev/null'
    $stdout.reopen @daemon.log_file_path, 'a'
    $stdout.sync = true
  
    $stderr.reopen $stdout
    $stderr.sync = true

    begin
      @daemon.spawn
    rescue Exception => error
      $stderr.puts "=== Daemon Exception Backtrace @ #{Time.now.to_s} ==="
      $stderr.puts "#{error.class}: #{error.message}"
      $!.backtrace.each { |at| $stderr.puts at }
      $stderr.puts "=== Daemon Crashed ==="
      $stderr.flush
    ensure
      $stderr.puts "=== Daemon Stopping @ #{Time.now.to_s} ==="
      $stderr.flush
    end
  end
end

#startObject

This function starts the daemon process in the background.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/process/daemon/controller.rb', line 99

def start
  @output.puts Rainbow("Starting #{@daemon.name} daemon...").blue

  case self.status
  when :running
    @output.puts Rainbow("Daemon already running!").blue
    return
  when :stopped
    # We are good to go...
  else
    @output.puts Rainbow("Daemon in unknown state! Will clear previous state and continue.").red
    ProcessFile.clear(@daemon)
  end

  spawn

  sleep 0.1
  timer = TIMEOUT
  pid = ProcessFile.recall(@daemon)

  while pid == nil and timer > 0
    # Wait a moment for the forking to finish...
    @output.puts Rainbow("Waiting for daemon to start (#{timer}/#{TIMEOUT})").blue
    sleep 1

    # If the @daemon has crashed, it is never going to start...
    break if @daemon.crashed?

    pid = ProcessFile.recall(@daemon)

    timer -= 1
  end
end

#statusObject

Prints out the status of the daemon



134
135
136
# File 'lib/process/daemon/controller.rb', line 134

def status
  ProcessFile.status(@daemon)
end

#stopObject

Stops the daemon process. This function initially sends SIGINT. It waits STOP_PERIOD and checks if the daemon is still running. If it is, it sends SIGTERM, and then waits a bit longer. It tries STOP_ATTEMPTS times until it basically assumes the daemon is stuck and sends SIGKILL.



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/process/daemon/controller.rb', line 172

def stop
  @output.puts Rainbow("Stopping #{@daemon.name} daemon...").blue

  # Check if the pid file exists...
  unless File.file?(@daemon.process_file_path)
    @output.puts Rainbow("Pid file not found. Is the daemon running?").red
    return
  end

  pid = ProcessFile.recall(@daemon)

  # Check if the @daemon is already stopped...
  unless ProcessFile.running(@daemon)
    @output.puts Rainbow("Pid #{pid} is not running. Has daemon crashed?").red
    @daemon.tail_log($stderr)
    return
  end

  pgid = -Process.getpgid(pid)
  
  # Stop by interrupt sends a single interrupt and waits for the process to terminate:
  unless stop_by_interrupt(pgid)
    # If the process is still running, we try sending SIGTERM followed by SIGKILL:
    @output.puts Rainbow("** Daemon did not stop gracefully after #{@stop_timeout}s **").red
    
    stop_by_terminate_or_kill(pgid)
  end

  # If after doing our best the @daemon is still running (pretty odd)...
  if ProcessFile.running(@daemon)
    @output.puts Rainbow("Daemon appears to be still running!").red
    return
  else
    @output.puts Rainbow("Daemon has left the building.").green
  end

  # Otherwise the @daemon has been stopped.
  ProcessFile.clear(@daemon)
end