@@ -13,18 +13,20 @@ class ScriptRunner
1313 # For an exception running the script
1414 class ScriptException < RuntimeError ; end
1515
16- attr_reader :script , :script_src , :logger , :stdout , :stderr , :exitcode
16+ attr_reader :script , :script_src , :logger , :stdout , :stderr , :exitcode , :existing_tempdir
1717
1818 # Create the object - the object is a configured script, which can be executed multiple
1919 # times with different environment varibles.
2020 #
2121 # @param opts [Hash] Options hash
2222 # opts[:default_script] (Required) Path to script, relative to `scripts` directory
2323 # opts[:logger] (Optional) Logger object
24+ # opts[:existing_tempdir] (Optional) An existing temporary directory (helpful when parallelizing)
2425 # opts[:override_script_path] (Optional) Directory where a similarly-named script MAY exist
2526 def initialize ( opts = { } )
2627 @logger = opts [ :logger ]
2728 @script_src = find_script ( opts . fetch ( :default_script ) , opts [ :override_script_path ] )
29+ @existing_tempdir = opts [ :existing_tempdir ]
2830 @script = temp_script ( @script_src )
2931 @stdout = nil
3032 @stderr = nil
@@ -91,14 +93,20 @@ def log(priority, message, logger = @logger)
9193 # @return [String] Path to tempfile containing script
9294 def temp_script ( script )
9395 raise Errno ::ENOENT , "Script '#{ script } ' not found" unless File . file? ( script )
94- temp_dir = Dir . mktmpdir ( 'ocd-scriptrunner-' )
95- at_exit do
96- begin
97- FileUtils . remove_entry_secure temp_dir
98- rescue Errno ::ENOENT # rubocop:disable Lint/HandleExceptions
99- # OK if the directory doesn't exist since we're trying to remove it anyway
96+ temp_dir = if existing_tempdir
97+ Dir . mktmpdir ( 'ocd-scriptrunner-' , existing_tempdir )
98+ else
99+ temp_dir_local = Dir . mktmpdir ( 'ocd-scriptrunner-' )
100+ at_exit do
101+ begin
102+ FileUtils . remove_entry_secure temp_dir_local
103+ rescue Errno ::ENOENT # rubocop:disable Lint/HandleExceptions
104+ # OK if the directory doesn't exist since we're trying to remove it anyway
105+ end
100106 end
107+ temp_dir_local
101108 end
109+
102110 temp_file = File . join ( temp_dir , File . basename ( script ) )
103111 File . open ( temp_file , 'w' ) { |f | f . write ( File . read ( script ) ) }
104112 FileUtils . chmod 0o755 , temp_file
0 commit comments