Class: Nand::Daemon

Inherits:
Object show all
Extended by:
ProcOperation
Includes:
Logging
Defined in:
lib/nand/daemon.rb

Constant Summary

Constants included from Logging

Logging::LOG_DEBUG, Logging::LOG_ERROR, Logging::LOG_FATAL, Logging::LOG_INFO, Logging::LOG_WARN

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ProcOperation

all_runnings, name_from_pidfile, pid_filename, pid_from_pidfile, ps, running_in, running_ps?, running_with?

Methods included from Logging

#log, #log_debug!

Constructor Details

#initialize(run_dir, execname, opts = {}) ⇒ Daemon

Returns a new instance of Daemon.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/nand/daemon.rb', line 16

def initialize(run_dir, execname, opts = {} )
  @run_dir = Pathname.new(run_dir)
  @execname = execname.to_s
  @launcher = opts[:launcher]
  @arg_uid  = opts[:uid]
  @pid_file = @run_dir.join(self.class.pid_filename(@execname))
  @daemon_log = opts[:daemon_log] || "/dev/null"
  @daemon_out = opts[:daemon_out] || "/dev/null"
  @daemon_err = opts[:daemon_err] || "/dev/null"
  @recovery   = opts[:recovery] || false
  @recovery_sec = opts[:recovery_sec] || 1
  @limit = opts[:sec]

  log.level  = LOG_DEBUG if opts[:debug]
end

Instance Attribute Details

#execnameObject (readonly)

Returns the value of attribute execname.



15
16
17
# File 'lib/nand/daemon.rb', line 15

def execname
  @execname
end

#run_dirObject (readonly)

Returns the value of attribute run_dir.



15
16
17
# File 'lib/nand/daemon.rb', line 15

def run_dir
  @run_dir
end

Instance Method Details

#killObject



45
# File 'lib/nand/daemon.rb', line 45

def kill; stop_with_signal(:KILL) end

#logger_formatterObject



13
# File 'lib/nand/daemon.rb', line 13

def logger_formatter     ; TimeFormatter.new end

#logger_output_paramsObject



11
# File 'lib/nand/daemon.rb', line 11

def logger_output_params ; [@daemon_log]     end

#logger_prognameObject



12
# File 'lib/nand/daemon.rb', line 12

def logger_progname      ; @execname         end

#pidObject



46
# File 'lib/nand/daemon.rb', line 46

def pid;  @pid ||=self.class.pid_from_pidfile(@pid_file) end

#runObject



39
40
41
42
43
# File 'lib/nand/daemon.rb', line 39

def run
  raise "Launcher is Not Specified for #{@execname}" if @launcher.nil?
  raise "PID file exist #{@pid_file}" if @pid_file.exist?
  daemonize
end

#running?Boolean

Returns:

  • (Boolean)


37
# File 'lib/nand/daemon.rb', line 37

def running?; @pid_file.exist? and self.class.running_with?(pid, @execname) end

#stopObject



44
# File 'lib/nand/daemon.rb', line 44

def stop; stop_with_signal(:TERM) end

#uidObject



36
# File 'lib/nand/daemon.rb', line 36

def uid;  @arg_uid || Process.uid  end

#userObject



31
32
33
34
35
# File 'lib/nand/daemon.rb', line 31

def user
  Etc.getpwuid(uid).name
rescue => e
  nil
end