Module: RRRSpec::Server
- Defined in:
- lib/rrrspec/server/cli.rb,
lib/rrrspec/server/arbiter.rb,
lib/rrrspec/server/version.rb,
lib/rrrspec/server/persister.rb,
lib/rrrspec/server/daemonizer.rb,
lib/rrrspec/server/dispatcher.rb,
lib/rrrspec/server/configuration.rb,
lib/rrrspec/server/worker_runner.rb,
lib/rrrspec/server/json_constructor.rb,
lib/rrrspec/server/persistent_models.rb,
lib/rrrspec/server/log_file_persister.rb
Defined Under Namespace
Modules: Arbiter, JSONConstructor, LogFilePersister, Persistence, Persister
Classes: CLI, Dispatcher, ServerConfiguration, WorkerConfiguration, WorkerRunner
Constant Summary
collapse
- VERSION =
"0.2.1"
- RESPAWN_INTERVAL_LIMIT_SEC =
30
Class Method Summary
collapse
Class Method Details
.check_pidfile(pidfile) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/rrrspec/server/daemonizer.rb', line 31
def self.check_pidfile(pidfile)
if File.exist?(pidfile)
if File.readable?(pidfile)
pid = File.read(pidfile).to_i
begin
Process.kill(0, pid)
raise "Pid(#{pid}) is running"
rescue Errno::EPERM
raise "Pid(#{pid}) is running"
rescue Errno::ESRCH
end
else
raise "Cannot access #{pidfile}"
end
unless File.writable?(pidfile)
raise "Cannot access #{pidfile}"
end
else
unless File.writable?(File.dirname(pidfile))
raise "Cannot access #{pidfile}"
end
end
end
|
.daemonizer(process_name, &block) ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/rrrspec/server/daemonizer.rb', line 5
def self.daemonizer(process_name, &block)
$0 = process_name
return block.call unless should_daemonize?
pidfile = File.absolute_path(RRRSpec.configuration.pidfile || File.join("/var/run", "#{process_name}.pid"))
check_pidfile(pidfile)
Process.daemon
File.write(pidfile, Process.pid.to_s)
File.umask(0)
setup_signal_handlers
setup_atexit_handlers(pidfile)
monitor_fork do
Process::Sys.setuid(RRRSpec.configuration.user) if RRRSpec.configuration.user
block.call
end
end
|
.monitor_fork(&block) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/rrrspec/server/daemonizer.rb', line 75
def self.monitor_fork(&block)
loop do
started_at = Time.now
pid = Process.fork(&block)
Process.waitpid(pid)
break unless should_monitor?
if Time.now - started_at < RESPAWN_INTERVAL_LIMIT_SEC
sleep RESPAWN_INTERVAL_LIMIT_SEC
end
end
end
|
.setup_atexit_handlers(pidfile) ⇒ Object
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/rrrspec/server/daemonizer.rb', line 64
def self.setup_atexit_handlers(pidfile)
current_pid = Process.pid
at_exit do
File.delete(pidfile) if Process.pid == current_pid
end
end
|
.setup_signal_handlers ⇒ Object
56
57
58
59
60
61
62
|
# File 'lib/rrrspec/server/daemonizer.rb', line 56
def self.setup_signal_handlers
Signal.trap('TERM') do
Signal.trap('TERM', 'DEFAULT')
Process.kill('TERM', 0)
end
end
|
.should_daemonize? ⇒ Boolean
23
24
25
|
# File 'lib/rrrspec/server/daemonizer.rb', line 23
def self.should_daemonize?
RRRSpec.configuration.daemonize == nil || RRRSpec.configuration.daemonize
end
|
.should_monitor? ⇒ Boolean
27
28
29
|
# File 'lib/rrrspec/server/daemonizer.rb', line 27
def self.should_monitor?
RRRSpec.configuration.monitor == nil || RRRSpec.configuration.monitor
end
|