Class: MultiDaemons::Daemon

Inherits:
Object
  • Object
show all
Defined in:
lib/multi_daemons/daemon.rb

Constant Summary collapse

KILL_TIMEOUT =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

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  = options
  raise unless Validate.valid_daemon?(self)
end

Instance Attribute Details

#daemonObject

Returns the value of attribute daemon.



4
5
6
# File 'lib/multi_daemons/daemon.rb', line 4

def daemon
  @daemon
end

#errorsObject

Returns the value of attribute errors.



4
5
6
# File 'lib/multi_daemons/daemon.rb', line 4

def errors
  @errors
end

#multipleObject

Returns the value of attribute multiple.



4
5
6
# File 'lib/multi_daemons/daemon.rb', line 4

def multiple
  @multiple
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/multi_daemons/daemon.rb', line 4

def name
  @name
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/multi_daemons/daemon.rb', line 4

def options
  @options
end

#typeObject

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_fileObject



52
53
54
# File 'lib/multi_daemons/daemon.rb', line 52

def pid_file
  "#{pid_dir}#{name}.pid"
end

#pidsObject



48
49
50
# File 'lib/multi_daemons/daemon.rb', line 48

def pids
  PidStore.get(pid_file)
end

#startObject



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

#stopObject



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, options[:force_kill_timeout])
    PidStore.cleanup(pid_file)
  else
    Log.log 'Pid file not found. Is daemon running?'
  end
end