Class: Foreground::Daemon

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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd, pid_file) ⇒ Daemon

Returns a new instance of Daemon.



23
24
25
26
# File 'lib/foreground/daemon.rb', line 23

def initialize(cmd, pid_file)
  @cmd = cmd
  @pid_file = pid_file
end

Class Attribute Details

.daemonObject

Returns the value of attribute daemon.



10
11
12
# File 'lib/foreground/daemon.rb', line 10

def daemon
  @daemon
end

Class Method Details

.kill(*args) ⇒ Object



17
18
19
# File 'lib/foreground/daemon.rb', line 17

def kill(*args)
  @daemon.kill(*args)
end

.run(*args) ⇒ Object



12
13
14
15
# File 'lib/foreground/daemon.rb', line 12

def run(*args)
  @daemon = new(*args)
  @daemon.run
end

Instance Method Details

#kill(signal = :TERM) ⇒ Object



35
36
37
# File 'lib/foreground/daemon.rb', line 35

def kill(signal = :TERM)
  Process.kill(signal, pid)
end

#pidObject

TODO: Add scenario for this stuff!



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/foreground/daemon.rb', line 40

def pid
  return @pid unless @pid.nil?
  elapsed_time = 0
  sleep_time = 0.1
  begin
    break if @pid = read_pid
  rescue
    raise unless elapsed_time < Foreground.config[:timeout]
    elapsed_time += sleep_time
  end while sleep(sleep_time)
  @pid
end

#runObject



28
29
30
31
32
33
# File 'lib/foreground/daemon.rb', line 28

def run
  STDOUT.sync = true
  puts "hi, there (foreground #{Foreground::VERSION})!"
  system(*@cmd)
  watch
end

#watchObject



53
54
55
56
57
58
59
60
# File 'lib/foreground/daemon.rb', line 53

def watch
  watch_interval = 10
  begin
    kill(0)
  rescue Errno::ESRCH
    raise DaemonError, "No process with PID #{pid} found."
  end while sleep(watch_interval)
end