17
17
GEN3_TEAM_PROJECT_METADATA_LABEL ,
18
18
GEN3_USER_METADATA_LABEL ,
19
19
GEN3_NON_VA_WORKFLOW_MONTHLY_CAP ,
20
+ GEN3_DEFAULT_WORKFLOW_MONTHLY_CAP ,
20
21
)
21
22
22
23
from argowrapper import logger
@@ -209,7 +210,7 @@ def wrapper(*args, **kwargs):
209
210
return wrapper
210
211
211
212
212
- def check_user_billing_id (request ):
213
+ def check_user_info_for_billing_id_and_workflow_limit (request ):
213
214
"""
214
215
Check whether user is non-VA user
215
216
if user is VA-user, do nothing and proceed
@@ -232,16 +233,26 @@ def check_user_billing_id(request):
232
233
raise exception
233
234
logger .info ("Got user info successfully. Checking for billing id.." )
234
235
235
- if "tags" in user_info and "billing_id" in user_info ["tags" ]:
236
- billing_id = user_info ["tags" ]["billing_id" ]
237
- logger .info ("billing id found in user tags: " + billing_id )
238
- return billing_id
236
+ if "tags" in user_info :
237
+ if "billing_id" in user_info ["tags" ]:
238
+ billing_id = user_info ["tags" ]["billing_id" ]
239
+ logger .info ("billing id found in user tags: " + billing_id )
240
+ else :
241
+ billing_id = None
242
+
243
+ if "workflow_limit" in user_info ["tags" ]:
244
+ workflow_limit = user_info ["tags" ]["workflow_limit" ]
245
+ logger .info ("Workflow limit found in user tags: " + workflow_limit )
246
+ else :
247
+ workflow_limit = None
248
+
249
+ return billing_id , workflow_limit
239
250
else :
240
- logger .info ("billing id not found. " )
241
- return None
251
+ logger .info ("User info does not have tags " )
252
+ return None , None
242
253
243
254
244
- def check_user_reached_monthly_workflow_cap (request_token ):
255
+ def check_user_reached_monthly_workflow_cap (request_token , billing_id , custom_limit ):
245
256
"""
246
257
Query Argo service to see how many successful run user already
247
258
have in the current calendar month. If the number is greater than
@@ -252,20 +263,28 @@ def check_user_reached_monthly_workflow_cap(request_token):
252
263
current_month_workflows = argo_engine .get_user_workflows_for_current_month (
253
264
request_token
254
265
)
266
+ username = argo_engine_helper .get_username_from_token (request_token )
267
+ if custom_limit :
268
+ limit = custom_limit
269
+ else :
270
+ if billing_id :
271
+ limit = GEN3_NON_VA_WORKFLOW_MONTHLY_CAP
272
+ else :
273
+ limit = GEN3_DEFAULT_WORKFLOW_MONTHLY_CAP
255
274
256
- if len (current_month_workflows ) >= GEN3_NON_VA_WORKFLOW_MONTHLY_CAP :
257
- logger .info (
258
- "User already executed {} workflows this month and cannot create new ones anymore." .format (
259
- len (current_month_workflows )
275
+ if len (current_month_workflows ) >= limit :
276
+ logger .warn (
277
+ "This user {} already executed {} workflows this month and cannot create new ones anymore. The currently monthly cap for this user is {} ." .format (
278
+ username , len (current_month_workflows ), limit
260
279
)
261
280
)
281
+ return True
282
+ else :
262
283
logger .info (
263
- "The currently monthly cap is {}." .format (
264
- GEN3_NON_VA_WORKFLOW_MONTHLY_CAP
284
+ "This user {} executed {} workflows this month. The currently monthly cap for this user is {}." .format (
285
+ username , len ( current_month_workflows ), limit
265
286
)
266
287
)
267
- return True
268
-
269
288
return False
270
289
except Exception as e :
271
290
logger .error (e )
@@ -292,13 +311,14 @@ def submit_workflow(
292
311
reached_monthly_cap = False
293
312
294
313
# check if user has a billing id tag:
295
- billing_id = check_user_billing_id (request )
314
+ billing_id , workflow_limit = check_user_info_for_billing_id_and_workflow_limit (
315
+ request
316
+ )
296
317
297
318
# if user has billing_id (non-VA user), check if they already reached the monthly cap
298
- if billing_id :
299
- reached_monthly_cap = check_user_reached_monthly_workflow_cap (
300
- request .headers .get ("Authorization" )
301
- )
319
+ reached_monthly_cap = check_user_reached_monthly_workflow_cap (
320
+ request .headers .get ("Authorization" ), billing_id , workflow_limit
321
+ )
302
322
303
323
# submit workflow:
304
324
if not reached_monthly_cap :
@@ -311,8 +331,9 @@ def submit_workflow(
311
331
status_code = HTTP_403_FORBIDDEN ,
312
332
)
313
333
except Exception as exception :
334
+ logger .error (str (exception ))
314
335
return HTMLResponse (
315
- content = str ( exception ) ,
336
+ content = "Unexpected Error Occurred" ,
316
337
status_code = HTTP_500_INTERNAL_SERVER_ERROR ,
317
338
)
318
339
@@ -331,8 +352,9 @@ def get_workflow_details(
331
352
return argo_engine .get_workflow_details (workflow_name , uid )
332
353
333
354
except Exception as exception :
355
+ logger .error (str (exception ))
334
356
return HTMLResponse (
335
- content = str ( exception ) ,
357
+ content = "Unexpected Error Occurred" ,
336
358
status_code = HTTP_500_INTERNAL_SERVER_ERROR ,
337
359
)
338
360
@@ -354,7 +376,7 @@ def retry_workflow(
354
376
logger .error (traceback .format_exc ())
355
377
logger .error (f"could not retry { workflow_name } , failed with error { exception } " )
356
378
return HTMLResponse (
357
- content = str ( exception ) ,
379
+ content = "Could not retry workflow, error occurred" ,
358
380
status_code = HTTP_500_INTERNAL_SERVER_ERROR ,
359
381
)
360
382
@@ -372,8 +394,9 @@ def cancel_workflow(
372
394
return argo_engine .cancel_workflow (workflow_name )
373
395
374
396
except Exception as exception :
397
+ logger .error (str (exception ))
375
398
return HTMLResponse (
376
- content = str ( exception ) ,
399
+ content = "Unexpected Error Occurred" ,
377
400
status_code = HTTP_500_INTERNAL_SERVER_ERROR ,
378
401
)
379
402
@@ -400,8 +423,9 @@ def get_workflows(
400
423
)
401
424
402
425
except Exception as exception :
426
+ logger .error (str (exception ))
403
427
return HTMLResponse (
404
- content = exception ,
428
+ content = "Unexpected Error Occurred" ,
405
429
status_code = HTTP_500_INTERNAL_SERVER_ERROR ,
406
430
)
407
431
@@ -419,7 +443,8 @@ def get_workflow_logs(
419
443
return argo_engine .get_workflow_logs (workflow_name , uid )
420
444
421
445
except Exception as exception :
446
+ logger .error (str (exception ))
422
447
return HTMLResponse (
423
- content = exception ,
448
+ content = "Unexpected Error Occurred" ,
424
449
status_code = HTTP_500_INTERNAL_SERVER_ERROR ,
425
450
)
0 commit comments