@@ -13,18 +13,20 @@ class ScriptRunner
13
13
# For an exception running the script
14
14
class ScriptException < RuntimeError ; end
15
15
16
- attr_reader :script , :script_src , :logger , :stdout , :stderr , :exitcode
16
+ attr_reader :script , :script_src , :logger , :stdout , :stderr , :exitcode , :existing_tempdir
17
17
18
18
# Create the object - the object is a configured script, which can be executed multiple
19
19
# times with different environment varibles.
20
20
#
21
21
# @param opts [Hash] Options hash
22
22
# opts[:default_script] (Required) Path to script, relative to `scripts` directory
23
23
# opts[:logger] (Optional) Logger object
24
+ # opts[:existing_tempdir] (Optional) An existing temporary directory (helpful when parallelizing)
24
25
# opts[:override_script_path] (Optional) Directory where a similarly-named script MAY exist
25
26
def initialize ( opts = { } )
26
27
@logger = opts [ :logger ]
27
28
@script_src = find_script ( opts . fetch ( :default_script ) , opts [ :override_script_path ] )
29
+ @existing_tempdir = opts [ :existing_tempdir ]
28
30
@script = temp_script ( @script_src )
29
31
@stdout = nil
30
32
@stderr = nil
@@ -91,14 +93,20 @@ def log(priority, message, logger = @logger)
91
93
# @return [String] Path to tempfile containing script
92
94
def temp_script ( script )
93
95
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
100
106
end
107
+ temp_dir_local
101
108
end
109
+
102
110
temp_file = File . join ( temp_dir , File . basename ( script ) )
103
111
File . open ( temp_file , 'w' ) { |f | f . write ( File . read ( script ) ) }
104
112
FileUtils . chmod 0o755 , temp_file
0 commit comments