-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcwl_flask.py
172 lines (144 loc) · 5.08 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""Simple webapp for running cwl-runner."""
import copy
import json
import shutil
import signal
import subprocess
import tempfile
import threading
import time
from collections.abc import Generator
from typing import Any
import werkzeug.wrappers.response
import yaml
from flask import Flask, Response, redirect, request
app = Flask(__name__)
jobs_lock = threading.Lock()
jobs: list["Job"] = []
class Job(threading.Thread):
"""cwl-runner webapp."""
def __init__(self, jobid: int, path: str, inputobj: bytes) -> None:
"""Initialize the execution Job."""
super().__init__()
self.jobid = jobid
self.path = path
self.inputobj = inputobj
self.updatelock = threading.Lock()
self.begin()
def begin(self) -> None:
"""Star executing using cwl-runner."""
loghandle, self.logname = tempfile.mkstemp()
with self.updatelock:
self.outdir = tempfile.mkdtemp()
self.proc = subprocess.Popen(
[shutil.which("cwl-runner") or "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) -> None:
"""Wait for execution to finish and report the result."""
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) -> dict[str, Any]:
"""Report the current status."""
with self.updatelock:
return self.status.copy()
def cancel(self) -> None:
"""Cancel the excution thread, if any."""
if self.status["state"] == "Running":
self.proc.send_signal(signal.SIGQUIT)
with self.updatelock:
self.status["state"] = "Canceled"
def pause(self) -> None:
"""Pause the execution thread, if any."""
if self.status["state"] == "Running":
self.proc.send_signal(signal.SIGTSTP)
with self.updatelock:
self.status["state"] = "Paused"
def resume(self) -> None:
"""If paused, then resume the execution thread."""
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() -> werkzeug.wrappers.response.Response:
"""Accept a workflow exection request and run it."""
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: int) -> tuple[str, int]:
"""Accept a job related action and report the result."""
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: Job) -> Generator[str, None, None]:
"""Yield 4 kilobytes of log text at a time."""
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: int) -> Response:
"""Dump the log."""
with jobs_lock:
job = jobs[jobid]
return Response(logspooler(job))
@app.route("/jobs", methods=["GET"])
def getjobs() -> Response:
"""Report all known jobs."""
with jobs_lock:
jobscopy = copy.copy(jobs)
def spool(jc: list[Job]) -> Generator[str, None, None]:
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()