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

Terminate script immediately (with explicit exit code) on error #1324

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Changes from 1 commit
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
24 changes: 16 additions & 8 deletions whelktool/src/main/groovy/datatool/WhelkTool.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,7 @@ class WhelkTool {
}

private void select(Iterable<Document> selection, Closure process,
int batchSize = DEFAULT_BATCH_SIZE, boolean newItems = false) {
if (errorDetected) {
log "Error detected, refusing further processing."
return
}

int batchSize = DEFAULT_BATCH_SIZE, boolean newItems = false) throws Exception {
int batchCount = 0
Batch batch = new Batch(number: ++batchCount)

Expand All @@ -303,6 +298,7 @@ class WhelkTool {
err.printStackTrace errorLog
errorLog.println "-" * 20
errorLog.flush()
errorDetected = err
}
}

Expand Down Expand Up @@ -351,6 +347,11 @@ class WhelkTool {
log()
}
loggerFuture?.cancel(true)

if (errorDetected) {
log "Error detected, refusing further processing."
throw new Exception()
}
}

private def createExecutorService() {
Expand Down Expand Up @@ -676,9 +677,12 @@ class WhelkTool {
log()

bindings = createMainBindings()
script.eval(bindings)

finish()
try {
script.eval(bindings)
} finally {
finish()
}
}

private void finish() {
Expand All @@ -687,6 +691,10 @@ class WhelkTool {
it.flush()
it.close()
}
if (errorDetected) {
log "Script terminated due to an error, see $reportsDir/ERRORS.txt for more info"
System.exit(2)
kwahlin marked this conversation as resolved.
Show resolved Hide resolved
}
log "Done!"
}

Expand Down