8
8
import subprocess
9
9
import tempfile
10
10
import functools
11
+ import threading
12
+ import logging
13
+
11
14
from wes_service .util import visit , WESBackend
12
15
13
16
@@ -36,6 +39,7 @@ def catch_exceptions_wrapper(self, *args, **kwargs):
36
39
try :
37
40
return orig_func (self , * args , ** kwargs )
38
41
except arvados .errors .ApiError as e :
42
+ logging .exception ("Failure" )
39
43
return {"msg" : e ._get_reason (), "status_code" : e .resp .status }, int (e .resp .status )
40
44
except subprocess .CalledProcessError as e :
41
45
return {"msg" : str (e ), "status_code" : 500 }, 500
@@ -77,6 +81,22 @@ def ListWorkflows(self):
77
81
"next_page_token" : ""
78
82
}
79
83
84
+ def invoke_cwl_runner (self , cr_uuid , workflow_url , workflow_params , env ):
85
+ try :
86
+ with tempfile .NamedTemporaryFile () as inputtemp :
87
+ json .dump (workflow_params , inputtemp )
88
+ inputtemp .flush ()
89
+ workflow_id = subprocess .check_output (["arvados-cwl-runner" , "--submit-request-uuid=" + cr_uuid , # NOQA
90
+ "--submit" , "--no-wait" , "--api=containers" , # NOQA
91
+ workflow_url , inputtemp .name ], env = env ).strip () # NOQA
92
+ except subprocess .CalledProcessError as e :
93
+ api = arvados .api_from_config (version = "v1" , apiconfig = {
94
+ "ARVADOS_API_HOST" : env ["ARVADOS_API_HOST" ],
95
+ "ARVADOS_API_TOKEN" : env ['ARVADOS_API_TOKEN' ],
96
+ "ARVADOS_API_HOST_INSECURE" : env ["ARVADOS_API_HOST_INSECURE" ] # NOQA
97
+ })
98
+ request = api .container_requests ().update (uuid = cr_uuid , body = {"priority" : 0 }).execute () # NOQA
99
+
80
100
@catch_exceptions
81
101
def RunWorkflow (self , body ):
82
102
if body ["workflow_type" ] != "CWL" or body ["workflow_type_version" ] != "v1.0" : # NOQA
@@ -88,19 +108,29 @@ def RunWorkflow(self, body):
88
108
"ARVADOS_API_TOKEN" : connexion .request .headers ['Authorization' ],
89
109
"ARVADOS_API_HOST_INSECURE" : os .environ .get ("ARVADOS_API_HOST_INSECURE" , "false" ) # NOQA
90
110
}
91
- with tempfile .NamedTemporaryFile () as inputtemp :
92
- json .dump (body ["workflow_params" ], inputtemp )
93
- inputtemp .flush ()
94
- workflow_id = subprocess .check_output (["arvados-cwl-runner" , "--submit" , "--no-wait" , "--api=containers" , # NOQA
95
- body .get ("workflow_url" ), inputtemp .name ], env = env ).strip () # NOQA
96
- return {"workflow_id" : workflow_id }
111
+
112
+ api = get_api ()
113
+
114
+ cr = api .container_requests ().create (body = {"container_request" :
115
+ {"command" : ["" ],
116
+ "container_image" : "n/a" ,
117
+ "state" : "Uncommitted" ,
118
+ "output_path" : "n/a" ,
119
+ "priority" : 500 }}).execute ()
120
+
121
+ threading .Thread (target = self .invoke_cwl_runner , args = (cr ["uuid" ], body .get ("workflow_url" ), body ["workflow_params" ], env )).start ()
122
+
123
+ return {"workflow_id" : cr ["uuid" ]}
97
124
98
125
@catch_exceptions
99
126
def GetWorkflowLog (self , workflow_id ):
100
127
api = get_api ()
101
128
102
129
request = api .container_requests ().get (uuid = workflow_id ).execute ()
103
- container = api .containers ().get (uuid = request ["container_uuid" ]).execute () # NOQA
130
+ if request ["container_uuid" ]:
131
+ container = api .containers ().get (uuid = request ["container_uuid" ]).execute () # NOQA
132
+ else :
133
+ container = {"state" : "Queued" , "exit_code" : None }
104
134
105
135
outputobj = {}
106
136
if request ["output_uuid" ]:
@@ -142,14 +172,19 @@ def keepref(d):
142
172
@catch_exceptions
143
173
def CancelJob (self , workflow_id ): # NOQA
144
174
api = get_api ()
145
- request = api .container_requests ().update (body = {"priority" : 0 }).execute () # NOQA
175
+ request = api .container_requests ().update (uuid = workflow_id , body = {"priority" : 0 }).execute () # NOQA
146
176
return {"workflow_id" : request ["uuid" ]}
147
177
148
178
@catch_exceptions
149
179
def GetWorkflowStatus (self , workflow_id ):
150
180
api = get_api ()
151
181
request = api .container_requests ().get (uuid = workflow_id ).execute ()
152
- container = api .containers ().get (uuid = request ["container_uuid" ]).execute () # NOQA
182
+ if request ["container_uuid" ]:
183
+ container = api .containers ().get (uuid = request ["container_uuid" ]).execute () # NOQA
184
+ elif request ["priority" ] == 0 :
185
+ container = {"state" : "Cancelled" }
186
+ else :
187
+ container = {"state" : "Queued" }
153
188
return {"workflow_id" : request ["uuid" ],
154
189
"state" : statemap [container ["state" ]]}
155
190
0 commit comments