Class: Invoker::Daemon

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

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDaemon

Returns a new instance of Daemon.



7
8
9
# File 'lib/invoker/daemon.rb', line 7

def initialize
  @process_name = 'invoker'
end

Instance Attribute Details

#process_nameObject (readonly)

Returns the value of attribute process_name.



5
6
7
# File 'lib/invoker/daemon.rb', line 5

def process_name
  @process_name
end

Instance Method Details

#daemonizeObject



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

def daemonize
  if fork
    sleep(2)
    exit(0)
  else
    Process.setsid
    File.open(pid_file, "w") do |file|
      file.write(Process.pid.to_s)
    end
    Invoker::Logger.puts "Invoker daemon log is available at #{log_file}"
    redirect_io(log_file)
    $0 = process_name
  end
end

#dead?Boolean

pidfile exists but process isn’t running

Returns:

  • (Boolean)


80
81
82
# File 'lib/invoker/daemon.rb', line 80

def dead?
  status == 1
end

#kill_processObject



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

def kill_process
  pgid =  Process.getpgid(pid)
  Process.kill('-TERM', pgid)
  File.delete(pid_file) if File.exist?(pid_file)
  Invoker::Logger.puts "Stopped Invoker daemon"
end

#log_fileObject



34
35
36
# File 'lib/invoker/daemon.rb', line 34

def log_file
  File.join(Invoker.home, ".invoker", "#{process_name}.log")
end

#pidObject



30
31
32
# File 'lib/invoker/daemon.rb', line 30

def pid
  File.read(pid_file).strip.to_i
end

#pid_fileObject



26
27
28
# File 'lib/invoker/daemon.rb', line 26

def pid_file
  File.join(Invoker.home, ".invoker", "#{process_name}.pid")
end

#pidfile_exists?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/invoker/daemon.rb', line 71

def pidfile_exists?
  File.exist?(pid_file)
end

#process_running?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
# File 'lib/invoker/daemon.rb', line 60

def process_running?
  Process.kill(0, pid)
  true
rescue Errno::ESRCH
  false
end

#running?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/invoker/daemon.rb', line 75

def running?
  status == 0
end

#startObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/invoker/daemon.rb', line 11

def start
  if running?
    Invoker::Logger.puts "Invoker daemon is already running"
    exit(0)
  elsif dead?
    File.delete(pid_file) if File.exists?(pid_file)
  end
  Invoker::Logger.puts "Running Invoker daemon"
  daemonize
end

#statusObject



67
68
69
# File 'lib/invoker/daemon.rb', line 67

def status
  @status ||= check_process_status
end

#stopObject



22
23
24
# File 'lib/invoker/daemon.rb', line 22

def stop
  kill_process
end