/
SlurmExecutor.groovy
177 lines (147 loc) · 5.91 KB
/
SlurmExecutor.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Copyright 2013-2019, Centre for Genomic Regulation (CRG)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nextflow.executor
import java.nio.file.Path
import java.util.regex.Pattern
import groovy.util.logging.Slf4j
import nextflow.processor.TaskRun
/**
* Processor for SLURM resource manager (DRAFT)
*
* See http://computing.llnl.gov/linux/slurm/
*
*
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
*/
@Slf4j
class SlurmExecutor extends AbstractGridExecutor {
static private Pattern SUBMIT_REGEX = ~/Submitted batch job (\d+)/
/**
* Gets the directives to submit the specified task to the cluster for execution
*
* @param task A {@link TaskRun} to be submitted
* @param result The {@link List} instance to which add the job directives
* @return A {@link List} containing all directive tokens and values.
*/
protected List<String> getDirectives(TaskRun task, List<String> result) {
result << '-D' << quote(task.workDir)
result << '-J' << getJobNameFor(task)
result << '-o' << quote(task.workDir.resolve(TaskRun.CMD_LOG)) // -o OUTFILE and no -e option => stdout and stderr merged to stdout/OUTFILE
result << '--no-requeue' << '' // note: directive need to be returned as pairs
if( task.config.cpus > 1 ) {
result << '-c' << task.config.cpus.toString()
}
if( task.config.time ) {
result << '-t' << task.config.getTime().format('HH:mm:ss')
}
if( task.config.getMemory() ) {
//NOTE: Enforcement of memory limits currently relies upon the task/cgroup plugin or
// enabling of accounting, which samples memory use on a periodic basis (data need not
// be stored, just collected). In both cases memory use is based upon the job's
// Resident Set Size (RSS). A task may exceed the memory limit until the next periodic
// accounting sample. -- https://slurm.schedmd.com/sbatch.html
result << '--mem' << task.config.getMemory().toMega().toString() + 'M'
}
// the requested partition (a.k.a queue) name
if( task.config.queue ) {
result << '-p' << (task.config.queue.toString())
}
// -- at the end append the command script wrapped file name
if( task.config.clusterOptions ) {
result << task.config.clusterOptions.toString() << ''
}
return result
}
String getHeaderToken() { '#SBATCH' }
/**
* The command line to submit this job
*
* @param task The {@link TaskRun} instance to submit for execution to the cluster
* @param scriptFile The file containing the job launcher script
* @return A list representing the submit command line
*/
@Override
List<String> getSubmitCommandLine(TaskRun task, Path scriptFile ) {
['sbatch', scriptFile.getName()]
}
/**
* Parse the string returned by the {@code sbatch} command and extract the job ID string
*
* @param text The string returned when submitting the job
* @return The actual job ID string
*/
@Override
def parseJobId(String text) {
for( String line : text.readLines() ) {
def m = SUBMIT_REGEX.matcher(line)
if( m.find() ) {
return m.group(1).toString()
}
}
// customised `sbatch` command can return only the jobid
def id = text.trim()
if( id.isLong() )
return id
throw new IllegalStateException("Invalid SLURM submit response:\n$text\n\n")
}
@Override
protected List<String> getKillCommand() { ['scancel'] }
@Override
protected List<String> queueStatusCommand(Object queue) {
final result = ['squeue','--noheader','-o','%i %t', '-t', 'all']
if( queue )
result << '-p' << queue.toString()
final user = System.getProperty('user.name')
if( user )
result << '-u' << user
else
log.debug "Cannot retrieve current user"
return result
}
/*
* Maps SLURM job status to nextflow status
* see http://slurm.schedmd.com/squeue.html#SECTION_JOB-STATE-CODES
*/
static private Map STATUS_MAP = [
'PD': QueueStatus.PENDING, // (pending)
'R': QueueStatus.RUNNING, // (running)
'CA': QueueStatus.ERROR, // (cancelled)
'CF': QueueStatus.PENDING, // (configuring)
'CG': QueueStatus.RUNNING, // (completing)
'CD': QueueStatus.DONE, // (completed)
'F': QueueStatus.ERROR, // (failed),
'TO': QueueStatus.ERROR, // (timeout),
'NF': QueueStatus.ERROR, // (node failure)
'S': QueueStatus.HOLD, // (job suspended)
'ST': QueueStatus.HOLD, // (stopped)
'PR': QueueStatus.ERROR, // (Job terminated due to preemption)
'BF': QueueStatus.ERROR, // (boot fail, Job terminated due to launch failure)
]
@Override
protected Map<String, QueueStatus> parseQueueStatus(String text) {
def result = [:]
text.eachLine { String line ->
def cols = line.split(/\s+/)
if( cols.size() == 2 ) {
result.put( cols[0], STATUS_MAP.get(cols[1]) )
}
else {
log.debug "[SLURM] invalid status line: `$line`"
}
}
return result
}
}