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

Fix workflow onComplete and onError in the entry workflow #5366

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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 @@ -303,7 +303,10 @@ class WorkflowMetadata {
* @param action The action handler
*/
void onComplete( Closure action ) {
onCompleteActions.add(action)
}

void setOnCompleteFormConfig( Closure action ) {
final clone = (Closure)action.clone()
clone.delegate = NF.binding.variables
clone.resolveStrategy = Closure.DELEGATE_FIRST
Expand Down Expand Up @@ -335,7 +338,10 @@ class WorkflowMetadata {
* @param action
*/
void onError( Closure action ) {
onErrorActions.add(action)
}

void setOnErrorFromConfig(Closure action ) {
final clone = (Closure)action.clone()
clone.delegate = NF.binding.variables
clone.resolveStrategy = Closure.DELEGATE_FIRST
Expand Down Expand Up @@ -376,11 +382,11 @@ class WorkflowMetadata {
if( !workflowConfig ) return
// -- register `onComplete`
if( workflowConfig.onComplete instanceof Closure ) {
onComplete( (Closure)workflowConfig.onComplete )
setOnCompleteFormConfig( (Closure)workflowConfig.onComplete )
}
// -- register `onError`
if( workflowConfig.onError instanceof Closure ) {
onError( (Closure)workflowConfig.onError )
setOnErrorFromConfig( (Closure)workflowConfig.onError )
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class WorkflowMetadataTest extends Specification {
}

when:
metadata.onComplete(handler)
metadata.setOnCompleteFormConfig(handler)
metadata.invokeOnComplete()

then:
Expand All @@ -190,7 +190,7 @@ class WorkflowMetadataTest extends Specification {
def metadata = new WorkflowMetadata(session, null)

when:
metadata.onComplete {
metadata.setOnCompleteFormConfig {
throw new WorkflowScriptErrorException('You failed!')
}
metadata.invokeOnComplete()
Expand Down Expand Up @@ -236,7 +236,7 @@ class WorkflowMetadataTest extends Specification {
}

when:
metadata.onError(handler)
metadata.setOnErrorFromConfig(handler)
metadata.invokeOnError(Mock(TraceRecord))

then:
Expand Down
21 changes: 21 additions & 0 deletions tests/checks/script-vars-oncomplete.nf/.checks
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
set -e

#
# run normal mode
#
echo ''
$NXF_RUN | tee stdout

[[ `grep -c 'Workflow Executed!' stdout` == 1 ]] || false
[[ `grep -c 'local variable: local' stdout` == 1 ]] || false
[[ `grep -c 'param output dir: results' stdout` == 1 ]] || false
[[ `grep -c 'Method executed!' stdout` == 1 ]] || false
[[ `grep -c 'script-vars-oncomplete.nf' stdout` == 1 ]] || false
[[ `grep -c "Can't access to a global variable" stdout` == 1 ]] || false
[[ `grep -c "Can't access to a private workflow property" stdout` == 1 ]] || false
[[ `grep -c 'Success!' stdout` == 1 ]] || false





21 changes: 21 additions & 0 deletions tests/checks/script-vars-onerror.nf/.checks
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
set -e

#
# run normal mode
#
echo ''
$NXF_RUN | tee stdout

[[ `grep -c 'Workflow Failed!' stdout` == 1 ]] || false
[[ `grep -c 'local variable: local' stdout` == 1 ]] || false
[[ `grep -c 'param output dir: results' stdout` == 1 ]] || false
[[ `grep -c 'Method executed!' stdout` == 1 ]] || false
[[ `grep 'Executed script' stdout | grep -c 'script-vars-onerror.nf'` == 1 ]] || false
[[ `grep -c "Can't access to a global variable" stdout` == 1 ]] || false
[[ `grep -c "Can't access to a private workflow property" stdout` == 1 ]] || false
[[ `grep -c 'Failure!' stdout` == 1 ]] || false





35 changes: 35 additions & 0 deletions tests/script-vars-oncomplete.nf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
def global_var = 'global'

def method() {
println("Method executed!")
}

workflow {
params.outdir = 'results'

def local = 'local'

workflow.onComplete {
println("local variable: ${local}")
println("param output dir: ${params.outdir}")
method()
println("Executed script : ${workflow.scriptFile}")
// expected failure
try {
println("global variable: ${global}")
}catch (groovy.lang.MissingPropertyException e){
println("Can't access to a global variable")
}
try {
println("workflow private variable: ${events}")
}catch (Exception e){
println("Can't access to a private workflow property")
}
if( workflow.success )
println("Success!")
else
println("Failure!")
}

println("Workflow Executed!")
}
35 changes: 35 additions & 0 deletions tests/script-vars-onerror.nf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
def global_var = 'global'

def method() {
println("Method executed!")
}

workflow {
params.outdir = 'results'

def local = 'local'

workflow.onError {
println("local variable: ${local}")
println("param output dir: ${params.outdir}")
method()
println("Executed script : ${workflow.scriptFile}")
// expected failures
try {
println("global variable: ${global}")
}catch (groovy.lang.MissingPropertyException e){
println("Can't access to a global variable")
}
try {
println("workflow private variable: ${events}")
}catch (Exception e){
println("Can't access to a private workflow property")
}
if( workflow.success )
println("Success!")
else
println("Failure!")
}

throw new Exception("Workflow Failed!")
}
Loading