-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Committed improvements #35
Changes from 8 commits
c42be83
13192e2
9f5158d
a1b7729
651e3b3
533223b
dd6945b
67520e3
539b8fc
6973c6f
46637ce
f662b8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,49 +6,50 @@ class PumaRunner | |
|
||
MAX_WAIT_COUNT = 20 | ||
|
||
attr_reader :options, :control_url, :control_token, :cmd_opts | ||
attr_reader :options, :control_url, :control_token, :cmd_opts, :pumactl | ||
|
||
def initialize(options) | ||
@control_token = options.delete(:control_token) { |_| ::Puma::Configuration.random_token } | ||
@control = "localhost" | ||
@control_port = (options.delete(:control_port) || '9293') | ||
@control_url = "#{@control}:#{@control_port}" | ||
@control_url = "localhost:#{@control_port}" | ||
@quiet = options.delete(:quiet) { true } | ||
@pumactl = options.delete(:pumactl) { false } | ||
@options = options | ||
|
||
puma_options = if options[:config] | ||
{ | ||
'--config' => options[:config], | ||
'--control-token' => @control_token, | ||
'--control' => "tcp://#{@control_url}", | ||
'--environment' => options[:environment] | ||
} | ||
puma_options = { | ||
(pumactl ? '--config-file' : '--config') => options[:config], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a fan of this syntax. I see what it's doing, but I'd prefer something more straightforward. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made private constant with keys and private method for getting right key. What do you think? |
||
'--control-token' => @control_token, | ||
(pumactl ? '--control-url' : '--control') => "tcp://#{@control_url}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. This is a bit too clever for my tastes 😉 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also changed. |
||
} | ||
if options[:config] | ||
puma_options['--config'] = options[:config] | ||
else | ||
{ | ||
'--port' => options[:port], | ||
'--control-token' => @control_token, | ||
'--control' => "tcp://#{@control_url}", | ||
'--environment' => options[:environment] | ||
} | ||
puma_options['--port'] = options[:port] | ||
end | ||
[:bind, :threads].each do |opt| | ||
puma_options["--#{opt}"] = options[opt] if options[opt] | ||
%i[bind threads environment].each do |opt| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, is there another way these could be checked with out using multiple There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One The second |
||
next unless options[opt] | ||
if pumactl | ||
next Compat::UI.warning( | ||
"`#{opt}` option is not compatible with `pumactl` option" | ||
) | ||
end | ||
puma_options["--#{opt}"] = options[opt] | ||
end | ||
puma_options = puma_options.to_a.flatten | ||
puma_options << '-q' if @quiet | ||
puma_options << '--quiet' if @quiet | ||
@cmd_opts = puma_options.join ' ' | ||
end | ||
|
||
def start | ||
if in_windows_cmd? | ||
Kernel.system windows_start_cmd | ||
else | ||
Kernel.system nix_start_cmd | ||
end | ||
Kernel.system build_command('start') | ||
end | ||
|
||
def halt | ||
Net::HTTP.get build_uri('halt') | ||
if pumactl | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like these can be pushed down a level too so we don't have so many of these conditionals. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, thank you. |
||
Kernel.system build_command('halt') | ||
else | ||
Net::HTTP.get build_uri('halt') | ||
end | ||
# server may not have been stopped correctly, but we are halting so who cares. | ||
return true | ||
end | ||
|
@@ -69,7 +70,11 @@ def sleep_time | |
private | ||
|
||
def run_puma_command!(cmd) | ||
Net::HTTP.get build_uri(cmd) | ||
if pumactl | ||
Kernel.system build_command(cmd) | ||
else | ||
Net::HTTP.get build_uri(cmd) | ||
end | ||
return true | ||
rescue Errno::ECONNREFUSED => e | ||
# server may not have been started correctly. | ||
|
@@ -80,12 +85,22 @@ def build_uri(cmd) | |
URI "http://#{control_url}/#{cmd}?token=#{control_token}" | ||
end | ||
|
||
def nix_start_cmd | ||
%{sh -c 'cd #{Dir.pwd} && puma #{cmd_opts} &'} | ||
def build_command(cmd) | ||
puma_cmd = "#{pumactl ? 'pumactl' : 'puma'} #{cmd_opts} #{cmd if pumactl}" | ||
background = cmd == 'start' | ||
if in_windows_cmd? | ||
windows_cmd(puma_cmd, background) | ||
else | ||
nix_cmd(puma_cmd, background) | ||
end | ||
end | ||
|
||
def nix_cmd(puma_cmd, background = false) | ||
%(sh -c 'cd #{Dir.pwd} && #{puma_cmd} #{'&' if background}') | ||
end | ||
|
||
def windows_start_cmd | ||
%{cd "#{Dir.pwd}" && start "" /B cmd /C "puma #{cmd_opts}"} | ||
def windows_cmd(puma_cmd, background = false) | ||
%(cd "#{Dir.pwd}" && #{'start "" /B' if background} cmd /C "#{puma_cmd}") | ||
end | ||
|
||
def in_windows_cmd? | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍