Module: DaemonOgre::Daemon

Defined in:
lib/daemon-ogre/daemon.rb

Class Method Summary collapse

Class Method Details

.initObject Also known as: new



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/daemon-ogre/daemon.rb', line 154

def init

  if DARGV.terminate?
    stop
  end

  if DARGV.daemonize?
    start( ::Kernel.fork )
  end

end

.killObject

Try and read the existing pid from the pid file and signal the process. Returns true for a non blocking status.



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

def kill

  opid  = OPTS.pidfile.to_i
  Process.kill 'HUP', opid.to_i
  true
rescue Errno::ENOENT
  $stdout.puts "#{pidfile} did not exist: Errno::ENOENT"                        if $DEBUG
  true
rescue Errno::ESRCH
  $stdout.puts "The process #{opid} did not exist: Errno::ESRCH"                if $DEBUG
  true
rescue Errno::EPERM
  $stderr.puts "Lack of privileges to manage the process #{opid}: Errno::EPERM" if $DEBUG
  false
rescue ::Exception => e
  $stderr.puts "While signaling the PID, unexpected #{e.class}: #{e}"           if $DEBUG
  false

end

.kill_by_nameObject



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
# File 'lib/daemon-ogre/daemon.rb', line 108

def kill_by_name

  # name switch
  target_name ||= $0
  $0 = "ruby_tmp_process"


  app_state= false

  start_time= Time.now
  while `ps aux | grep #{target_name}`.split(' ')[1] != ""  || (Time.now - start_time) < 6

    begin

      Process.kill "TERM",`ps aux | grep #{target_name}`.split(' ')[1].to_i

      app_state= true
    rescue Errno::ESRCH
      $stdout.puts "The process #{target_name} did not exist: Errno::ESRCH"                 if $DEBUG
      break
    rescue Errno::EPERM
      $stderr.puts "Lack of privileges to manage the process #{target_name}: Errno::EPERM"  if $DEBUG
      break
    rescue ::Exception => e
      $stderr.puts "While signaling the PID, unexpected #{e.class}: #{e}"                   if $DEBUG
      break
    end

  end


  # name switch back
  begin
    $0 = target_name
  end

  return app_state
end

.redirectObject

Send stdout and stderr to log files for the child process



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/daemon-ogre/daemon.rb', line 55

def redirect

  var= 3
  begin

    $stdin.reopen   File.join('','dev','null')
    $stdout.reopen  OPTS.out__path__
    $stderr.reopen  OPTS.err__path__
    $stdout.sync =  $stderr.sync = true

  rescue Errno::ENOENT => ex

    var -= 1
    retry if var > 0
    raise ex.class,ex

  end

end

.start(pid) ⇒ Object

Checks to see if the current process is the child process and if not will update the pid file with the child pid.



9
10
11
12
13
14
15
16
17
# File 'lib/daemon-ogre/daemon.rb', line 9

def start pid
  unless pid.nil?
    raise "Fork failed" if pid == -1
    OPTS.pidfile= pid
    ::Kernel.exit
  else
    redirect
  end
end

.stopObject



147
148
149
150
151
152
# File 'lib/daemon-ogre/daemon.rb', line 147

def stop

  self.terminate
  ::Process.exit!

end

.term_killObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/daemon-ogre/daemon.rb', line 75

def term_kill
  unless OPTS.pidfile.nil?

    begin

      STDOUT.puts("PidFile found, processing...")   if $DEBUG
      Process.kill('TERM', OPTS.pidfile.to_i)
      return true

    rescue Exception => ex

      STDOUT.puts "Exception happened on terminating process: #{ex.class}, #{ex}" if $DEBUG
      return false

    end

  end
end

.terminateObject



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/daemon-ogre/daemon.rb', line 94

def terminate

  # kill methods
  app_killed= false
  [:kill,:term_kill,:kill_by_name].each do |method_name|
    app_killed= self.__send__ method_name
    break if app_killed
  end
  OPTS.pidfile= nil

  return app_killed

end

.write(pid) ⇒ Object

Attempts to write the pid of the forked process to the pid file.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/daemon-ogre/daemon.rb', line 20

def write pid
  OPTS.pid= pid

  File.open pidfile, "a+" do |new_line|
    new_line.write "#{pid}\n"
  end

rescue ::Exception => e
  $stderr.puts "While writing the PID to file, unexpected #{e.class}: #{e}"
  Process.kill "HUP", pid
end