Module: RExec::Daemon::Controller

Defined in:
lib/rexec/daemon/controller.rb

Overview

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

Class Method Summary collapse

Class Method Details

.daemonize(daemon) ⇒ Object

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rexec/daemon/controller.rb', line 34

def self.daemonize(daemon)
  case ARGV.shift
  when 'start'
    start(daemon)
    status(daemon)
  when 'stop'
    stop(daemon)
    status(daemon)
    ProcessFile.cleanup(daemon)
  when 'restart'
    stop(daemon)
    ProcessFile.cleanup(daemon)
    start(daemon)
    status(daemon)
  when 'status'
    status(daemon)
  else
    puts "Invalid command. Please specify start, restart, stop or status."
    exit
  end
end

.start(daemon) ⇒ Object

This function starts the supplied daemon



57
58
59
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
94
95
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rexec/daemon/controller.rb', line 57

def self.start(daemon)
  puts "Starting daemon...".color(:blue)

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

  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
      error = nil
      
      main = Thread.new do
        begin
          daemon.run
        rescue
          error = $!
        end
      end

      trap("INT") do
        begin
          daemon.shutdown
          main.exit
        rescue
          error = $!
        end
      end

      trap("TERM") do
        exit!
      end

      main.join

      raise error if error
    rescue
      $stderr.puts "=== Daemon Exception Backtrace @ #{Time.now.to_s} ==="
      $stderr.puts "#{$!.class}: #{$!.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

  puts "Waiting for daemon to start...".color(:blue)
  sleep 0.1
  timer = TIMEOUT
  pid = ProcessFile.recall(daemon)

  while pid == nil and timer > 0
    # Wait a moment for the forking to finish...
    puts "Waiting for daemon to start (#{timer}/#{TIMEOUT})".color(: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

.status(daemon) ⇒ Object

Prints out the status of the daemon



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/rexec/daemon/controller.rb', line 150

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

      $stdout.flush
      $stderr.puts "Dumping daemon crash log:".color(:red)
      daemon.tail_log($stderr)
    else
      puts "Daemon status: unknown".color(:red)
    end
  when :stopped
    puts "Daemon status: stopped".color(:blue)
  end
end

.stop(daemon) ⇒ Object

Stops the daemon process.



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
204
205
206
207
208
209
210
211
212
# File 'lib/rexec/daemon/controller.rb', line 170

def self.stop(daemon)
  puts "Stopping daemon...".color(:blue)

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

  pid = ProcessFile.recall(daemon)

  # Check if the daemon is already stopped...
  unless ProcessFile.running(daemon)
    puts "Pid #{pid} is not running. Has daemon crashed?".color(:red)
    return
  end

  pid = ProcessFile.recall(daemon)
  Process.kill("INT", pid)
  sleep 0.1

  # 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"

    puts "Sending #{sig} to pid #{pid}...".color(:red)
    Process.kill(sig, pid)

    sleep 1
    attempts -= 1
  end

  # If after doing our best the daemon is still running (pretty odd)...
  if ProcessFile.running(daemon)
    puts "Daemon appears to be still running!".color(:red)
    return
  end

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