Module: Daemontools
- Defined in:
- lib/daemontools.rb,
lib/daemontools/version.rb,
lib/daemontools/service_builder.rb,
lib/daemontools/service_remover.rb
Defined Under Namespace
Classes: Builder, Remover
Constant Summary
collapse
- VERSION =
"0.2.2"
Class Attribute Summary collapse
Class Method Summary
collapse
Class Attribute Details
.log_root ⇒ Object
Returns the value of attribute log_root.
9
10
11
|
# File 'lib/daemontools.rb', line 9
def log_root
@log_root
end
|
.svc_root ⇒ Object
Returns the value of attribute svc_root.
9
10
11
|
# File 'lib/daemontools.rb', line 9
def svc_root
@svc_root
end
|
Class Method Details
.add(name, command, options = {}) ⇒ Object
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
87
88
89
90
91
|
# File 'lib/daemontools.rb', line 60
def self.add(name, command, options = {})
@name = name
@command = command
@log_dir = options[:log_dir] || "#{@log_root}/#{@name}"
@pre_command = options[:pre_command]
@sleep = options[:sleep] || 3
@path = "#{@svc_root}/#{name}"
@change_user_command = options[:change_user_command]
@ulimit = options[:ulimit]
@write_time = options[:write_time]
if Dir.exists?(@path)
stop(name)
else
Dir.mkdir(@path)
end
File.open("#{@path}/down", 'w') {|f| f.write('')}
Dir.mkdir("#{@path}/log") unless Dir.exists?("#{@path}/log")
File.open("#{@path}/log/run", 'w', 0755) {|f| f.write(run_template('log.erb'))}
File.open("#{@path}/run", 'w', 0755) {|f| f.write(run_template('run.erb'))}
unless options[:not_wait]
wait_timeout = options[:wait_timeout] || 10
now = Time.now.to_f
while `sudo svstat #{@path} 2>&1`.match(/unable to open/i)
raise "Timeout wait for svc add service" if Time.now.to_f - now > wait_timeout
sleep 0.1
end
end
true
end
|
.add_empty(name) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/daemontools.rb', line 46
def self.add_empty(name)
path = "#{@svc_root}/#{name}"
Dir.mkdir(path) unless Dir.exists?(path)
File.open("#{path}/down", 'w') {|f| f.write('')}
now = Time.now.to_f
while `sudo svstat #{path} 2>&1`.match(/unable to open/i)
raise "Timeout wait for svc add service" if Time.now.to_f - now > 10
sleep 0.1
end
File.delete("#{path}/down")
stop(name)
true
end
|
.check_service_exists(name, raise_error = true) ⇒ Object
128
129
130
131
132
133
134
135
|
# File 'lib/daemontools.rb', line 128
def self.check_service_exists(name, raise_error = true)
@path = "#{@svc_root}/#{name}"
if raise_error
raise "Service #{name} not exists" unless Dir.exists?(@path)
else
Dir.exists?(@path)
end
end
|
.delete(name, rm_cmd = nil) ⇒ Object
93
94
95
96
97
98
99
100
|
# File 'lib/daemontools.rb', line 93
def self.delete(name, rm_cmd = nil)
check_service_exists(name)
stop(name)
cmd = rm_cmd.nil? ? "rm -rf #{@path} 2>&1" : "#{rm_cmd} #{@path}"
r = `#{cmd}`
raise r if $?.exitstatus != 0
true
end
|
.down?(name) ⇒ Boolean
30
31
32
|
# File 'lib/daemontools.rb', line 30
def self.down?(name)
status(name)[0] == "down"
end
|
.exists?(name) ⇒ Boolean
14
15
16
|
# File 'lib/daemontools.rb', line 14
def self.exists?(name)
check_service_exists(name, false)
end
|
.make_run_status_down(name) ⇒ Object
120
121
122
123
124
|
# File 'lib/daemontools.rb', line 120
def self.make_run_status_down(name)
check_service_exists(name)
File.open("#{@path}/down", 'w') {|f| f.write('')}
true
end
|
.make_run_status_up(name) ⇒ Object
115
116
117
118
|
# File 'lib/daemontools.rb', line 115
def self.make_run_status_up(name)
File.delete("#{@path}/down")
true
end
|
.restart(name) ⇒ Object
42
43
44
|
# File 'lib/daemontools.rb', line 42
def self.restart(name)
run_svc(name, 't')
end
|
.run_status(name) ⇒ Object
102
103
104
105
|
# File 'lib/daemontools.rb', line 102
def self.run_status(name)
check_service_exists(name)
File.exists?("#{@path}/down") ? "down" : "up"
end
|
.run_status_down?(name) ⇒ Boolean
111
112
113
|
# File 'lib/daemontools.rb', line 111
def self.run_status_down?(name)
run_status(name) == "down"
end
|
.run_status_up?(name) ⇒ Boolean
107
108
109
|
# File 'lib/daemontools.rb', line 107
def self.run_status_up?(name)
run_status(name) == "up"
end
|
.run_svc(name, command) ⇒ Object
137
138
139
140
141
142
143
|
# File 'lib/daemontools.rb', line 137
def self.run_svc(name, command)
check_service_exists(name)
r = `sudo svc -#{command} #{@path} 2>&1`
raise r if $?.exitstatus != 0
raise r if ! r.empty?
true
end
|
.run_template(template_name) ⇒ Object
145
146
147
148
149
|
# File 'lib/daemontools.rb', line 145
def self.run_template(template_name)
@user = Etc.getpwuid(Process.uid).name
template_path = File.expand_path(File.dirname(__FILE__))+'/../templates/'+template_name
ERB.new(File.read(template_path)).result(binding())
end
|
.start(name) ⇒ Object
38
39
40
|
# File 'lib/daemontools.rb', line 38
def self.start(name)
run_svc(name, 'u')
end
|
.status(name) ⇒ Object
18
19
20
21
22
23
24
|
# File 'lib/daemontools.rb', line 18
def self.status(name)
check_service_exists(name)
r = `sudo svstat #{@path} 2>&1`
raise r if $?.exitstatus != 0
raise "Unknown status" unless r.match(/.*?:\s*(\S+).*\s(\d+) seconds.*/)
[$1, $2.to_i]
end
|
.stop(name) ⇒ Object
34
35
36
|
# File 'lib/daemontools.rb', line 34
def self.stop(name)
run_svc(name, 'd')
end
|
.up?(name) ⇒ Boolean
26
27
28
|
# File 'lib/daemontools.rb', line 26
def self.up?(name)
status(name)[0] == "up"
end
|