Method: Main::Daemon#cmd_stop

Defined in:
lib/main/daemon.rb

#cmd_stopObject



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
213
214
215
216
217
218
219
220
221
# File 'lib/main/daemon.rb', line 171

def cmd_stop
  pid = Integer(IO.read(@pid_file)) rescue nil

  if pid
    alive = true

    %w( QUIT TERM ).each do |signal|
      begin
        Process.kill(signal, pid)
      rescue Errno::ESRCH
        nil
      end

      42.times do
        begin
          Process.kill(0, pid)
          sleep(rand)
        rescue Errno::ESRCH
          alive = false
          puts(pid)
          exit(0)
        end
      end
    end

    if alive
      begin
        Process.kill(-9, pid)
        sleep(rand)
      rescue Errno::ESRCH
        nil
      end

      begin
        Process.kill(0, pid)
      rescue Errno::ESRCH
        puts(pid)
        exit(0)
      end
    end
  end
  
  exit(1)
ensure
  unless alive?
    begin
      FileUtils.rm_f(@pid_file) rescue nil
    rescue Object
    end
  end
end