|
8 | 8 |
|
9 | 9 | cli = typer.Typer(help="Antenna worker commands for remote processing") |
10 | 10 |
|
| 11 | +_PIPELINE_HELP = ( |
| 12 | + "Pipeline to use for processing (e.g., moth_binary, panama_moths_2024). " |
| 13 | + "Can be specified multiple times. Defaults to all pipelines if not specified." |
| 14 | +) |
| 15 | +_PROJECT_HELP = ( |
| 16 | + "Limit to jobs for specific Antenna project IDs. " |
| 17 | + "Can be specified multiple times. Defaults to all projects the auth token has access to." |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +def _validate_pipelines(pipelines: list[str] | None) -> list[str]: |
| 22 | + """Resolve and validate the --pipeline option.""" |
| 23 | + if not pipelines: |
| 24 | + return list(PIPELINE_CHOICES.keys()) |
| 25 | + |
| 26 | + invalid = [p for p in pipelines if p not in PIPELINE_CHOICES] |
| 27 | + if invalid: |
| 28 | + raise typer.BadParameter( |
| 29 | + f"Invalid pipeline(s): {', '.join(invalid)}. " |
| 30 | + f"Must be one of: {', '.join(PIPELINE_CHOICES.keys())}" |
| 31 | + ) |
| 32 | + return pipelines |
| 33 | + |
| 34 | + |
| 35 | +def _start_worker(pipelines: list[str] | None, project: list[int] | None) -> None: |
| 36 | + """Shared implementation for ``ami worker`` and ``ami worker run``.""" |
| 37 | + validated = _validate_pipelines(pipelines) |
| 38 | + project_ids = project or [] |
| 39 | + |
| 40 | + from trapdata.antenna.worker import run_worker |
| 41 | + |
| 42 | + run_worker(pipelines=validated, project_ids=project_ids) |
| 43 | + |
11 | 44 |
|
12 | 45 | @cli.callback(invoke_without_command=True) |
13 | | -def run( |
| 46 | +def worker_callback( |
14 | 47 | ctx: typer.Context, |
15 | 48 | pipelines: Annotated[ |
16 | 49 | list[str] | None, |
17 | | - typer.Option( |
18 | | - "--pipeline", |
19 | | - help="Pipeline to use for processing (e.g., moth_binary, panama_moths_2024). Can be specified multiple times. " |
20 | | - "Defaults to all pipelines if not specified.", |
21 | | - ), |
| 50 | + typer.Option("--pipeline", help=_PIPELINE_HELP), |
| 51 | + ] = None, |
| 52 | + project: Annotated[ |
| 53 | + list[int] | None, |
| 54 | + typer.Option("--project", help=_PROJECT_HELP), |
22 | 55 | ] = None, |
23 | 56 | ): |
24 | 57 | """ |
25 | 58 | Run the worker to process images from the Antenna API queue. |
26 | 59 |
|
27 | 60 | Can be invoked as 'ami worker' or 'ami worker run'. |
28 | 61 | """ |
29 | | - # Only run the worker if no subcommand was invoked |
30 | 62 | if ctx.invoked_subcommand is not None: |
31 | 63 | return |
| 64 | + _start_worker(pipelines, project) |
32 | 65 |
|
33 | | - if not pipelines: |
34 | | - pipelines = list(PIPELINE_CHOICES.keys()) |
35 | | - |
36 | | - # Validate that each pipeline is in PIPELINE_CHOICES |
37 | | - invalid_pipelines = [ |
38 | | - pipeline for pipeline in pipelines if pipeline not in PIPELINE_CHOICES.keys() |
39 | | - ] |
40 | 66 |
|
41 | | - if invalid_pipelines: |
42 | | - raise typer.BadParameter( |
43 | | - f"Invalid pipeline(s): {', '.join(invalid_pipelines)}. Must be one of: {', '.join(PIPELINE_CHOICES.keys())}" |
44 | | - ) |
45 | | - |
46 | | - from trapdata.antenna.worker import run_worker |
| 67 | +@cli.command("run") |
| 68 | +def run_cmd( |
| 69 | + pipelines: Annotated[ |
| 70 | + list[str] | None, |
| 71 | + typer.Option("--pipeline", help=_PIPELINE_HELP), |
| 72 | + ] = None, |
| 73 | + project: Annotated[ |
| 74 | + list[int] | None, |
| 75 | + typer.Option("--project", help=_PROJECT_HELP), |
| 76 | + ] = None, |
| 77 | +): |
| 78 | + """ |
| 79 | + Run the worker to process images from the Antenna API queue. |
47 | 80 |
|
48 | | - run_worker(pipelines=pipelines) |
| 81 | + Alias for 'ami worker' — both forms are identical. |
| 82 | + """ |
| 83 | + _start_worker(pipelines, project) |
49 | 84 |
|
50 | 85 |
|
51 | 86 | @cli.command("register") |
52 | 87 | def register( |
53 | 88 | project: Annotated[ |
54 | 89 | list[int] | None, |
55 | 90 | typer.Option( |
| 91 | + "--project", |
56 | 92 | help="Specific project IDs to register pipelines for. " |
57 | 93 | "If not specified, registers for all accessible projects.", |
58 | 94 | ), |
59 | 95 | ] = None, |
| 96 | + pipelines: Annotated[ |
| 97 | + list[str] | None, |
| 98 | + typer.Option("--pipeline", help=_PIPELINE_HELP), |
| 99 | + ] = None, |
60 | 100 | ): |
61 | 101 | """ |
62 | 102 | Register available pipelines with the Antenna platform for specified projects. |
63 | 103 |
|
64 | | - This command registers all available pipeline configurations with the Antenna platform |
65 | | - for the specified projects (or all accessible projects if none specified). |
| 104 | + This command registers the processing service and its pipeline configurations |
| 105 | + with the Antenna platform for the specified projects (or all accessible projects |
| 106 | + if none specified). |
| 107 | +
|
| 108 | + When --pipeline is specified, only those pipelines are advertised (instead of all). |
66 | 109 |
|
67 | 110 | The service name is read from the AMI_ANTENNA_SERVICE_NAME configuration setting. |
68 | 111 | Hostname will be added automatically to the service name. |
69 | 112 |
|
70 | 113 | Examples: |
71 | 114 | ami worker register --project 1 --project 2 |
72 | | - ami worker register # registers for all accessible projects |
| 115 | + ami worker register --pipeline mothbot_insect_orders_2025 |
| 116 | + ami worker register # registers all pipelines for all accessible projects |
73 | 117 | """ |
74 | 118 | from trapdata.antenna.registration import register_pipelines |
75 | 119 | from trapdata.settings import read_settings |
76 | 120 |
|
77 | 121 | settings = read_settings() |
78 | 122 | project_ids = project if project else [] |
| 123 | + validated_pipelines = _validate_pipelines(pipelines) |
79 | 124 | register_pipelines( |
80 | | - project_ids=project_ids, service_name=settings.antenna_service_name |
| 125 | + project_ids=project_ids, |
| 126 | + service_name=settings.antenna_service_name, |
| 127 | + pipeline_slugs=validated_pipelines, |
81 | 128 | ) |
0 commit comments