-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster_submit.py
176 lines (140 loc) · 5.83 KB
/
cluster_submit.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
import argparse
import math
import os
import os.path
import subprocess
import sys
import time
from snakemake.utils import read_job_properties
import cluster_commands
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('jobscript',
help='the script to be executed on the cluster')
parser.add_argument(
'--retry-submit-interval-seconds',
default='',
help='a "," separated list of integers representing'
' the number of seconds to wait after sequential failed'
' job submission commands before retrying')
parser.add_argument(
'--resource-usage-dir',
help='a directory for storing the file paths where cluster_status.py'
' should log the resource usage of each job')
args = parser.parse_args()
jobscript = args.jobscript
job_properties = read_job_properties(jobscript)
retry_submit_interval_seconds = list()
for int_str in args.retry_submit_interval_seconds.split(','):
retry_submit_interval_seconds.append(int(int_str))
resource_usage_dir = args.resource_usage_dir
if resource_usage_dir:
os.makedirs(resource_usage_dir, exist_ok=True)
return {
'jobscript': jobscript,
'job_properties': job_properties,
'retry_submit_interval_seconds': retry_submit_interval_seconds,
'resource_usage_dir': resource_usage_dir,
}
def get_base_path_from_jobscript(jobscript):
with open(jobscript) as f_handle:
for line in f_handle:
tokens = line.split()
if len(tokens) == 4:
if ((tokens[0] == 'cd' and tokens[2] == '&&'
and tokens[3] == '\\')):
base_path = tokens[1]
if os.path.isdir(base_path):
return os.path.abspath(base_path)
return None
def get_cluster_log_paths(jobscript, job_properties):
cluster_logs = {'out': os.devnull, 'err': os.devnull, 'usage': os.devnull}
base_path = get_base_path_from_jobscript(jobscript)
orig_logs = job_properties.get('log')
if (not base_path) or (not orig_logs):
return cluster_logs
if len(orig_logs) == 1:
orig_log_out = orig_logs[0]
orig_log_err = orig_log_out
else:
orig_log_out = orig_logs[0]
orig_log_err = orig_logs[1]
cluster_logs['out'] = os.path.join(base_path,
'{}.cluster.out'.format(orig_log_out))
cluster_logs['err'] = os.path.join(base_path,
'{}.cluster.err'.format(orig_log_err))
cluster_logs['usage'] = os.path.join(
base_path, '{}.cluster.usage'.format(orig_log_out))
return cluster_logs
def build_submit_command(jobscript, job_properties, cluster_log_out,
cluster_log_err):
threads = job_properties.get('threads')
resources = job_properties.get('resources')
time_hours = None
mem_mb = None
mem_mb_per_thread = None
if resources:
time_hours = resources.get('time_hours')
mem_mb = resources.get('mem_mb')
mem_mb_per_thread = mem_mb
if mem_mb and threads:
mem_mb_per_thread /= float(threads)
mem_mb_per_thread = math.ceil(mem_mb_per_thread)
return cluster_commands.submit_command(cluster_log_out, cluster_log_err,
threads, time_hours, mem_mb,
mem_mb_per_thread, jobscript)
def try_run_submit_command(command):
completed_process = subprocess.run(command,
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
decoded_stdout = completed_process.stdout.decode()
decoded_stderr = completed_process.stderr.decode()
if completed_process.returncode != 0:
return None, 'stdout:\n{}\n\nstderr:\n{}'.format(
decoded_stdout, decoded_stderr)
return decoded_stdout, None
def run_submit_command(command, retry_submit_interval_seconds):
errors = list()
# The final (retry_seconds: None) allows running the command, but
# without the ability to wait and retry.
retry_submit_interval_seconds = retry_submit_interval_seconds + [None]
for retry_seconds in retry_submit_interval_seconds:
stdout, error = try_run_submit_command(command)
if not error:
return stdout
errors.append(error)
if retry_seconds is not None:
time.sleep(retry_seconds)
continue
else:
break
for i, error in enumerate(errors):
print('attempt {}\n{}'.format(i, error), file=sys.stderr)
sys.exit(1)
def extract_job_id(stdout):
job_id, error = cluster_commands.try_extract_job_id_from_submit_output(
stdout)
if error:
print('error: {}\n{}'.format(error, stdout), file=sys.stderr)
sys.exit(1)
return job_id
def record_usage_file(job_id, cluster_log_usage, resource_usage_dir):
job_file_path = os.path.join(resource_usage_dir, '{}.txt'.format(job_id))
with open(job_file_path, 'wt') as f_handle:
f_handle.write('{}\n'.format(cluster_log_usage))
def main():
parsed_args = parse_args()
jobscript = parsed_args['jobscript']
job_properties = parsed_args['job_properties']
cluster_logs = get_cluster_log_paths(jobscript, job_properties)
command = build_submit_command(jobscript, job_properties,
cluster_logs['out'], cluster_logs['err'])
stdout = run_submit_command(command,
parsed_args['retry_submit_interval_seconds'])
job_id = extract_job_id(stdout)
record_usage_file(job_id, cluster_logs['usage'],
parsed_args['resource_usage_dir'])
print(job_id)
if __name__ == '__main__':
main()