Skip to content

Commit 30d22f9

Browse files
committed
remaining_jobs -> list_jobs with view type
Without this, we would have had to duplicate code to view successful and failed jobs the same way we viewed remaining jobs.
1 parent 588c8e3 commit 30d22f9

1 file changed

Lines changed: 54 additions & 18 deletions

File tree

bugout/jobs.py

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import argparse
77
from datetime import datetime
8+
from enum import Enum
89
import json
910
import os
1011
import requests
@@ -22,6 +23,12 @@
2223
DEFAULT_CURSOR_CONTEXT_TYPE = "job_cursor"
2324

2425

26+
class JobView(Enum):
27+
REMAINING = "remaining"
28+
SUCCESS = "success"
29+
FAILURE = "failure"
30+
31+
2532
class BugoutJobQueue:
2633
"""
2734
This class implements a job queue in a Bugout journal.
@@ -95,24 +102,45 @@ def update_cursor(self, created_at: datetime):
95102
timeout=self.write_timeout,
96103
)
97104

98-
def remaining_jobs(
105+
def list_jobs(
99106
self,
107+
job_view: JobView = JobView.REMAINING,
100108
use_cursor: bool = True,
101109
limit: int = 10,
102110
offset: int = 0,
103111
) -> List[BugoutSearchResultWithEntryID]:
104112
"""
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+
106118
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.
108122
109123
Jobs are returned in chronological order.
110124
"""
111125
query_components: List[str] = [
112126
f"context_type:{self.context_type}",
113-
f"!tag:{self.success_tag}",
114-
f"!tag:{self.failure_tag}",
115127
]
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+
116144
if use_cursor:
117145
cursor_results = self.client.search(
118146
self.bugout_token,
@@ -282,10 +310,10 @@ def handle_create_job(args: argparse.Namespace) -> None:
282310
queue.create_job(args.id, args.title, args.content)
283311

284312

285-
def handle_remaining_jobs(args: argparse.Namespace) -> None:
313+
def handle_list_jobs(args: argparse.Namespace) -> None:
286314
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]))
289317

290318

291319
def handle_complete_job(args: argparse.Namespace) -> None:
@@ -323,26 +351,34 @@ def generate_cli() -> argparse.ArgumentParser:
323351
)
324352
create_job_parser.set_defaults(func=handle_create_job)
325353

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.",
328365
)
329-
add_queue_args(remaining_jobs_parser)
330-
remaining_jobs_parser.add_argument(
366+
list_jobs_parser.add_argument(
331367
"-c",
332368
"--use-cursor",
333369
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",
335371
)
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"
338374
)
339-
remaining_jobs_parser.add_argument(
375+
list_jobs_parser.add_argument(
340376
"--offset",
341377
type=int,
342378
default=0,
343-
help="Offset from which to page through remaining jobs",
379+
help="Offset from which to page through list jobs",
344380
)
345-
remaining_jobs_parser.set_defaults(func=handle_remaining_jobs)
381+
list_jobs_parser.set_defaults(func=handle_list_jobs)
346382

347383
complete_job_parser = subparsers.add_parser(
348384
"complete-job", help="Mark a job as complete"

0 commit comments

Comments
 (0)