Skip to content

Commit

Permalink
Handle unsupported jobs gracefully, bump bma-client-lib dep to 0.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tykling committed Nov 18, 2024
1 parent c8573e5 commit eb9b112
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ classifiers = [
]
dependencies = [
"typer-slim==0.12.5",
"bma-client-lib>=0.6.0",
"bma-client-lib>=0.7.0",
]
name = "bma-cli"
description = "BornHack Media Archive CLI Tool"
Expand Down
24 changes: 19 additions & 5 deletions src/bma_cli/bma_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import click
import typer
from bma_client_lib import BmaClient
from bma_client_lib.datastructures import job_types
from bma_client_lib.datastructures import JobNotSupportedError, job_types

APP_NAME = "bma-cli"
app = typer.Typer()
Expand Down Expand Up @@ -66,12 +66,20 @@ def download(file_uuid: uuid.UUID) -> None:
def grind() -> None:
"""Get jobs from the server and handle them."""
client, config = init()
# keep track of failing jobs to prevent getting them assigned again
failed_jobs: set[str] = set()
# run in a loop to make sure jobs created as a result of other jobs being completed are all processed
while True:
# first get any unfinished jobs already assigned to this client
jobs = client.get_jobs(job_filter=f"?limit=0&finished=false&client_uuid={client.uuid}")
job_filter = f"?limit=0&finished=false&client_uuid={client.uuid}"
if failed_jobs:
job_filter += f"&skip_jobs={','.join(failed_jobs)}"
jobs = client.get_jobs(job_filter=job_filter)
if not jobs:
# no unfinished jobs assigned to this client, ask for new assignment
jobs = client.get_job_assignment()
# no unfinished jobs assigned to this client, ask for new assignment,
# but skip jobs which previously failed
job_filter = f"?skip_jobs={','.join(failed_jobs)}" if failed_jobs else ""
jobs = client.get_job_assignment(job_filter=job_filter)

if not jobs:
click.echo("Nothing left to do.")
Expand All @@ -82,7 +90,13 @@ def grind() -> None:
for dictjob in jobs:
job = job_types[dictjob["job_type"]](**dictjob)
click.echo(f"Handling {job.job_type} {job.job_uuid} ...")
client.handle_job(job=job)
try:
client.handle_job(job=job)
except JobNotSupportedError:
logger.exception(f"Job {job.job_uuid} not supported")
failed_jobs.add(str(job.job_uuid))
client.unassign_job(job=job)
continue
click.echo("Done grinding for now!")


Expand Down

0 comments on commit eb9b112

Please sign in to comment.