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.

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
# File 'lib/process/daemon/controller.rb', line 31

def initialize(daemon, options = {})
  @daemon = daemon
  
  @output = options[:output] || $stdout
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`.



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

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

#spawnObject

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



60
61
62
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
# File 'lib/process/daemon/controller.rb', line 60

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.run
    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.



96
97
98
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
# File 'lib/process/daemon/controller.rb', line 96

def start
  @output.puts Rainbow("Starting daemon...").blue

  case ProcessFile.status(@daemon)
  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



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/process/daemon/controller.rb', line 131

def status
  daemon_state = ProcessFile.status(@daemon)
  
  case daemon_state
  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
  
  return daemon_state
end

#stopObject

Stops the daemon process.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
# File 'lib/process/daemon/controller.rb', line 155

def stop
  @output.puts Rainbow("Stopping 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

  # Interrupt the process group:
  pgid = -Process.getpgid(pid)
  Process.kill("INT", pgid)
  sleep 0.1

  sleep 1 if ProcessFile.running(@daemon)

  # Kill/Term loop - if the @daemon didn't die easily, shoot
  # it a few more times.
  attempts = 5
  while ProcessFile.running(@daemon) and attempts > 0
    sig = (attempts >= 2) ? "KILL" : "TERM"

    @output.puts Rainbow("Sending #{sig} to process group #{pgid}...").red
    Process.kill(sig, pgid)

    attempts -= 1
    sleep 1
  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
  end

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