-
Notifications
You must be signed in to change notification settings - Fork 621
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor common Bash helper functions
- Loading branch information
1 parent
ed58bd2
commit 22a0ff3
Showing
8 changed files
with
309 additions
and
281 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
modules/nextflow/src/main/groovy/nextflow/executor/BashFunLib.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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package nextflow.executor | ||
|
||
import nextflow.util.Duration | ||
|
||
/** | ||
* Bash common functions library | ||
* | ||
* @author Paolo Di Tommaso <[email protected]> | ||
*/ | ||
class BashFunLib { | ||
|
||
static String body(int maxConnect, int maxAttempts, Duration delayBetweenAttempts) { | ||
""" | ||
# bash helper functions | ||
nxf_cp_retry() { | ||
local max_attempts=$maxAttempts | ||
local timeout=${delayBetweenAttempts.seconds} | ||
local attempt=0 | ||
local exitCode=0 | ||
while (( \$attempt < \$max_attempts )) | ||
do | ||
if "\$@" | ||
then | ||
return 0 | ||
else | ||
exitCode=\$? | ||
fi | ||
if [[ \$exitCode == 0 ]] | ||
then | ||
break | ||
fi | ||
sleep \$timeout | ||
attempt=\$(( attempt + 1 )) | ||
timeout=\$(( timeout * 2 )) | ||
done | ||
} | ||
nxf_parallel() { | ||
IFS=\$'\\n' | ||
local cmd=("\$@") | ||
local cpus=\$(nproc 2>/dev/null || < /proc/cpuinfo grep '^process' -c) | ||
local max=\$(if (( cpus>$maxConnect )); then echo $maxConnect; else echo \$cpus; fi) | ||
local i=0 | ||
local pid=() | ||
( | ||
set +u | ||
while ((i<\${#cmd[@]})); do | ||
local copy=() | ||
for x in "\${pid[@]}"; do | ||
[[ -e /proc/\$x ]] && copy+=(\$x) | ||
done | ||
pid=("\${copy[@]}") | ||
if ((\${#pid[@]}>=\$max)); then | ||
sleep 1 | ||
else | ||
eval "\${cmd[\$i]}" & | ||
pid+=(\$!) | ||
((i+=1)) | ||
fi | ||
done | ||
((\${#pid[@]}>0)) && wait \${pid[@]} | ||
) | ||
unset IFS | ||
} | ||
""".stripIndent() | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.