-
Notifications
You must be signed in to change notification settings - Fork 831
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
run multiple scripts in the same GroovyScriptEngine #782
Closed
Closed
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,79 +12,114 @@ import java.util.logging.Logger | |
* Runs provided DSL scripts via an external {@link JobManagement}. | ||
*/ | ||
class DslScriptLoader { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(DslScriptLoader.name) | ||
private static final Comparator<? super Item> ITEM_COMPARATOR = new ItemProcessingOrderComparator() | ||
|
||
private static GeneratedItems runDslEngineForParent(ScriptRequest scriptRequest, | ||
JobManagement jobManagement) throws IOException { | ||
PrintStream logger = jobManagement.outputStream | ||
private final JobManagement jobManagement | ||
private final PrintStream logger | ||
|
||
/** | ||
* For testing a string directly. | ||
*/ | ||
static GeneratedItems runDslEngine(String scriptBody, JobManagement jobManagement) throws IOException { | ||
ScriptRequest scriptRequest = new ScriptRequest(null, scriptBody, new File('.').toURI().toURL()) | ||
runDslEngine(scriptRequest, jobManagement) | ||
} | ||
|
||
static GeneratedItems runDslEngine(ScriptRequest scriptRequest, JobManagement jobManagement) throws IOException { | ||
DslScriptLoader loader = new DslScriptLoader(jobManagement) | ||
loader.runScripts([scriptRequest]) | ||
} | ||
|
||
DslScriptLoader(JobManagement jobManagement) { | ||
this.jobManagement = jobManagement | ||
this.logger = jobManagement.outputStream | ||
} | ||
|
||
GeneratedItems runScripts(Collection<ScriptRequest> scriptRequests) throws IOException { | ||
ClassLoader parentClassLoader = DslScriptLoader.classLoader | ||
CompilerConfiguration config = createCompilerConfiguration(jobManagement) | ||
|
||
// Otherwise baseScript won't take effect | ||
GroovyClassLoader groovyClassLoader = new GroovyClassLoader(parentClassLoader, config) | ||
|
||
try { | ||
GroovyScriptEngine engine = new GroovyScriptEngine(scriptRequest.urlRoots, groovyClassLoader) | ||
runScriptsWithClassLoader(scriptRequests, groovyClassLoader, config) | ||
} finally { | ||
if (groovyClassLoader instanceof Closeable) { | ||
((Closeable) groovyClassLoader).close() | ||
} | ||
} | ||
} | ||
|
||
private GeneratedItems runScriptsWithClassLoader(Collection<ScriptRequest> scriptRequests, | ||
GroovyClassLoader groovyClassLoader, | ||
CompilerConfiguration config) { | ||
GeneratedItems generatedItems = new GeneratedItems() | ||
|
||
// group requests that share the same classpath | ||
scriptRequests.groupBy { it.urlRoots*.toString().sort() }.values().each { List<ScriptRequest> requestSet -> | ||
GroovyScriptEngine engine | ||
try { | ||
engine = new GroovyScriptEngine(requestSet.first().urlRoots, groovyClassLoader) | ||
engine.config = config | ||
|
||
Binding binding = createBinding(jobManagement) | ||
|
||
JobParent jobParent | ||
try { | ||
Script script | ||
if (scriptRequest.body != null) { | ||
logger.println('Processing provided DSL script') | ||
Class cls = engine.groovyClassLoader.parseClass(scriptRequest.body, 'script') | ||
script = InvokerHelper.createScript(cls, binding) | ||
} else { | ||
logger.println("Processing DSL script ${scriptRequest.location}") | ||
if (!isValidScriptName(scriptRequest.location)) { | ||
throw new DslException( | ||
"invalid script name '${scriptRequest.location}; script names may only contain " + | ||
'letters, digits and underscores, but may not start with a digit' | ||
) | ||
} | ||
checkCollidingScriptName(scriptRequest.location, engine.groovyClassLoader, logger) | ||
script = engine.createScript(scriptRequest.location, binding) | ||
} | ||
assert script instanceof JobParent | ||
|
||
jobParent = (JobParent) script | ||
jobParent.setJm(jobManagement) | ||
|
||
binding.setVariable('jobFactory', jobParent) | ||
|
||
script.run() | ||
} catch (CompilationFailedException e) { | ||
throw new DslException(e.message, e) | ||
} catch (GroovyRuntimeException e) { | ||
throw new DslScriptException(e.message, e) | ||
} catch (ResourceException e) { | ||
throw new IOException('Unable to run script', e) | ||
} catch (ScriptException e) { | ||
throw new IOException('Unable to run script', e) | ||
} | ||
requestSet.each { ScriptRequest scriptRequest -> | ||
JobParent jobParent = runScript(scriptRequest, engine) | ||
|
||
GeneratedItems generatedItems = new GeneratedItems() | ||
generatedItems.configFiles = extractGeneratedConfigFiles(jobParent, scriptRequest.ignoreExisting) | ||
generatedItems.jobs = extractGeneratedJobs(jobParent, scriptRequest.ignoreExisting) | ||
generatedItems.views = extractGeneratedViews(jobParent, scriptRequest.ignoreExisting) | ||
generatedItems.userContents = extractGeneratedUserContents(jobParent, scriptRequest.ignoreExisting) | ||
boolean ignoreExisting = scriptRequest.ignoreExisting | ||
generatedItems.configFiles.addAll(extractGeneratedConfigFiles(jobParent, ignoreExisting)) | ||
generatedItems.jobs.addAll(extractGeneratedJobs(jobParent, ignoreExisting)) | ||
generatedItems.views.addAll(extractGeneratedViews(jobParent, ignoreExisting)) | ||
generatedItems.userContents.addAll(extractGeneratedUserContents(jobParent, ignoreExisting)) | ||
|
||
scheduleJobsToRun(jobParent.queueToBuild, jobManagement) | ||
scheduleJobsToRun(jobParent.queueToBuild) | ||
} | ||
|
||
return generatedItems | ||
} finally { | ||
if (engine.groovyClassLoader instanceof Closeable) { | ||
if (engine?.groovyClassLoader instanceof Closeable) { | ||
((Closeable) engine.groovyClassLoader).close() | ||
} | ||
} | ||
} finally { | ||
if (groovyClassLoader instanceof Closeable) { | ||
((Closeable) groovyClassLoader).close() | ||
} | ||
generatedItems | ||
} | ||
|
||
private JobParent runScript(ScriptRequest scriptRequest, GroovyScriptEngine engine) { | ||
LOGGER.log(Level.FINE, String.format("Request for ${scriptRequest.location}")) | ||
|
||
Binding binding = createBinding() | ||
try { | ||
Script script | ||
if (scriptRequest.body != null) { | ||
logger.println('Processing provided DSL script') | ||
Class cls = engine.groovyClassLoader.parseClass(scriptRequest.body, 'script') | ||
script = InvokerHelper.createScript(cls, binding) | ||
} else { | ||
logger.println("Processing DSL script ${scriptRequest.location}") | ||
checkValidScriptName(scriptRequest.location) | ||
checkCollidingScriptName(scriptRequest.location, engine.groovyClassLoader, logger) | ||
script = engine.createScript(scriptRequest.location, binding) | ||
} | ||
assert script instanceof JobParent | ||
|
||
JobParent jobParent = (JobParent) script | ||
jobParent.setJm(jobManagement) | ||
|
||
binding.setVariable('jobFactory', jobParent) | ||
|
||
script.run() | ||
|
||
return jobParent | ||
} catch (CompilationFailedException e) { | ||
throw new DslException(e.message, e) | ||
} catch (GroovyRuntimeException e) { | ||
throw new DslScriptException(e.message, e) | ||
} catch (ResourceException e) { | ||
throw new IOException('Unable to run script', e) | ||
} catch (ScriptException e) { | ||
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. It's good that |
||
throw new IOException('Unable to run script', e) | ||
} | ||
} | ||
|
||
|
@@ -101,6 +136,15 @@ class DslScriptLoader { | |
true | ||
} | ||
|
||
private static void checkValidScriptName(String scriptName) { | ||
if (!isValidScriptName(scriptName)) { | ||
throw new DslException( | ||
"invalid script name '${scriptName}; script names may only contain " + | ||
'letters, digits and underscores, but may not start with a digit' | ||
) | ||
} | ||
} | ||
|
||
private static void checkCollidingScriptName(String scriptFile, ClassLoader classLoader, PrintStream logger) { | ||
String scriptName = getScriptName(scriptFile) | ||
Package[] packages = new SnitchingClassLoader(classLoader).packages | ||
|
@@ -118,18 +162,6 @@ class DslScriptLoader { | |
idx > -1 ? fileName[0..idx - 1] : fileName | ||
} | ||
|
||
/** | ||
* For testing a string directly. | ||
*/ | ||
static GeneratedItems runDslEngine(String scriptBody, JobManagement jobManagement) throws IOException { | ||
ScriptRequest scriptRequest = new ScriptRequest(null, scriptBody, new File('.').toURI().toURL()) | ||
runDslEngine(scriptRequest, jobManagement) | ||
} | ||
|
||
static GeneratedItems runDslEngine(ScriptRequest scriptRequest, JobManagement jobManagement) throws IOException { | ||
runDslEngineForParent(scriptRequest, jobManagement) | ||
} | ||
|
||
private static Set<GeneratedJob> extractGeneratedJobs(JobParent jobParent, | ||
boolean ignoreExisting) throws IOException { | ||
// Iterate jobs which were setup, save them, and convert to a serializable form | ||
|
@@ -186,7 +218,7 @@ class DslScriptLoader { | |
} | ||
|
||
@SuppressWarnings('CatchException') | ||
static void scheduleJobsToRun(List<String> jobNames, JobManagement jobManagement) { | ||
private void scheduleJobsToRun(List<String> jobNames) { | ||
Map<String, Throwable> exceptions = [:] | ||
jobNames.each { String jobName -> | ||
try { | ||
|
@@ -203,7 +235,7 @@ class DslScriptLoader { | |
} | ||
} | ||
|
||
private static Binding createBinding(JobManagement jobManagement) { | ||
private Binding createBinding() { | ||
Binding binding = new Binding() | ||
binding.setVariable('out', jobManagement.outputStream) // Works for println, but not System.out | ||
|
||
|
8 changes: 4 additions & 4 deletions
8
job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/GeneratedItems.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
package javaposse.jobdsl.dsl | ||
|
||
class GeneratedItems { | ||
Set<GeneratedJob> jobs | ||
Set<GeneratedView> views | ||
Set<GeneratedConfigFile> configFiles | ||
Set<GeneratedUserContent> userContents | ||
Set<GeneratedJob> jobs = [] | ||
Set<GeneratedView> views = [] | ||
Set<GeneratedConfigFile> configFiles = [] | ||
Set<GeneratedUserContent> userContents = [] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Hm, this introduces another incompatibility. Users may rely on the script execution order, e.g. they have one script generating some folders and then other scripts in sub directories generating the job in these folders. If we change the execution order, it's no longer guaranteed that the folders are generated before the jobs. See JENKINS-33081.
How about iterating over the script requests and creating a new script engine when the classpath changes? When using
jobs/*.groovy
this would still be efficient, but when using weird script locations it would still be compatible.