Class: DaemonKit::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/daemon_kit/application.rb

Overview

Class responsible for making the daemons run and keep them running.

Class Method Summary collapse

Class Method Details

.exec(file) ⇒ Object

Run the specified file as a daemon process.

Raises:

  • (DaemonNotFound)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/daemon_kit/application.rb', line 11

def exec( file )
  raise DaemonNotFound.new( file ) unless File.exist?( file )

  DaemonKit.configuration.daemon_name ||= File.basename( file )

  command, configs, args = Arguments.parse( ARGV )

  case command
  when :run
    parse_arguments( args )
    run( file )
  when :start
    parse_arguments( args )
    start( file )
  when :stop
    stop
  end
end

.exit!(code = 0) ⇒ Object

Exit the daemon TODO: Make configurable callback chain TODO: Hook into at_exit()



100
101
# File 'lib/daemon_kit/application.rb', line 100

def exit!( code = 0 )
end

.run(file) ⇒ Object

Run the daemon in the foreground without daemonizing



31
32
33
34
35
36
37
38
39
# File 'lib/daemon_kit/application.rb', line 31

def run( file )
  self.chroot
  self.clean_fd
  self.redirect_io( true )

  DaemonKit.configuration.log_stdout = true

  require file
end

.running! {|DaemonKit.configuration| ... } ⇒ Object

Call this from inside a daemonized process to complete the initialization process



91
92
93
94
95
# File 'lib/daemon_kit/application.rb', line 91

def running!
  Initializer.continue!

  yield DaemonKit.configuration if block_given?
end

.start(file) ⇒ Object

Run our file properly



42
43
44
45
46
47
48
49
# File 'lib/daemon_kit/application.rb', line 42

def start( file )
  self.daemonize
  self.chroot
  self.clean_fd
  self.redirect_io

  require file
end

.stopObject



51
52
53
54
55
56
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
# File 'lib/daemon_kit/application.rb', line 51

def stop
  @pid_file = PidFile.new( DaemonKit.configuration.pid_file )

  unless @pid_file.running?
    @pid_file.cleanup
    puts "Nothing to stop"
    exit
  end

  target_pid = @pid_file.pid

  puts "Sending TERM to #{target_pid}"
  Process.kill( 'TERM', target_pid )

  if seconds = DaemonKit.configuration.force_kill_wait
    begin
      Timeout::timeout( seconds ) do
        loop do
          puts "Waiting #{seconds} seconds for #{target_pid} before sending KILL"

          break unless @pid_file.running?

          seconds -= 1
          sleep 1
        end
      end
    rescue Timeout::Error
      Process.kill( 'KILL', target_pid )
    end
  end

  if @pid_file.running?
    puts "Process still running, leaving pidfile behind! Consider using configuration.force_kill_wait."
  else
    @pid_file.cleanup
  end
end