1010import click
1111import typer
1212from bma_client_lib import BmaClient
13- from bma_client_lib .datastructures import job_types
13+ from bma_client_lib .datastructures import JobNotSupportedError , job_types
1414
1515APP_NAME = "bma-cli"
1616app = typer .Typer ()
@@ -66,12 +66,20 @@ def download(file_uuid: uuid.UUID) -> None:
6666def grind () -> None :
6767 """Get jobs from the server and handle them."""
6868 client , config = init ()
69+ # keep track of failing jobs to prevent getting them assigned again
70+ failed_jobs : set [str ] = set ()
71+ # run in a loop to make sure jobs created as a result of other jobs being completed are all processed
6972 while True :
7073 # first get any unfinished jobs already assigned to this client
71- jobs = client .get_jobs (job_filter = f"?limit=0&finished=false&client_uuid={ client .uuid } " )
74+ job_filter = f"?limit=0&finished=false&client_uuid={ client .uuid } "
75+ if failed_jobs :
76+ job_filter += f"&skip_jobs={ ',' .join (failed_jobs )} "
77+ jobs = client .get_jobs (job_filter = job_filter )
7278 if not jobs :
73- # no unfinished jobs assigned to this client, ask for new assignment
74- jobs = client .get_job_assignment ()
79+ # no unfinished jobs assigned to this client, ask for new assignment,
80+ # but skip jobs which previously failed
81+ job_filter = f"?skip_jobs={ ',' .join (failed_jobs )} " if failed_jobs else ""
82+ jobs = client .get_job_assignment (job_filter = job_filter )
7583
7684 if not jobs :
7785 click .echo ("Nothing left to do." )
@@ -82,7 +90,13 @@ def grind() -> None:
8290 for dictjob in jobs :
8391 job = job_types [dictjob ["job_type" ]](** dictjob )
8492 click .echo (f"Handling { job .job_type } { job .job_uuid } ..." )
85- client .handle_job (job = job )
93+ try :
94+ client .handle_job (job = job )
95+ except JobNotSupportedError :
96+ logger .exception (f"Job { job .job_uuid } not supported" )
97+ failed_jobs .add (str (job .job_uuid ))
98+ client .unassign_job (job = job )
99+ continue
86100 click .echo ("Done grinding for now!" )
87101
88102
0 commit comments