Skip to content

Commit 40a9cb3

Browse files
author
Kevin Paulisse
committed
More logger fixes for parallel
1 parent 5daa1e7 commit 40a9cb3

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

lib/octocatalog-diff/util/scriptrunner.rb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ class ScriptException < RuntimeError; end
2121
#
2222
# @param opts [Hash] Options hash
2323
# opts[:default_script] (Required) Path to script, relative to `scripts` directory
24-
# opts[:logger] (Required) Logger object
24+
# opts[:logger] (Optional) Logger object
2525
# opts[:override_script_path] (Optional) Directory where a similarly-named script MAY exist
2626
def initialize(opts = {})
27-
@logger = opts.fetch(:logger)
27+
@logger = opts[:logger]
2828
@script_src = find_script(opts.fetch(:default_script), opts[:override_script_path])
2929
@script = temp_script(@script_src)
3030
@stdout = nil
@@ -45,20 +45,21 @@ def run(opts = {})
4545
assert_directory_exists(working_dir)
4646

4747
argv = opts.fetch(:argv, [])
48+
logger = opts[:logger] || @logger
4849

4950
pass_env_vars = [opts[:pass_env_vars], 'HOME', 'PATH'].flatten.compact
5051
env = opts.select { |k, _v| k.is_a?(String) }
5152
pass_env_vars.each { |var| env[var] ||= ENV[var] }
5253
env['PWD'] = working_dir
5354

5455
cmdline = [script, argv].flatten.compact.map { |x| Shellwords.escape(x) }.join(' ')
55-
@logger.debug "Execute: #{cmdline}"
56+
log(:debug, "Execute: #{cmdline}", opts[:logger])
5657

5758
@stdout, @stderr, status = Open3.capture3(env, cmdline, unsetenv_others: true, chdir: working_dir)
5859
@exitcode = status.exitstatus
5960

60-
@stderr.split(/\n/).select { |line| line =~ /\S/ }.each { |line| @logger.debug "STDERR: #{line}" }
61-
@logger.debug "Exit status: #{@exitcode}"
61+
@stderr.split(/\n/).select { |line| line =~ /\S/ }.each { |line| log(:debug, "STDERR: #{line}", logger) }
62+
log(:debug, "Exit status: #{@exitcode}", logger)
6263
return @stdout if @exitcode.zero?
6364
raise ScriptException, output
6465
end
@@ -77,6 +78,13 @@ def output
7778

7879
private
7980

81+
# PRIVATE: Log a message, if logger is defined. Since this might be called under `parallel`
82+
# it's possible that the logger isn't defined, and if so the logged message is skipped.
83+
def log(priority, message, logger = @logger)
84+
return unless logger
85+
logger.send(priority, [message])
86+
end
87+
8088
# PRIVATE: Create a temporary file with the contents of the script and mark the script executable.
8189
# This is to avoid changing ownership or permissions on any user-supplied file.
8290
#
@@ -115,10 +123,10 @@ def find_script_from_override_path(default_script, override_script_path = nil)
115123
return unless override_script_path
116124
script_test = File.join(override_script_path, File.basename(default_script))
117125
if File.file?(script_test)
118-
logger.debug "Selecting #{script_test} from override script path"
126+
log(:debug, "Selecting #{script_test} from override script path")
119127
script_test
120128
else
121-
logger.debug "Did not find #{script_test} in override script path"
129+
log(:debug, "Did not find #{script_test} in override script path")
122130
nil
123131
end
124132
end

0 commit comments

Comments
 (0)