1
- from flask import Flask
2
- from flask import request , redirect
1
+ from flask import Flask , Response , request , redirect
3
2
import os
4
3
import subprocess
5
4
import tempfile
8
7
import urlparse
9
8
import signal
10
9
import threading
10
+ import time
11
11
12
12
app = Flask (__name__ )
13
13
@@ -24,21 +24,22 @@ def __init__(self, jobid, path, inputobj):
24
24
self .begin ()
25
25
26
26
def begin (self ):
27
+ loghandle , self .logname = tempfile .mkstemp ()
27
28
with self .updatelock :
28
29
self .outdir = tempfile .mkdtemp ()
29
30
self .proc = subprocess .Popen (["cwl-runner" , self .path , "-" ],
30
31
stdin = subprocess .PIPE ,
31
32
stdout = subprocess .PIPE ,
32
- stderr = subprocess . PIPE ,
33
+ stderr = loghandle ,
33
34
close_fds = True ,
34
35
cwd = self .outdir )
35
36
self .status = {
36
37
"id" : "%sjobs/%i" % (request .url_root , self .jobid ),
38
+ "log" : "%sjobs/%i/log" % (request .url_root , self .jobid ),
37
39
"run" : self .path ,
38
40
"state" : "Running" ,
39
- "input" : self .inputobj ,
40
- "output" : None ,
41
- "message" : "" }
41
+ "input" : json .loads (self .inputobj ),
42
+ "output" : None }
42
43
43
44
def run (self ):
44
45
self .stdoutdata , self .stderrdata = self .proc .communicate (self .inputobj )
@@ -50,7 +51,6 @@ def run(self):
50
51
else :
51
52
with self .updatelock :
52
53
self .status ["state" ] = "Failed"
53
- self .status ["message" ] = self .stderrdata
54
54
55
55
def getstatus (self ):
56
56
with self .updatelock :
@@ -104,6 +104,25 @@ def jobcontrol(jobid):
104
104
return json .dumps (status , indent = 4 ), 200 , ""
105
105
106
106
107
+ def logspooler (job ):
108
+ with open (job .logname , "r" ) as f :
109
+ while True :
110
+ r = f .read (4096 )
111
+ if r :
112
+ yield r
113
+ else :
114
+ with job .updatelock :
115
+ if job .status ["state" ] != "Running" :
116
+ break
117
+ time .sleep (1 )
118
+
119
+ @app .route ("/jobs/<int:jobid>/log" , methods = ['GET' ])
120
+ def getlog (jobid ):
121
+ with jobs_lock :
122
+ job = jobs [jobid ]
123
+ return Response (logspooler (job ))
124
+
125
+
107
126
if __name__ == "__main__" :
108
- app .debug = True
127
+ # app.debug = True
109
128
app .run ()
0 commit comments