Skip to content

Commit

Permalink
Allow customizing the upgrade command line... via the commandline
Browse files Browse the repository at this point in the history
This allows replacing the einhorn command name (say,
`/usr/local/bin/einhorn`) with a more intricate command
line (e.g. `rbenv exec bundle exec einhorn`). That command line will
be split using shellwords.

To that end, introduce `Einhorn.upgrade_commandline`, a helper method
that can construct the command line used when reloading code.
  • Loading branch information
asf-stripe committed May 22, 2014
1 parent 9dd324e commit b9fc5ee
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
4 changes: 4 additions & 0 deletions bin/einhorn
Expand Up @@ -275,6 +275,10 @@ if true # $0 == __FILE__
Einhorn::Command.louder(false)
end

opts.on('--reexec-as=CMDLINE', 'Substitute CMDLINE for \"einhorn\" when upgrading') do |cmdline|
Einhorn::TransientState.re_exec_commandline = Shellwords.shellsplit(cmdline)
end

Einhorn.plugins_send(:optparse, opts)

opts.on('--nice MASTER[:WORKER=0][:RENICE_CMD=/usr/bin/renice]', 'Unix nice level at which to run the einhorn processes. If not running as root, make sure to ulimit -e as appopriate.') do |nice|
Expand Down
15 changes: 15 additions & 0 deletions lib/einhorn.rb
Expand Up @@ -5,6 +5,7 @@
require 'socket'
require 'tmpdir'
require 'yaml'
require 'shellwords'

require 'einhorn/third/little-plugger'

Expand Down Expand Up @@ -329,6 +330,20 @@ def self.socketify!(cmd)
end
end

# Construct and a command and args that can be used to re-exec
# Einhorn for upgrades.
def self.upgrade_commandline(prefix=[])
cmdline = []
if Einhorn::TransientState.re_exec_commandline
cmdline += Einhorn::TransientState.re_exec_commandline
else
cmdline << Einhorn::TransientState.script_name
end
cmdline += prefix
cmdline += Einhorn::State.cmd
[cmdline[0], cmdline[1..-1]]
end

# Perform startup checks to ensure our environment is sane
def self.sanity_check
log_info("Running under Ruby #{RUBY_VERSION}")
Expand Down
11 changes: 5 additions & 6 deletions lib/einhorn/command.rb
Expand Up @@ -226,11 +226,10 @@ def self.reload
ENV.update(Einhorn::TransientState.environ)

begin
Einhorn::Compat.exec(
Einhorn::TransientState.script_name,
['--with-state-fd', read.fileno.to_s, '--'] + Einhorn::State.cmd,
:close_others => false
)
upgrade_cmd, upgrade_args =
Einhorn::Compat.exec(
*Einhorn.upgrade_commandline(['--with-state-fd', read.fileno.to_s, '--']),
:close_others => false)
rescue SystemCallError => e
Einhorn.log_error("Could not reload! Attempting to continue. Error was: #{e}")
Einhorn::State.reloading_for_preload_upgrade = false
Expand Down Expand Up @@ -278,7 +277,7 @@ def self.spinup(cmd=nil)
Einhorn::Event.close_all_for_worker

prepare_child_environment(index)
Einhorn::Compat.exec(cmd[0], cmd[1..-1], :close_others => false)
Einhorn::Compat.exec(*Einhorn.upgrade_commandline, :close_others => false)
end
end

Expand Down

0 comments on commit b9fc5ee

Please sign in to comment.