-
Notifications
You must be signed in to change notification settings - Fork 31
/
resume_analysis.py
executable file
·250 lines (226 loc) · 8.26 KB
/
resume_analysis.py
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#! /usr/bin/env python3
# Support resume on slurm.
# Monitors the jobs/tasks of analysis. If enabled, resume will do the following:
# 1. Determines if this job is an array job (cluster dependent)
# 2. Creates a file for each job that completes and runs without errors
# 3. If a completed/success file for a job exists, then the this script just returns
# 4. Else this scrip runs the job (typically an R job) and if it completes without
# it then touches the complete file
#
# The name of completed file associated with the job is:
# <jobname>_<task_id>.completed (for an array job)
# <jobname>.completed (for a single job)
# The script will place the job id into the completed file.
#
# There are 4 or more arguments:
# <resumescript> <cluster_type> <jobname> <job run cmd> <job run cmd arguments>
# This script basically parses out the jobrun command and all it's arguments, and then
# calls popen to execute syncrhonously. Once completed, this script checks the status and
# touches the completed file.
#
# and the sc
from __future__ import print_function
import sys
import errno
import os
import time
import subprocess
import glob
import port_popen
# init globals
fileversion = '1.0'
msgErrPrefix = '>>> Error: '
msgInfoPrefix = '>>> Info: '
debugPrefix = '>>> Vebose: '
verbose = False
class JobScheduler(object):
def __init__(self):
print("JobScheduler init ...")
self.jobInfo = {"arrayjob":None, "jobid": None, "taskid": None}
self.resumeDir = "./resume/"
def getJobInfo(self):
return self.jobInfo
def updateJobInfo(self, jinfo):
self.jobInfo = jinfo
def setEnvInfo(self, envInfo):
for key in list(envInfo.keys()):
envInfo[key] = os.getenv(key)
class SGE(JobScheduler):
def __init__(self):
super(SGE,self).__init__()
self.envInfo = {
"JOB_ID": None,
"SGE_TASK_ID": None
}
self.setJobInfo()
def setJobInfo(self):
jobInfo = super(SGE, self).getJobInfo()
super(SGE, self).setEnvInfo(self.envInfo)
# if array job, use the array job id; else job id
# check if array_job
if self.envInfo["SGE_TASK_ID"] != "undefined":
jobInfo["arrayjob"] = True
jobInfo["jobid"] = self.envInfo["JOB_ID"]
jobInfo["taskid"] = self.envInfo["SGE_TASK_ID"]
else:
jobInfo["arrayjob"] = False
if self.envInfo["JOB_ID"] != None:
jobInfo["jobid"] = self.envInfo["JOB_ID"]
else:
jobInfo["jobid"] = "NOJOBID"
super(SGE, self).updateJobInfo(jobInfo)
def createFilename(self, jName, ctag=None):
jInfo = super(SGE, self).getJobInfo()
if ctag == None:
pfile = jName + ".o" + jInfo["jobid"]
if jInfo["arrayjob"]:
pfile += "." + jInfo["taskid"]
else:
if jInfo["arrayjob"]:
pfile = ctag + "_" + jName + "_" + jInfo["taskid"]
else:
pfile = ctag + "_" + jName
return pfile
class BATCH(JobScheduler):
def __init__(self):
super(BATCH,self).__init__()
self.envInfo = {
"AWS_BATCH_JOB_ARRAY_INDEX": None,
"AWS_BATCH_JOB_ID": None
}
self.setJobInfo()
def setJobInfo(self):
jobInfo = super(BATCH, self).getJobInfo()
super(BATCH, self).setEnvInfo(self.envInfo)
# if array job, use the array job id; else job id
# check if array_job
if self.envInfo["AWS_BATCH_JOB_ARRAY_INDEX"] != None:
jobInfo["arrayjob"] = True
jobInfo["jobid"] = self.envInfo["AWS_BATCH_JOB_ID"]
jobInfo["taskid"] = self.envInfo["AWS_BATCH_JOB_ARRAY_INDEX"]
else:
jobInfo["arrayjob"] = False
if self.envInfo["AWS_BATCH_JOB_ID"] != None:
jobInfo["jobid"] = self.envInfo["AWS_BATCH_JOB_ID"]
else:
jobInfo["jobid"] = "NOJOBID"
super(BATCH, self).updateJobInfo(jobInfo)
def createFilename(self, jName, ctag=None):
jInfo = super(BATCH, self).getJobInfo()
if ctag == None:
pfile = jName + "_" + jInfo["jobid"] + ".log"
if jInfo["arrayjob"]:
pfile += "_" + jInfo["taskid"] + ".log"
else:
if jInfo["arrayjob"]:
pfile = ctag + "_" + jName + "_" + jInfo["taskid"]
else:
pfile = ctag + "_" + jName
return pfile
class SLURM(JobScheduler):
def __init__(self):
super(SLURM,self).__init__()
self.envInfo = {
"SLURM_ARRAY_TASK_ID": None,
"SLURM_ARRAY_JOB_ID": None,
"SLURM_JOB_ID": None
}
self.setJobInfo()
def setJobInfo(self):
jobInfo = super(SLURM, self).getJobInfo()
super(SLURM, self).setEnvInfo(self.envInfo)
# if array job, use the array job id; else job id
# check if array_job
if self.envInfo["SLURM_ARRAY_JOB_ID"] != None:
jobInfo["arrayjob"] = True
jobInfo["jobid"] = self.envInfo["SLURM_ARRAY_JOB_ID"]
jobInfo["taskid"] = self.envInfo["SLURM_ARRAY_TASK_ID"]
else:
jobInfo["arrayjob"] = False
if self.envInfo["SLURM_JOB_ID"] != None:
jobInfo["jobid"] = self.envInfo["SLURM_JOB_ID"]
else:
jobInfo["jobid"] = "NOJOBID"
super(SLURM, self).updateJobInfo(jobInfo)
def createFilename(self, jName, ctag=None):
jInfo = super(SLURM, self).getJobInfo()
if ctag == None:
pfile = jName + "_" + jInfo["jobid"] + ".log"
if jInfo["arrayjob"]:
pfile += "_" + jInfo["taskid"] + ".log"
else:
if jInfo["arrayjob"]:
pfile = ctag + "_" + jName + "_" + jInfo["taskid"]
else:
pfile = ctag + "_" + jName
return pfile
def pInfo_file(msg, fhandle):
tmsg=time.asctime()
print(msgInfoPrefix+tmsg+": "+msg, file=fhandle)
def pInfo(msg):
tmsg=time.asctime()
print(msgInfoPrefix+tmsg+": "+msg)
def pError(msg):
tmsg=time.asctime()
print(msgErrPrefix+tmsg+": "+msg)
def pDebug(msg):
if verbose:
tmsg=time.asctime()
print(debugPrefix+tmsg+": "+msg)
def Summary(hdr):
print(hdr)
print("\tVersion: " + fileversion)
# get the args
minArgs = 4
if (len(sys.argv) < minArgs):
pError("There must be at least " + str(minArgs) + " arguments provided to this script.")
sys.exit(2)
sName = sys.argv[0]
cName = sys.argv[1]
jName = sys.argv[2]
# set the cmd
jCmd = sys.argv[3]
if (len(sys.argv) > minArgs):
# must iterate over the arguments in case an argument contains a space
jArgs = ""
for a in sys.argv[minArgs:len(sys.argv)]:
if a.find(' ') == -1:
jArgs += ' ' + a
else:
jArgs += " '" + a + "'"
jCmd += ' ' + jArgs
# check and instantiate cluster class
clusters = ['SLURM', 'BATCH', 'SGE']
if cName.upper() not in clusters:
pError(sName + "- Error: invalid cluster type " + cName)
sys.exit(2)
clusterclass = globals()[cName.upper()]
thecluster = clusterclass()
pInfo("Script: " + sName)
pInfo("Job: " + jName)
pInfo("Cmd: " + jCmd)
# get the job info
jInfo = thecluster.getJobInfo()
jId = jInfo["jobid"]
# set the file name
ctag = "completed"
cfname = thecluster.resumeDir + thecluster.createFilename(jName, ctag)
lfname = thecluster.createFilename(jName)
status = 0
if os.path.isfile(cfname):
pInfo("Job " + jName + " already completed (" + cfname +")")
else:
pInfo("Calling Popen to execute: \n\t" + jCmd)
(pmsg, status) = port_popen.popen_stdout(jCmd, logfile=lfname, jobname=jName)
# if status == 0, write a complete file unless it's the post_analysis
# open the file and write out job info (name, id, task)
if status == 0 and "post_analysis" not in jName:
with open(cfname, mode='w') as fhandle:
msgC = "job: " + jName + ", job id: " + jId
if jInfo["arrayjob"]:
pInfo_file(msgC + " task: " + jInfo["taskid"] + " completed", fhandle)
else:
pInfo_file(msgC + " completed", fhandle)
else:
pError("Status: " + str(status) + " See " + lfname + " for details")
sys.exit(status)