Skip to content

Commit

Permalink
Extract as method
Browse files Browse the repository at this point in the history
Signed-off-by: Yuta Iwama <[email protected]>
  • Loading branch information
ganmacs committed Feb 3, 2020
1 parent 0fd8133 commit bde50da
Showing 1 changed file with 43 additions and 38 deletions.
81 changes: 43 additions & 38 deletions lib/fluent/supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -660,44 +660,7 @@ def supervise
Process.setproctitle("supervisor:#{@system_config.process_name}") if @system_config.process_name
$log.info "starting fluentd-#{Fluent::VERSION}", pid: Process.pid, ruby: RUBY_VERSION

rubyopt = ENV["RUBYOPT"]
fluentd_spawn_cmd = [ServerEngine.ruby_bin_path]

if rubyopt
encodes, others = rubyopt.split(' ').partition { |e| e.match?('\A-E') }
fluentd_spawn_cmd.concat(others)

adopted_encode =
if encodes.size >= 1
r = encodes.pop
unless encodes.empty?
drop = encodes.join(', ')
$log.warn("Encoding option #{drop} is(are) ignored")
end

r
else
'-Eascii-8bit:ascii-8bit'
end

fluentd_spawn_cmd << adopted_encode
else
fluentd_spawn_cmd << '-Eascii-8bit:ascii-8bit'
end

# Adding `-h` so that it can avoid ruby's command blocking
# e.g. `ruby -Eascii-8bit:ascii-8bit` will block. but `ruby -Eascii-8bit:ascii-8bit -h` won't.
cmd = fluentd_spawn_cmd.join(' ')
_, e, s = Open3.capture3("#{cmd} -h")
if s.exitstatus != 0
$log.error('Invalid option is passed to RUBYOPT', command: cmd, error: e)
exit s.exitstatus
end

fluentd_spawn_cmd << $0
fluentd_spawn_cmd += $fluentdargv
fluentd_spawn_cmd << "--under-supervisor"

fluentd_spawn_cmd = build_spawn_command
$log.info "spawn command to main: ", cmdline: fluentd_spawn_cmd

params = {
Expand Down Expand Up @@ -904,5 +867,47 @@ def build_system_config(conf)
system_config.overwrite_variables(**opt)
system_config
end

def build_spawn_command
fluentd_spawn_cmd = [ServerEngine.ruby_bin_path]

rubyopt = ENV['RUBYOPT']
if rubyopt
encodes, others = rubyopt.split(' ').partition { |e| e.match?('\A-E') }
fluentd_spawn_cmd.concat(others)

adopted_encode =
if encodes.size >= 1
r = encodes.pop
unless encodes.empty?
drop = encodes.join(', ')
$log.warn("Encoding option #{drop} is(are) ignored")
end

r
else
'-Eascii-8bit:ascii-8bit'
end

fluentd_spawn_cmd << adopted_encode
else
fluentd_spawn_cmd << '-Eascii-8bit:ascii-8bit'
end

# Adding `-h` so that it can avoid ruby's command blocking
# e.g. `ruby -Eascii-8bit:ascii-8bit` will block. but `ruby -Eascii-8bit:ascii-8bit -h` won't.
cmd = fluentd_spawn_cmd.join(' ')
_, e, s = Open3.capture3("#{cmd} -h")
if s.exitstatus != 0
$log.error('Invalid option is passed to RUBYOPT', command: cmd, error: e)
exit s.exitstatus
end

fluentd_spawn_cmd << $0
fluentd_spawn_cmd += $fluentdargv
fluentd_spawn_cmd << '--under-supervisor'

fluentd_spawn_cmd
end
end
end

0 comments on commit bde50da

Please sign in to comment.