From d5c1ce20290fde3fe51010db2ec5055c5d77af02 Mon Sep 17 00:00:00 2001 From: Kelly Sovacool Date: Fri, 17 Nov 2023 15:24:46 -0500 Subject: [PATCH] feat: run spooker on workflow completion When a workflow completes (regardless of success or failure), check whether the spooker command is available, and if so, run it to track usage statistics --- lib/Utils.groovy | 42 ++++++++++++++++++++++++++++++++++++++++++ main.nf | 8 ++++++++ 2 files changed, 50 insertions(+) create mode 100644 lib/Utils.groovy diff --git a/lib/Utils.groovy b/lib/Utils.groovy new file mode 100644 index 0000000..3fe6464 --- /dev/null +++ b/lib/Utils.groovy @@ -0,0 +1,42 @@ +class Utils { + // run spooker for the workflow + public static String spooker(workflow) { + def pipeline_name = "${workflow.manifest.name.tokenize('/')[-1]}" + def command_string = "spooker ${workflow.launchDir} ${pipeline_name}" + def out = new StringBuilder() + def err = new StringBuilder() + def spooker_in_path = check_command_in_path("spooker") + if (spooker_in_path) { + try { + println "Running spooker" + def command = command_string.execute() + command.consumeProcessOutput(out, err) + command.waitFor() + } catch(IOException e) { + err = e + } + new FileWriter("${workflow.launchDir}/log/spooker.log").with { + write("${out}\n${err}") + flush() + } + } else { + err = "spooker not found, skipping" + } + return err + } + // check whether a command is in the path + public static Boolean check_command_in_path(cmd) { + def command_string = "command -V ${cmd}" + def out = new StringBuilder() + def err = new StringBuilder() + try { + def command = command_string.execute() + command.consumeProcessOutput(out, err) + command.waitFor() + } catch(IOException e) { + err = e + } + return err.length()==0 + + } +} diff --git a/main.nf b/main.nf index a52beec..5a3a3ee 100644 --- a/main.nf +++ b/main.nf @@ -36,6 +36,14 @@ log.info """\ """ .stripIndent() +workflow.onComplete { + if (!workflow.stubRun && !workflow.commandLine.contains('-preview')) { + def message = Utils.spooker(workflow) + if (message) { + println message + } + } +} //Final Workflow workflow {