Class: MultiDaemons::Daemon
- Inherits:
-
Object
- Object
- MultiDaemons::Daemon
- Defined in:
- lib/multi_daemons/daemon.rb
Constant Summary collapse
- KILL_TIMEOUT =
30
Instance Attribute Summary collapse
-
#daemon ⇒ Object
Returns the value of attribute daemon.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#multiple ⇒ Object
Returns the value of attribute multiple.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(daemon, name:, type:, options: {}) ⇒ Daemon
constructor
A new instance of Daemon.
- #pid_file ⇒ Object
- #pids ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(daemon, name:, type:, options: {}) ⇒ Daemon
Returns a new instance of Daemon.
6 7 8 9 10 11 12 |
# File 'lib/multi_daemons/daemon.rb', line 6 def initialize(daemon, name:, type:, options: {}) @daemon = daemon @name = name @type = type @options = raise unless Validate.valid_daemon?(self) end |
Instance Attribute Details
#daemon ⇒ Object
Returns the value of attribute daemon.
4 5 6 |
# File 'lib/multi_daemons/daemon.rb', line 4 def daemon @daemon end |
#errors ⇒ Object
Returns the value of attribute errors.
4 5 6 |
# File 'lib/multi_daemons/daemon.rb', line 4 def errors @errors end |
#multiple ⇒ Object
Returns the value of attribute multiple.
4 5 6 |
# File 'lib/multi_daemons/daemon.rb', line 4 def multiple @multiple end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/multi_daemons/daemon.rb', line 4 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
4 5 6 |
# File 'lib/multi_daemons/daemon.rb', line 4 def @options end |
#type ⇒ Object
Returns the value of attribute type.
4 5 6 |
# File 'lib/multi_daemons/daemon.rb', line 4 def type @type end |
Instance Method Details
#pid_file ⇒ Object
52 53 54 |
# File 'lib/multi_daemons/daemon.rb', line 52 def pid_file "#{pid_dir}#{name}.pid" end |
#pids ⇒ Object
48 49 50 |
# File 'lib/multi_daemons/daemon.rb', line 48 def pids PidStore.get(pid_file) end |
#start ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/multi_daemons/daemon.rb', line 14 def start case type when :proc, 'proc' safe_fork do daemon.call end when :script, 'script' safe_fork do Kernel.exec(daemon) end else Log.log 'Unsupported type' end end |
#stop ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/multi_daemons/daemon.rb', line 29 def stop if File.file?(pid_file) pids.each do |pid| begin Process.kill('TERM', pid) rescue Errno::ESRCH => e Log.log "#{e} #{pid}" end end return if multiple Pid.force_kill(pids, [:force_kill_timeout]) PidStore.cleanup(pid_file) else Log.log 'Pid file not found. Is daemon running?' end end |