Class: Upman::Core::Daemon

Inherits:
Object
  • Object
show all
Includes:
Utils::Helper
Defined in:
lib/upman/core/daemon.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Helper

#fail, #info, #success, #warn

Constructor Details

#initialize(options) ⇒ Daemon

Returns a new instance of Daemon.



12
13
14
15
16
17
18
# File 'lib/upman/core/daemon.rb', line 12

def initialize(options)
  @options = options
  options[:logfile] = File.expand_path(logfile) if logfile? # daemonization might change CWD so expand any relative paths in advance
  options[:pidfile] = File.expand_path(pidfile) if pidfile? # (ditto)


end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#quitObject (readonly)

Returns the value of attribute quit.



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

def quit
  @quit
end

Instance Method Details

#check_pidObject



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/upman/core/daemon.rb', line 125

def check_pid
  if pidfile?
    case pid_status(pidfile)
    when :running, :not_owned
      puts "A server is already running. Check #{pidfile}"
      exit(1)
    when :dead
      File.delete(pidfile)
    end
  end
end

#daemonizeObject

DAEMONIZING, PID MANAGEMENT, and OUTPUT REDIRECTION



92
93
94
95
96
97
# File 'lib/upman/core/daemon.rb', line 92

def daemonize
  exit if fork
  Process.setsid
  exit if fork
  Dir.chdir "/"
end

#daemonize?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/upman/core/daemon.rb', line 20

def daemonize?
  options[:daemonize]
end

#logfileObject



24
25
26
# File 'lib/upman/core/daemon.rb', line 24

def logfile
  options[:logfile]
end

#logfile?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/upman/core/daemon.rb', line 32

def logfile?
  !logfile.nil?
end

#pid_status(pidfile) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/upman/core/daemon.rb', line 137

def pid_status(pidfile)
  return :exited unless File.exists?(pidfile)
  pid = ::File.read(pidfile).to_i
  return :dead if pid == 0
  Process.kill(0, pid)
  :running
rescue Errno::ESRCH
  :dead
rescue Errno::EPERM
  :not_owned
end

#pidfileObject



28
29
30
# File 'lib/upman/core/daemon.rb', line 28

def pidfile
  options[:pidfile]
end

#pidfile?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/upman/core/daemon.rb', line 36

def pidfile?
  !pidfile.nil?
end

#redirect_outputObject



99
100
101
102
103
104
105
106
# File 'lib/upman/core/daemon.rb', line 99

def redirect_output
  FileUtils.mkdir_p(File.dirname(logfile), :mode => 0755)
  FileUtils.touch logfile
  File.chmod(0644, logfile)
  $stderr.reopen(logfile, 'a')
  $stdout.reopen($stderr)
  $stdout.sync = $stderr.sync = true
end

#run!Object




43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/upman/core/daemon.rb', line 43

def run!

  check_pid
  daemonize if daemonize?
  write_pid
  trap_signals

  if logfile?
    redirect_output
  elsif daemonize?
    suppress_output
  end

  api = Upman::Utils::Api.new
  unless api.check_connection
    return nil
  end

  register = Upman::Core::Register.new
  register.perform

  threads = []

  Thread.abort_on_exception = true

  threads << Thread.new do
    server = Upman::Server::Socket.new
    server.run!
  end

  threads << Thread.new do
    worker = Upman::Core::Worker.new
    worker.run!
  end

  trap("INT") do
    warn "CTRL+C - Shutdown all threads"
    threads.each { |t| Thread.kill t }
    exit 130
  end

  threads.each { |t| t.join }

end

#suppress_outputObject



108
109
110
111
# File 'lib/upman/core/daemon.rb', line 108

def suppress_output
  $stderr.reopen('/dev/null', 'a')
  $stdout.reopen($stderr)
end

#trap_signalsObject

SIGNAL HANDLING



153
154
155
156
157
# File 'lib/upman/core/daemon.rb', line 153

def trap_signals
  trap(:QUIT) do # graceful shutdown
    @quit = true
  end
end

#write_pidObject



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/upman/core/daemon.rb', line 113

def write_pid
  if pidfile?
    begin
      File.open(pidfile, ::File::CREAT | ::File::EXCL | ::File::WRONLY) {|f| f.write("#{Process.pid}")}
      at_exit {File.delete(pidfile) if File.exists?(pidfile)}
    rescue Errno::EEXIST
      check_pid
      retry
    end
  end
end