Module: Einhorn
- Defined in:
- lib/einhorn.rb,
lib/einhorn/event.rb,
lib/einhorn/prctl.rb,
lib/einhorn/third.rb,
lib/einhorn/client.rb,
lib/einhorn/compat.rb,
lib/einhorn/worker.rb,
lib/einhorn/command.rb,
lib/einhorn/version.rb,
lib/einhorn/safe_yaml.rb,
lib/einhorn/prctl_linux.rb,
lib/einhorn/worker_pool.rb
Defined Under Namespace
Modules: AbstractState, Command, Compat, Event, PrctlRaw, SafeYAML, State, Third, TransientState, Worker, WorkerPool
Classes: Client, PrctlAbstract, PrctlLinux, PrctlUnimplemented
Constant Summary
collapse
- Prctl =
PrctlUnimplemented
- VERSION =
"1.1.1"
Class Method Summary
collapse
-
.bind(addr, port, flags) ⇒ Object
-
.can_safely_reload? ⇒ Boolean
Returns true if a reload of the einhorn master via re-execing is not likely to be completely unsafe (that is, the new process's environment won't prevent it from loading its code on exec).
-
.dump_environment_info ⇒ Object
Log info about the environment as observed by ruby on startup.
-
.initialize_reload_environment ⇒ Object
Set up the environment for reloading the einhorn master: 1.
-
.is_script(file) ⇒ Object
Not really a thing, but whatever.
-
.log_debug(msg, tag = nil) ⇒ Object
Implement these ourselves so it plays nicely with state persistence.
-
.log_error(msg, tag = nil) ⇒ Object
-
.log_info(msg, tag = nil) ⇒ Object
-
.master_ps_name ⇒ Object
-
.preload ⇒ Object
-
.print_state ⇒ Object
-
.renice_self ⇒ Object
-
.restore_state(state) ⇒ Object
-
.run ⇒ Object
-
.send_tagged_message(tag, message, last = false) ⇒ Object
-
.set_argv(cmd, set_ps_name) ⇒ Object
-
.set_master_ps_name ⇒ Object
-
.socketify_env! ⇒ Object
-
.update_state(store, store_name, old_state) ⇒ Object
-
.upgrade_commandline(einhorn_flags = []) ⇒ Object
Construct and a command and args that can be used to re-exec Einhorn for upgrades.
-
.which(cmd) ⇒ Object
-
.worker_ps_name ⇒ Object
Class Method Details
.bind(addr, port, flags) ⇒ Object
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
# File 'lib/einhorn.rb', line 165
def self.bind(addr, port, flags)
log_info("Binding to #{addr}:#{port} with flags #{flags.inspect}")
sd = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
Einhorn::Compat.cloexec!(sd, false)
if flags.include?("r") || flags.include?("so_reuseaddr")
sd.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1)
end
sd.bind(Socket.pack_sockaddr_in(port, addr))
sd.listen(Einhorn::State.config[:backlog])
if flags.include?("n") || flags.include?("o_nonblock")
fl = sd.fcntl(Fcntl::F_GETFL)
sd.fcntl(Fcntl::F_SETFL, fl | Fcntl::O_NONBLOCK)
end
Einhorn::TransientState.socket_handles << sd
[sd.fileno, sd.local_address.ip_port]
end
|
.can_safely_reload? ⇒ Boolean
Returns true if a reload of the einhorn master via re-execing is
not likely to be completely unsafe (that is, the new process's
environment won't prevent it from loading its code on exec).
.dump_environment_info ⇒ Object
Log info about the environment as observed by ruby on
startup. Currently, this means the bundler and rbenv versions.
404
405
406
407
408
409
410
411
412
|
# File 'lib/einhorn.rb', line 404
def self.dump_environment_info
log_info("Running under Ruby #{RUBY_VERSION}", :environment)
log_info("Rbenv ruby version: #{ENV["RBENV_VERSION"]}", :environment) if ENV["RBENV_VERSION"]
begin
bundler_gem = Gem::Specification.find_by_name("bundler")
log_info("Using Bundler #{bundler_gem.version}", :environment)
rescue Gem::LoadError
end
end
|
.initialize_reload_environment ⇒ Object
Set up the environment for reloading the einhorn master:
- Clear the current process's environment,
- Set it to the environmment at startup
- Delete all variables marked to be dropped via
--drop-env-var
This method is safe to call in the master only before execing
something.
394
395
396
397
398
399
400
|
# File 'lib/einhorn.rb', line 394
def self.initialize_reload_environment
ENV.clear
ENV.update(Einhorn::TransientState.environ)
Einhorn::State.drop_environment_variables.each do |var|
ENV.delete(var)
end
end
|
.is_script(file) ⇒ Object
Not really a thing, but whatever.
237
238
239
240
241
242
|
# File 'lib/einhorn.rb', line 237
def self.is_script(file)
File.open(file) do |f|
bytes = f.read(2)
bytes == "#!"
end
end
|
.log_debug(msg, tag = nil) ⇒ Object
Implement these ourselves so it plays nicely with state persistence
187
188
189
190
191
|
# File 'lib/einhorn.rb', line 187
def self.log_debug(msg, tag = nil)
warn("#{log_tag} DEBUG: #{msg}\n") if Einhorn::State.verbosity <= 0
$stderr.flush
send_tagged_message(tag, msg) if tag
end
|
.log_error(msg, tag = nil) ⇒ Object
199
200
201
202
203
|
# File 'lib/einhorn.rb', line 199
def self.log_error(msg, tag = nil)
warn("#{log_tag} ERROR: #{msg}\n") if Einhorn::State.verbosity <= 2
$stderr.flush
send_tagged_message(tag, "ERROR: #{msg}") if tag
end
|
.log_info(msg, tag = nil) ⇒ Object
193
194
195
196
197
|
# File 'lib/einhorn.rb', line 193
def self.log_info(msg, tag = nil)
warn("#{log_tag} INFO: #{msg}\n") if Einhorn::State.verbosity <= 1
$stderr.flush
send_tagged_message(tag, msg) if tag
end
|
.master_ps_name ⇒ Object
324
325
326
|
# File 'lib/einhorn.rb', line 324
def self.master_ps_name
"einhorn: #{worker_ps_name}"
end
|
.preload ⇒ Object
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
# File 'lib/einhorn.rb', line 244
def self.preload
if (path = Einhorn::State.path)
set_argv(Einhorn::State.cmd, false)
begin
Einhorn::State.preloaded = false
if !path.end_with?(".rb") && File.exist?(path)
log_info("Loading #{path} (if this hangs, make sure your code can be properly loaded as a library)", :upgrade)
load path
else
log_info("Requiring #{path} (if this hangs, make sure your code can be properly loaded as a library)", :upgrade)
require path
if Einhorn::State.config[:gc_before_fork]
if Process.respond_to?(:warmup)
Process.warmup
else
force_move_to_oldgen
end
end
end
rescue StandardError, LoadError => e
log_info("Proceeding with postload -- could not load #{path}: #{e} (#{e.class})\n #{e.backtrace.join("\n ")}", :upgrade)
else
if defined?(einhorn_main)
log_info("Successfully loaded #{path}", :upgrade)
Einhorn::State.preloaded = true
else
log_info("Proceeding with postload -- loaded #{path}, but no einhorn_main method was defined", :upgrade)
end
end
end
end
|
.print_state ⇒ Object
161
162
163
|
# File 'lib/einhorn.rb', line 161
def self.print_state
log_info(Einhorn::State.state.pretty_inspect)
end
|
.renice_self ⇒ Object
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
# File 'lib/einhorn.rb', line 332
def self.renice_self
whatami = Einhorn::TransientState.whatami
return unless (nice = Einhorn::State.nice[whatami])
pid = $$
unless nice.is_a?(Integer)
raise "Nice must be a fixnum: #{nice.inspect}"
end
cmd = "#{Einhorn::State.nice[:renice_cmd]} #{nice} -p #{pid}"
log_info("Running #{cmd.inspect} to renice self to level #{nice}")
`#{cmd}`
unless $?.exitstatus == 0
log_error("Renice command exited with status: #{$?.inspect}, but continuing on anyway.")
end
end
|
.restore_state(state) ⇒ Object
.send_tagged_message(tag, message, last = false) ⇒ Object
.set_argv(cmd, set_ps_name) ⇒ Object
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
# File 'lib/einhorn.rb', line 296
def self.set_argv(cmd, set_ps_name)
idx = 0
if /(^|\/)ruby$/.match?(cmd[0])
idx = 1
elsif !is_script(cmd[0])
log_info("WARNING: Going to set $0 to #{cmd[idx]}, but it doesn't look like a script")
end
if set_ps_name
$0 = worker_ps_name
end
ARGV[0..-1] = cmd[idx + 1..-1]
log_info("Set#{" $0 = #{$0.inspect}, " if set_ps_name} ARGV = #{ARGV.inspect}")
end
|
.set_master_ps_name ⇒ Object
320
321
322
|
# File 'lib/einhorn.rb', line 320
def self.set_master_ps_name
$0 = master_ps_name
end
|
.socketify_env! ⇒ Object
351
352
353
354
355
356
357
|
# File 'lib/einhorn.rb', line 351
def self.socketify_env!
Einhorn::State.bind.each do |host, port, flags|
fd, actual_port = bind(host, port, flags)
Einhorn::State.bind_fds << fd
Einhorn::State.bound_ports << actual_port
end
end
|
.update_state(store, store_name, old_state) ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/einhorn.rb', line 122
def self.update_state(store, store_name, old_state)
message = []
updated_state = old_state.dup
if store == Einhorn::State && updated_state[:children]
dead = []
updated_state[:children].each do |pid, v|
pid = Process.wait(pid, Process::WNOHANG)
dead << pid if pid
rescue Errno::ECHILD
dead << pid
end
Einhorn::Event::Timer.open(0) do
dead.each { |pid| Einhorn::Command.cleanup(pid) }
end
end
default = store.default_state
added_keys = default.keys - updated_state.keys
deleted_keys = updated_state.keys - default.keys
return [updated_state, message.first] if added_keys.length == 0 && deleted_keys.length == 0
added_keys.each { |key| updated_state[key] = default[key] }
deleted_keys.each { |key| updated_state.delete(key) }
message << "adding default values for #{added_keys.inspect}"
message << "deleting values for #{deleted_keys.inspect}"
message = "State format for #{store_name} has changed: #{message.join(", ")}"
[updated_state, message]
end
|
.upgrade_commandline(einhorn_flags = []) ⇒ Object
Construct and a command and args that can be used to re-exec
Einhorn for upgrades.
361
362
363
364
365
366
367
368
369
370
371
372
|
# File 'lib/einhorn.rb', line 361
def self.upgrade_commandline(einhorn_flags = [])
cmdline = []
if Einhorn::State.reexec_commandline
cmdline += Einhorn::State.reexec_commandline
else
cmdline << Einhorn::TransientState.script_name
end
cmdline += einhorn_flags
cmdline << "--"
cmdline += Einhorn::State.cmd
[cmdline[0], cmdline[1..-1]]
end
|
.which(cmd) ⇒ Object
223
224
225
226
227
228
229
230
231
232
233
234
|
# File 'lib/einhorn.rb', line 223
def self.which(cmd)
if cmd.include?("/")
return cmd if File.exist?(cmd)
raise "Could not find #{cmd}"
else
ENV["PATH"].split(":").each do |f|
abs = File.join(f, cmd)
return abs if File.exist?(abs)
end
raise "Could not find #{cmd} in PATH"
end
end
|