@@ -19,12 +19,13 @@ class MissingAuthorization(Exception):
1919 pass
2020
2121
22- def get_api ():
23- if not connexion .request .headers .get ('Authorization' ):
24- raise MissingAuthorization ()
25- authtoken = connexion .request .headers ['Authorization' ]
26- if authtoken .startswith ("Bearer " ) or authtoken .startswith ("OAuth2 " ):
27- authtoken = authtoken [7 :]
22+ def get_api (authtoken = None ):
23+ if authtoken is None :
24+ if not connexion .request .headers .get ('Authorization' ):
25+ raise MissingAuthorization ()
26+ authtoken = connexion .request .headers ['Authorization' ]
27+ if authtoken .startswith ("Bearer " ) or authtoken .startswith ("OAuth2 " ):
28+ authtoken = authtoken [7 :]
2829 return arvados .api_from_config (version = "v1" , apiconfig = {
2930 "ARVADOS_API_HOST" : os .environ ["ARVADOS_API_HOST" ],
3031 "ARVADOS_API_TOKEN" : authtoken ,
@@ -55,6 +56,10 @@ def catch_exceptions_wrapper(self, *args, **kwargs):
5556 return {"msg" : str (e ), "status_code" : 500 }, 500
5657 except MissingAuthorization :
5758 return {"msg" : "'Authorization' header is missing or empty, expecting Arvados API token" , "status_code" : 401 }, 401
59+ except ValueError as e :
60+ return {"msg" : str (e ), "status_code" : 400 }, 400
61+ except Exception as e :
62+ return {"msg" : str (e ), "status_code" : 500 }, 500
5863
5964 return catch_exceptions_wrapper
6065
@@ -108,10 +113,10 @@ def ListRuns(self, page_size=None, page_token=None, state_search=None):
108113 "next_page_token" : workflow_list [- 1 ]["run_id" ] if workflow_list else ""
109114 }
110115
111- def log_for_run (self , run_id , message ):
112- get_api ().logs ().create (body = {"log" : {"object_uuid" : run_id ,
116+ def log_for_run (self , run_id , message , authtoken = None ):
117+ get_api (authtoken ).logs ().create (body = {"log" : {"object_uuid" : run_id ,
113118 "event_type" : "stderr" ,
114- "properties" : {"text" : message }}}).execute ()
119+ "properties" : {"text" : message + " \n " }}}).execute ()
115120
116121 def invoke_cwl_runner (self , cr_uuid , workflow_url , workflow_params ,
117122 env , project_uuid ,
@@ -123,9 +128,18 @@ def invoke_cwl_runner(self, cr_uuid, workflow_url, workflow_params,
123128 })
124129
125130 try :
126- with tempfile .NamedTemporaryFile () as inputtemp :
131+ with tempfile .NamedTemporaryFile (dir = tempdir , suffix = ".json" ) as inputtemp :
127132 json .dump (workflow_params , inputtemp )
128133 inputtemp .flush ()
134+
135+ msg = ""
136+ for dirpath , dirs , files in os .walk (tempdir ):
137+ for f in files :
138+ msg += " " + dirpath + "/" + f + "\n "
139+
140+ self .log_for_run (cr_uuid , "Contents of %s:\n %s" % (tempdir , msg ),
141+ env ['ARVADOS_API_TOKEN' ])
142+
129143 # TODO: run submission process in a container to prevent
130144 # a-c-r submission processes from seeing each other.
131145
@@ -138,6 +152,8 @@ def invoke_cwl_runner(self, cr_uuid, workflow_url, workflow_params,
138152 cmd .append (workflow_url )
139153 cmd .append (inputtemp .name )
140154
155+ self .log_for_run (cr_uuid , "Executing %s" % cmd , env ['ARVADOS_API_TOKEN' ])
156+
141157 proc = subprocess .Popen (cmd , env = env ,
142158 cwd = tempdir ,
143159 stdout = subprocess .PIPE ,
@@ -146,7 +162,7 @@ def invoke_cwl_runner(self, cr_uuid, workflow_url, workflow_params,
146162 if proc .returncode != 0 :
147163 api .container_requests ().update (uuid = cr_uuid , body = {"priority" : 0 }).execute ()
148164
149- self .log_for_run (cr_uuid , stderrdata )
165+ self .log_for_run (cr_uuid , stderrdata , env [ 'ARVADOS_API_TOKEN' ] )
150166
151167 if tempdir :
152168 shutil .rmtree (tempdir )
@@ -180,7 +196,15 @@ def RunWorkflow(self, **args):
180196 "output_path" : "n/a" ,
181197 "priority" : 500 }}).execute ()
182198
183- tempdir , body = self .collect_attachments (cr ["uuid" ])
199+ try :
200+ tempdir , body = self .collect_attachments (cr ["uuid" ])
201+ except Exception as e :
202+ self .log_for_run (cr ["uuid" ], str (e ))
203+ cr = api .container_requests ().update (uuid = cr ["uuid" ],
204+ body = {"container_request" :
205+ {"priority" : 0 }}).execute ()
206+
207+ return {"run_id" : cr ["uuid" ]}
184208
185209 workflow_url = body .get ("workflow_url" )
186210
0 commit comments