|
5 | 5 |
|
6 | 6 | import argparse |
7 | 7 | from datetime import datetime |
| 8 | +from enum import Enum |
8 | 9 | import json |
9 | 10 | import os |
10 | 11 | import requests |
|
22 | 23 | DEFAULT_CURSOR_CONTEXT_TYPE = "job_cursor" |
23 | 24 |
|
24 | 25 |
|
| 26 | +class JobView(Enum): |
| 27 | + REMAINING = "remaining" |
| 28 | + SUCCESS = "success" |
| 29 | + FAILURE = "failure" |
| 30 | + |
| 31 | + |
25 | 32 | class BugoutJobQueue: |
26 | 33 | """ |
27 | 34 | This class implements a job queue in a Bugout journal. |
@@ -95,24 +102,45 @@ def update_cursor(self, created_at: datetime): |
95 | 102 | timeout=self.write_timeout, |
96 | 103 | ) |
97 | 104 |
|
98 | | - def remaining_jobs( |
| 105 | + def list_jobs( |
99 | 106 | self, |
| 107 | + job_view: JobView = JobView.REMAINING, |
100 | 108 | use_cursor: bool = True, |
101 | 109 | limit: int = 10, |
102 | 110 | offset: int = 0, |
103 | 111 | ) -> List[BugoutSearchResultWithEntryID]: |
104 | 112 | """ |
105 | | - List all remaining jobs. These are jobs that have neither been marked as complete nor as failed. |
| 113 | + List all jobs from the given job view: |
| 114 | + - REMAINING: These are jobs that have neither been marked as complete nor as failed. |
| 115 | + - SUCCESS: These are jobs that have been marked as successfully completed. |
| 116 | + - FAILURE: These are jobs that have meen marked as failures. |
| 117 | +
|
106 | 118 | If the use_cursor argument is True, this only returns jobs since the most recent cursor. If it is |
107 | | - False, remaining_jobs returns all incomplete and unfailed jobs since the beginning of time. |
| 119 | + False, returns all jobs from the given job view since the beginning of time. |
| 120 | +
|
| 121 | + Use the limit and offset parameters to page through the jobs. |
108 | 122 |
|
109 | 123 | Jobs are returned in chronological order. |
110 | 124 | """ |
111 | 125 | query_components: List[str] = [ |
112 | 126 | f"context_type:{self.context_type}", |
113 | | - f"!tag:{self.success_tag}", |
114 | | - f"!tag:{self.failure_tag}", |
115 | 127 | ] |
| 128 | + if job_view == JobView.REMAINING: |
| 129 | + query_components.extend( |
| 130 | + [ |
| 131 | + f"!tag:{self.success_tag}", |
| 132 | + f"!tag:{self.failure_tag}", |
| 133 | + ] |
| 134 | + ) |
| 135 | + elif job_view == JobView.SUCCESS: |
| 136 | + query_components.append( |
| 137 | + f"tag:{self.success_tag}", |
| 138 | + ) |
| 139 | + elif job_view == JobView.FAILURE: |
| 140 | + query_components.append( |
| 141 | + f"tag:{self.failure_tag}", |
| 142 | + ) |
| 143 | + |
116 | 144 | if use_cursor: |
117 | 145 | cursor_results = self.client.search( |
118 | 146 | self.bugout_token, |
@@ -282,10 +310,10 @@ def handle_create_job(args: argparse.Namespace) -> None: |
282 | 310 | queue.create_job(args.id, args.title, args.content) |
283 | 311 |
|
284 | 312 |
|
285 | | -def handle_remaining_jobs(args: argparse.Namespace) -> None: |
| 313 | +def handle_list_jobs(args: argparse.Namespace) -> None: |
286 | 314 | queue = queue_from_args(args) |
287 | | - remaining_jobs = queue.remaining_jobs(args.use_cursor, args.limit, args.offset) |
288 | | - print(json.dumps([json.loads(job.json()) for job in remaining_jobs])) |
| 315 | + jobs = queue.list_jobs(args.view, args.use_cursor, args.limit, args.offset) |
| 316 | + print(json.dumps([json.loads(job.json()) for job in jobs])) |
289 | 317 |
|
290 | 318 |
|
291 | 319 | def handle_complete_job(args: argparse.Namespace) -> None: |
@@ -323,26 +351,34 @@ def generate_cli() -> argparse.ArgumentParser: |
323 | 351 | ) |
324 | 352 | create_job_parser.set_defaults(func=handle_create_job) |
325 | 353 |
|
326 | | - remaining_jobs_parser = subparsers.add_parser( |
327 | | - "remaining-jobs", help="View remaining jobs in queue (FIFO order)" |
| 354 | + list_jobs_parser = subparsers.add_parser( |
| 355 | + "list-jobs", help="View jobs in queue (FIFO order)" |
| 356 | + ) |
| 357 | + add_queue_args(list_jobs_parser) |
| 358 | + list_jobs_parser.add_argument( |
| 359 | + "-v", |
| 360 | + "--view", |
| 361 | + required=True, |
| 362 | + type=JobView, |
| 363 | + choices=[JobView.REMAINING, JobView.SUCCESS, JobView.FAILURE], |
| 364 | + help="What kind of jobs to list from the queue.", |
328 | 365 | ) |
329 | | - add_queue_args(remaining_jobs_parser) |
330 | | - remaining_jobs_parser.add_argument( |
| 366 | + list_jobs_parser.add_argument( |
331 | 367 | "-c", |
332 | 368 | "--use-cursor", |
333 | 369 | action="store_true", |
334 | | - help="Set this flag if you want to only view remaining jobs created after the most recent cursor position", |
| 370 | + help="Set this flag if you want to only view list jobs created after the most recent cursor position", |
335 | 371 | ) |
336 | | - remaining_jobs_parser.add_argument( |
337 | | - "--limit", type=int, default=10, help="Number of remaining jobs to view" |
| 372 | + list_jobs_parser.add_argument( |
| 373 | + "--limit", type=int, default=10, help="Number of list jobs to view" |
338 | 374 | ) |
339 | | - remaining_jobs_parser.add_argument( |
| 375 | + list_jobs_parser.add_argument( |
340 | 376 | "--offset", |
341 | 377 | type=int, |
342 | 378 | default=0, |
343 | | - help="Offset from which to page through remaining jobs", |
| 379 | + help="Offset from which to page through list jobs", |
344 | 380 | ) |
345 | | - remaining_jobs_parser.set_defaults(func=handle_remaining_jobs) |
| 381 | + list_jobs_parser.set_defaults(func=handle_list_jobs) |
346 | 382 |
|
347 | 383 | complete_job_parser = subparsers.add_parser( |
348 | 384 | "complete-job", help="Mark a job as complete" |
|
0 commit comments