|
| 1 | +import asyncio |
| 2 | +import enum |
| 3 | +import logging |
| 4 | +import socket |
| 5 | +from datetime import timedelta |
| 6 | +from typing import Annotated |
| 7 | + |
1 | 8 | import typer |
2 | 9 |
|
3 | | -from docket import __version__ |
| 10 | +from . import __version__, tasks |
| 11 | +from .docket import Docket |
| 12 | +from .worker import Worker |
4 | 13 |
|
5 | 14 | app: typer.Typer = typer.Typer( |
6 | 15 | help="Docket - A distributed background task system for Python functions", |
|
9 | 18 | ) |
10 | 19 |
|
11 | 20 |
|
| 21 | +class LogLevel(enum.StrEnum): |
| 22 | + DEBUG = "DEBUG" |
| 23 | + INFO = "INFO" |
| 24 | + WARNING = "WARNING" |
| 25 | + ERROR = "ERROR" |
| 26 | + CRITICAL = "CRITICAL" |
| 27 | + |
| 28 | + |
| 29 | +def duration(duration_str: str | timedelta) -> timedelta: |
| 30 | + """ |
| 31 | + Parse a duration string into a timedelta. |
| 32 | +
|
| 33 | + Supported formats: |
| 34 | + - 123 = 123 seconds |
| 35 | + - 123s = 123 seconds |
| 36 | + - 123m = 123 minutes |
| 37 | + - 123h = 123 hours |
| 38 | + - 00:00 = mm:ss |
| 39 | + - 00:00:00 = hh:mm:ss |
| 40 | + """ |
| 41 | + if isinstance(duration_str, timedelta): |
| 42 | + return duration_str |
| 43 | + |
| 44 | + if ":" in duration_str: |
| 45 | + parts = duration_str.split(":") |
| 46 | + if len(parts) == 2: # mm:ss |
| 47 | + minutes, seconds = map(int, parts) |
| 48 | + return timedelta(minutes=minutes, seconds=seconds) |
| 49 | + elif len(parts) == 3: # hh:mm:ss |
| 50 | + hours, minutes, seconds = map(int, parts) |
| 51 | + return timedelta(hours=hours, minutes=minutes, seconds=seconds) |
| 52 | + else: |
| 53 | + raise ValueError(f"Invalid duration string: {duration_str}") |
| 54 | + elif duration_str.endswith("s"): |
| 55 | + return timedelta(seconds=int(duration_str[:-1])) |
| 56 | + elif duration_str.endswith("m"): |
| 57 | + return timedelta(minutes=int(duration_str[:-1])) |
| 58 | + elif duration_str.endswith("h"): |
| 59 | + return timedelta(hours=int(duration_str[:-1])) |
| 60 | + else: |
| 61 | + return timedelta(seconds=int(duration_str)) |
| 62 | + |
| 63 | + |
12 | 64 | @app.command( |
13 | 65 | help="Start a worker to process tasks", |
14 | 66 | ) |
15 | | -def worker() -> None: |
16 | | - print("TODO: Configure and start a worker") |
| 67 | +def worker( |
| 68 | + tasks: Annotated[ |
| 69 | + list[str], |
| 70 | + typer.Option( |
| 71 | + "--tasks", |
| 72 | + help=( |
| 73 | + "The dotted path of a task collection to register with the docket. " |
| 74 | + "This can be specified multiple times. A task collection is any " |
| 75 | + "iterable of async functions." |
| 76 | + ), |
| 77 | + ), |
| 78 | + ] = ["docket.tasks:standard_tasks"], |
| 79 | + docket_: Annotated[ |
| 80 | + str, |
| 81 | + typer.Option( |
| 82 | + "--docket", |
| 83 | + help="The name of the docket", |
| 84 | + envvar="DOCKET_NAME", |
| 85 | + ), |
| 86 | + ] = "docket", |
| 87 | + url: Annotated[ |
| 88 | + str, |
| 89 | + typer.Option( |
| 90 | + help="The URL of the Redis server", |
| 91 | + envvar="DOCKET_URL", |
| 92 | + ), |
| 93 | + ] = "redis://localhost:6379/0", |
| 94 | + name: Annotated[ |
| 95 | + str | None, |
| 96 | + typer.Option( |
| 97 | + help="The name of the worker", |
| 98 | + envvar="DOCKET_WORKER_NAME", |
| 99 | + ), |
| 100 | + ] = socket.gethostname(), |
| 101 | + logging_level: Annotated[ |
| 102 | + LogLevel, |
| 103 | + typer.Option( |
| 104 | + help="The logging level", |
| 105 | + envvar="DOCKET_LOGGING_LEVEL", |
| 106 | + ), |
| 107 | + ] = LogLevel.INFO, |
| 108 | + prefetch_count: Annotated[ |
| 109 | + int, |
| 110 | + typer.Option( |
| 111 | + help="The number of tasks to request from the docket at a time", |
| 112 | + envvar="DOCKET_WORKER_PREFETCH_COUNT", |
| 113 | + ), |
| 114 | + ] = 10, |
| 115 | + redelivery_timeout: Annotated[ |
| 116 | + timedelta, |
| 117 | + typer.Option( |
| 118 | + parser=duration, |
| 119 | + help="How long to wait before redelivering a task to another worker", |
| 120 | + envvar="DOCKET_WORKER_REDELIVERY_TIMEOUT", |
| 121 | + ), |
| 122 | + ] = timedelta(minutes=5), |
| 123 | + reconnection_delay: Annotated[ |
| 124 | + timedelta, |
| 125 | + typer.Option( |
| 126 | + parser=duration, |
| 127 | + help=( |
| 128 | + "How long to wait before reconnecting to the Redis server after " |
| 129 | + "a connection error" |
| 130 | + ), |
| 131 | + envvar="DOCKET_WORKER_RECONNECTION_DELAY", |
| 132 | + ), |
| 133 | + ] = timedelta(seconds=5), |
| 134 | + until_finished: Annotated[ |
| 135 | + bool, |
| 136 | + typer.Option( |
| 137 | + "--until-finished", |
| 138 | + help="Exit after the current docket is finished", |
| 139 | + ), |
| 140 | + ] = False, |
| 141 | +) -> None: |
| 142 | + logging.basicConfig(level=logging_level) |
| 143 | + asyncio.run( |
| 144 | + Worker.run( |
| 145 | + docket_name=docket_, |
| 146 | + url=url, |
| 147 | + name=name, |
| 148 | + prefetch_count=prefetch_count, |
| 149 | + redelivery_timeout=redelivery_timeout, |
| 150 | + reconnection_delay=reconnection_delay, |
| 151 | + until_finished=until_finished, |
| 152 | + tasks=tasks, |
| 153 | + ) |
| 154 | + ) |
| 155 | + |
| 156 | + |
| 157 | +@app.command(help="Adds a trace task to the Docket") |
| 158 | +def trace( |
| 159 | + docket_: Annotated[ |
| 160 | + str, |
| 161 | + typer.Option( |
| 162 | + "--docket", |
| 163 | + help="The name of the docket", |
| 164 | + envvar="DOCKET_NAME", |
| 165 | + ), |
| 166 | + ] = "docket", |
| 167 | + url: Annotated[ |
| 168 | + str, |
| 169 | + typer.Option( |
| 170 | + help="The URL of the Redis server", |
| 171 | + envvar="DOCKET_URL", |
| 172 | + ), |
| 173 | + ] = "redis://localhost:6379/0", |
| 174 | + message: Annotated[ |
| 175 | + str, |
| 176 | + typer.Argument( |
| 177 | + help="The message to print", |
| 178 | + ), |
| 179 | + ] = "Howdy!", |
| 180 | +) -> None: |
| 181 | + async def run() -> None: |
| 182 | + async with Docket(name=docket_, url=url) as docket: |
| 183 | + execution = await docket.add(tasks.trace)(message) |
| 184 | + print(f"Added trace task {execution.key!r} to the docket {docket.name!r}") |
| 185 | + |
| 186 | + asyncio.run(run()) |
17 | 187 |
|
18 | 188 |
|
19 | 189 | @app.command( |
|
0 commit comments