-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
cwl_flask.py
151 lines (126 loc) · 4.05 KB
/
cwl_flask.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
from flask import Flask, Response, request, redirect
import subprocess
import tempfile
import json
import yaml
import signal
import threading
import time
import copy
app = Flask(__name__)
jobs_lock = threading.Lock()
jobs = []
class Job(threading.Thread):
def __init__(self, jobid, path, inputobj):
super().__init__()
self.jobid = jobid
self.path = path
self.inputobj = inputobj
self.updatelock = threading.Lock()
self.begin()
def begin(self):
loghandle, self.logname = tempfile.mkstemp()
with self.updatelock:
self.outdir = tempfile.mkdtemp()
self.proc = subprocess.Popen(
["cwl-runner", self.path, "-"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=loghandle,
close_fds=True,
cwd=self.outdir,
)
self.status = {
"id": "%sjobs/%i" % (request.url_root, self.jobid),
"log": "%sjobs/%i/log" % (request.url_root, self.jobid),
"run": self.path,
"state": "Running",
"input": json.loads(self.inputobj),
"output": None,
}
def run(self):
self.stdoutdata, self.stderrdata = self.proc.communicate(self.inputobj)
if self.proc.returncode == 0:
outobj = yaml.load(self.stdoutdata, Loader=yaml.FullLoader)
with self.updatelock:
self.status["state"] = "Success"
self.status["output"] = outobj
else:
with self.updatelock:
self.status["state"] = "Failed"
def getstatus(self):
with self.updatelock:
return self.status.copy()
def cancel(self):
if self.status["state"] == "Running":
self.proc.send_signal(signal.SIGQUIT)
with self.updatelock:
self.status["state"] = "Canceled"
def pause(self):
if self.status["state"] == "Running":
self.proc.send_signal(signal.SIGTSTP)
with self.updatelock:
self.status["state"] = "Paused"
def resume(self):
if self.status["state"] == "Paused":
self.proc.send_signal(signal.SIGCONT)
with self.updatelock:
self.status["state"] = "Running"
@app.route("/run", methods=["POST"])
def runworkflow():
path = request.args["wf"]
with jobs_lock:
jobid = len(jobs)
job = Job(jobid, path, request.stream.read())
job.start()
jobs.append(job)
return redirect("/jobs/%i" % jobid, code=303)
@app.route("/jobs/<int:jobid>", methods=["GET", "POST"])
def jobcontrol(jobid):
with jobs_lock:
job = jobs[jobid]
if request.method == "POST":
action = request.args.get("action")
if action:
if action == "cancel":
job.cancel()
elif action == "pause":
job.pause()
elif action == "resume":
job.resume()
status = job.getstatus()
return json.dumps(status, indent=4), 200, ""
def logspooler(job):
with open(job.logname) as f:
while True:
r = f.read(4096)
if r:
yield r
else:
with job.updatelock:
if job.status["state"] != "Running":
break
time.sleep(1)
@app.route("/jobs/<int:jobid>/log", methods=["GET"])
def getlog(jobid):
with jobs_lock:
job = jobs[jobid]
return Response(logspooler(job))
@app.route("/jobs", methods=["GET"])
def getjobs():
with jobs_lock:
jobscopy = copy.copy(jobs)
def spool(jc):
yield "["
first = True
for j in jc:
if first:
yield json.dumps(j.getstatus(), indent=4)
first = False
else:
yield ", " + json.dumps(j.getstatus(), indent=4)
yield "]"
return Response(spool(jobscopy))
if __name__ == "__main__":
# app.debug = True
app.run()