Skip to content
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
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Copy link
Member

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.

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good that DslScriptLoader no longer relies on static methods, but do not move any methods. Leave these where they are and mark them as deprecated. Then we can remove them eventually.

throw new IOException('Unable to run script', e)
}
}

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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

Expand Down
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 = []
}
Loading