rrq 0.3.5__py3-none-any.whl → 0.3.7__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rrq/cli.py +30 -18
- {rrq-0.3.5.dist-info → rrq-0.3.7.dist-info}/METADATA +45 -66
- {rrq-0.3.5.dist-info → rrq-0.3.7.dist-info}/RECORD +6 -6
- {rrq-0.3.5.dist-info → rrq-0.3.7.dist-info}/WHEEL +0 -0
- {rrq-0.3.5.dist-info → rrq-0.3.7.dist-info}/entry_points.txt +0 -0
- {rrq-0.3.5.dist-info → rrq-0.3.7.dist-info}/licenses/LICENSE +0 -0
rrq/cli.py
CHANGED
|
@@ -18,6 +18,14 @@ from .settings import RRQSettings
|
|
|
18
18
|
from .store import JobStore
|
|
19
19
|
from .worker import RRQWorker
|
|
20
20
|
|
|
21
|
+
# Attempt to import dotenv components for .env file loading
|
|
22
|
+
try:
|
|
23
|
+
from dotenv import find_dotenv, load_dotenv
|
|
24
|
+
|
|
25
|
+
DOTENV_AVAILABLE = True
|
|
26
|
+
except ImportError:
|
|
27
|
+
DOTENV_AVAILABLE = False
|
|
28
|
+
|
|
21
29
|
logger = logging.getLogger(__name__)
|
|
22
30
|
|
|
23
31
|
|
|
@@ -28,12 +36,21 @@ def _load_app_settings(settings_object_path: str | None = None) -> RRQSettings:
|
|
|
28
36
|
If the environment variable is not set, will create a default settings object.
|
|
29
37
|
RRQ Setting objects, automatically pick up ENVIRONMENT variables starting with RRQ_.
|
|
30
38
|
|
|
39
|
+
This function will also attempt to load a .env file if python-dotenv is installed
|
|
40
|
+
and a .env file is found. System environment variables take precedence over .env variables.
|
|
41
|
+
|
|
31
42
|
Args:
|
|
32
43
|
settings_object_path: A string representing the path to the settings object. (e.g. "myapp.worker_config.rrq_settings").
|
|
33
44
|
|
|
34
45
|
Returns:
|
|
35
46
|
The RRQSettings object.
|
|
36
47
|
"""
|
|
48
|
+
if DOTENV_AVAILABLE:
|
|
49
|
+
dotenv_path = find_dotenv(usecwd=True)
|
|
50
|
+
if dotenv_path:
|
|
51
|
+
logger.debug(f"Loading .env file at: {dotenv_path}...")
|
|
52
|
+
load_dotenv(dotenv_path=dotenv_path, override=False)
|
|
53
|
+
|
|
37
54
|
try:
|
|
38
55
|
if settings_object_path is None:
|
|
39
56
|
settings_object_path = os.getenv("RRQ_SETTINGS")
|
|
@@ -153,12 +170,10 @@ def start_rrq_worker_subprocess(
|
|
|
153
170
|
) -> subprocess.Popen | None:
|
|
154
171
|
"""Start an RRQ worker process, optionally for specific queues."""
|
|
155
172
|
command = ["rrq", "worker", "run"]
|
|
173
|
+
|
|
156
174
|
if settings_object_path:
|
|
157
175
|
command.extend(["--settings", settings_object_path])
|
|
158
|
-
|
|
159
|
-
raise ValueError(
|
|
160
|
-
"start_rrq_worker_subprocess called without settings_object_path!"
|
|
161
|
-
)
|
|
176
|
+
|
|
162
177
|
# Add queue filters if specified
|
|
163
178
|
if queues:
|
|
164
179
|
for q in queues:
|
|
@@ -220,16 +235,6 @@ async def watch_rrq_worker_impl(
|
|
|
220
235
|
settings_object_path: str | None = None,
|
|
221
236
|
queues: list[str] | None = None,
|
|
222
237
|
) -> None:
|
|
223
|
-
if not settings_object_path:
|
|
224
|
-
click.echo(
|
|
225
|
-
click.style(
|
|
226
|
-
"ERROR: 'rrq worker watch' requires --settings to be specified.",
|
|
227
|
-
fg="red",
|
|
228
|
-
),
|
|
229
|
-
err=True,
|
|
230
|
-
)
|
|
231
|
-
sys.exit(1)
|
|
232
|
-
|
|
233
238
|
abs_watch_path = os.path.abspath(watch_path)
|
|
234
239
|
click.echo(
|
|
235
240
|
f"Watching for file changes in {abs_watch_path} to restart RRQ worker (app settings: {settings_object_path})..."
|
|
@@ -295,7 +300,7 @@ def rrq():
|
|
|
295
300
|
"""RRQ: Reliable Redis Queue Command Line Interface.
|
|
296
301
|
|
|
297
302
|
Provides tools for running RRQ workers, checking system health,
|
|
298
|
-
and managing jobs. Requires an application-specific
|
|
303
|
+
and managing jobs. Requires an application-specific settings object
|
|
299
304
|
for most operations.
|
|
300
305
|
"""
|
|
301
306
|
pass
|
|
@@ -329,6 +334,7 @@ def worker_cli():
|
|
|
329
334
|
help=(
|
|
330
335
|
"Python settings path for application worker settings "
|
|
331
336
|
"(e.g., myapp.worker_config.rrq_settings). "
|
|
337
|
+
"Alternatively, this can be specified as RRQ_SETTINGS env variable. "
|
|
332
338
|
"The specified settings object must include a `job_registry: JobRegistry`."
|
|
333
339
|
),
|
|
334
340
|
)
|
|
@@ -337,7 +343,9 @@ def worker_run_command(
|
|
|
337
343
|
queues: tuple[str, ...],
|
|
338
344
|
settings_object_path: str,
|
|
339
345
|
):
|
|
340
|
-
"""Run an RRQ worker process.
|
|
346
|
+
"""Run an RRQ worker process.
|
|
347
|
+
Requires an application-specific settings object.
|
|
348
|
+
"""
|
|
341
349
|
rrq_settings = _load_app_settings(settings_object_path)
|
|
342
350
|
|
|
343
351
|
# Determine queues to poll
|
|
@@ -418,7 +426,9 @@ def worker_watch_command(
|
|
|
418
426
|
settings_object_path: str,
|
|
419
427
|
queues: tuple[str, ...],
|
|
420
428
|
):
|
|
421
|
-
"""Run the RRQ worker with auto-restart on file changes in PATH.
|
|
429
|
+
"""Run the RRQ worker with auto-restart on file changes in PATH.
|
|
430
|
+
Requires an application-specific settings object.
|
|
431
|
+
"""
|
|
422
432
|
# Run watch with optional queue filters
|
|
423
433
|
asyncio.run(
|
|
424
434
|
watch_rrq_worker_impl(
|
|
@@ -446,7 +456,9 @@ def worker_watch_command(
|
|
|
446
456
|
),
|
|
447
457
|
)
|
|
448
458
|
def check_command(settings_object_path: str):
|
|
449
|
-
"""Perform a health check on active RRQ worker(s).
|
|
459
|
+
"""Perform a health check on active RRQ worker(s).
|
|
460
|
+
Requires an application-specific settings object.
|
|
461
|
+
"""
|
|
450
462
|
click.echo("Performing RRQ health check...")
|
|
451
463
|
healthy = asyncio.run(
|
|
452
464
|
check_health_async_impl(settings_object_path=settings_object_path)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: rrq
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.7
|
|
4
4
|
Summary: RRQ is a Python library for creating reliable job queues using Redis and asyncio
|
|
5
5
|
Project-URL: Homepage, https://github.com/getresq/rrq
|
|
6
6
|
Project-URL: Bug Tracker, https://github.com/getresq/rrq/issues
|
|
@@ -29,18 +29,6 @@ Description-Content-Type: text/markdown
|
|
|
29
29
|
|
|
30
30
|
RRQ is a Python library for creating reliable job queues using Redis and `asyncio`, inspired by [ARQ (Async Redis Queue)](https://github.com/samuelcolvin/arq). It focuses on providing at-least-once job processing semantics with features like automatic retries, job timeouts, dead-letter queues, and graceful worker shutdown.
|
|
31
31
|
|
|
32
|
-
## Core Components
|
|
33
|
-
|
|
34
|
-
* **`RRQClient` (`client.py`)**: Used to enqueue jobs onto specific queues. Supports deferring jobs (by time delta or specific datetime), assigning custom job IDs, and enforcing job uniqueness via keys.
|
|
35
|
-
* **`RRQWorker` (`worker.py`)**: The process that polls queues, fetches jobs, executes the corresponding handler functions, and manages the job lifecycle based on success, failure, retries, or timeouts. Handles graceful shutdown via signals (SIGINT, SIGTERM).
|
|
36
|
-
* **`JobRegistry` (`registry.py`)**: A simple registry to map string function names (used when enqueuing) to the actual asynchronous handler functions the worker should execute.
|
|
37
|
-
* **`JobStore` (`store.py`)**: An abstraction layer handling all direct interactions with Redis. It manages job definitions (Hashes), queues (Sorted Sets), processing locks (Strings with TTL), unique job locks, and worker health checks.
|
|
38
|
-
* **`Job` (`job.py`)**: A Pydantic model representing a job, containing its ID, handler name, arguments, status, retry counts, timestamps, results, etc.
|
|
39
|
-
* **`JobStatus` (`job.py`)**: An Enum defining the possible states of a job (`PENDING`, `ACTIVE`, `COMPLETED`, `FAILED`, `RETRYING`).
|
|
40
|
-
* **`RRQSettings` (`settings.py`)**: A Pydantic `BaseSettings` model for configuring RRQ behavior (Redis DSN, queue names, timeouts, retry policies, concurrency, etc.). Loadable from environment variables (prefix `RRQ_`).
|
|
41
|
-
* **`constants.py`**: Defines shared constants like Redis key prefixes and default configuration values.
|
|
42
|
-
* **`exc.py`**: Defines custom exceptions, notably `RetryJob` which handlers can raise to explicitly request a retry, potentially with a custom delay.
|
|
43
|
-
|
|
44
32
|
## Key Features
|
|
45
33
|
|
|
46
34
|
* **At-Least-Once Semantics**: Uses Redis locks to ensure a job is processed by only one worker at a time. If a worker crashes or shuts down mid-processing, the lock expires, and the job *should* be re-processed (though re-queueing on unclean shutdown isn't implemented here yet - graceful shutdown *does* re-queue).
|
|
@@ -52,8 +40,10 @@ RRQ is a Python library for creating reliable job queues using Redis and `asynci
|
|
|
52
40
|
* **Graceful Shutdown**: Workers listen for SIGINT/SIGTERM and attempt to finish active jobs within a grace period before exiting. Interrupted jobs are re-queued.
|
|
53
41
|
* **Worker Health Checks**: Workers periodically update a health key in Redis with a TTL, allowing monitoring systems to track active workers.
|
|
54
42
|
* **Deferred Execution**: Jobs can be scheduled to run at a future time using `_defer_by` or `_defer_until`.
|
|
55
|
-
|
|
56
|
-
|
|
43
|
+
|
|
44
|
+
- Using deferral with a specific `_job_id` will effectively reschedule the job associated with that ID to the new time, overwriting its previous definition and score. It does not create multiple distinct scheduled jobs with the same ID.
|
|
45
|
+
|
|
46
|
+
- To batch multiple enqueue calls into a single deferred job (and prevent duplicates within the defer window), combine `_unique_key` with `_defer_by`. For example:
|
|
57
47
|
|
|
58
48
|
```python
|
|
59
49
|
await client.enqueue(
|
|
@@ -129,6 +119,9 @@ if __name__ == "__main__":
|
|
|
129
119
|
|
|
130
120
|
**5. Run a Worker:**
|
|
131
121
|
|
|
122
|
+
Note: You don't need to run a worker as the Command Line Interface `rrq` is used for
|
|
123
|
+
this purpose.
|
|
124
|
+
|
|
132
125
|
```python
|
|
133
126
|
# worker_script.py
|
|
134
127
|
from rrq.worker import RRQWorker
|
|
@@ -145,61 +138,47 @@ if __name__ == "__main__":
|
|
|
145
138
|
|
|
146
139
|
You can run multiple instances of `worker_script.py` for concurrent processing.
|
|
147
140
|
|
|
148
|
-
##
|
|
149
|
-
|
|
150
|
-
RRQ
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
141
|
+
## Command Line Interface
|
|
142
|
+
|
|
143
|
+
RRQ provides a command-line interface (CLI) for managing workers and performing health checks:
|
|
144
|
+
|
|
145
|
+
- **`rrq worker run`** - Run an RRQ worker process.
|
|
146
|
+
- `--settings` (optional): Specify the Python path to your settings object (e.g., `myapp.worker_config.rrq_settings`). If not provided, it will use the `RRQ_SETTINGS` environment variable or default to a basic `RRQSettings` object.
|
|
147
|
+
- `--queue` (optional, multiple): Specify queue(s) to poll. Defaults to the `default_queue_name` in settings.
|
|
148
|
+
- `--burst` (flag): Run the worker in burst mode to process one job or batch and then exit.
|
|
149
|
+
- **`rrq worker watch`** - Run an RRQ worker with auto-restart on file changes.
|
|
150
|
+
- `--path` (optional): Directory path to watch for changes. Defaults to the current directory.
|
|
151
|
+
- `--settings` (optional): Same as above.
|
|
152
|
+
- `--queue` (optional, multiple): Same as above.
|
|
153
|
+
- **`rrq check`** - Perform a health check on active RRQ workers.
|
|
154
|
+
- `--settings` (optional): Same as above.
|
|
155
|
+
- **`rrq dlq requeue`** - Requeue jobs from the dead letter queue back into a live queue.
|
|
156
|
+
- `--settings` (optional): Same as above.
|
|
157
|
+
- `--dlq-name` (optional): Name of the DLQ (without prefix). Defaults to `default_dlq_name` in settings.
|
|
158
|
+
- `--queue` (optional): Target queue name (without prefix). Defaults to `default_queue_name` in settings.
|
|
159
|
+
- `--limit` (optional): Maximum number of DLQ jobs to requeue; all if not set.
|
|
162
160
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
```bash
|
|
166
|
-
rrq <command> [options]
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
### Commands
|
|
170
|
-
|
|
171
|
-
- **`worker run`**: Run an RRQ worker process to process jobs from queues.
|
|
172
|
-
```bash
|
|
173
|
-
rrq worker run [--burst] --settings <settings_path>
|
|
174
|
-
```
|
|
175
|
-
- `--burst`: Run in burst mode (process one job/batch then exit).
|
|
176
|
-
- `--settings`: Python settings path for application worker settings (e.g., `myapp.worker_config.rrq_settings`).
|
|
177
|
-
|
|
178
|
-
- **`worker watch`**: Run an RRQ worker with auto-restart on file changes in a specified directory.
|
|
179
|
-
```bash
|
|
180
|
-
rrq worker watch [--path <directory>] --settings <settings_path>
|
|
181
|
-
```
|
|
182
|
-
- `--path`: Directory to watch for changes (default: current directory).
|
|
183
|
-
- `--settings`: Python settings path for application worker settings.
|
|
184
|
-
|
|
185
|
-
- **`check`**: Perform a health check on active RRQ workers.
|
|
186
|
-
```bash
|
|
187
|
-
rrq check --settings <settings_path>
|
|
188
|
-
```
|
|
189
|
-
- `--settings`: Python settings path for application settings.
|
|
161
|
+
## Configuration
|
|
190
162
|
|
|
163
|
+
RRQ can be configured in several ways, with the following precedence:
|
|
191
164
|
|
|
192
|
-
|
|
165
|
+
1. **Command-Line Argument (`--settings`)**: Directly specify the settings object path via the CLI. This takes the highest precedence.
|
|
166
|
+
2. **Environment Variable (`RRQ_SETTINGS`)**: Set the `RRQ_SETTINGS` environment variable to point to your settings object path. Used if `--settings` is not provided.
|
|
167
|
+
3. **Default Settings**: If neither of the above is provided, RRQ will instantiate a default `RRQSettings` object, which can still be influenced by environment variables starting with `RRQ_`.
|
|
168
|
+
4. **Environment Variables (Prefix `RRQ_`)**: Individual settings can be overridden by environment variables starting with `RRQ_`, which are automatically picked up by the `RRQSettings` object.
|
|
169
|
+
5. **.env File**: If `python-dotenv` is installed, RRQ will attempt to load a `.env` file from the current working directory or parent directories. System environment variables take precedence over `.env` variables.
|
|
193
170
|
|
|
194
|
-
The
|
|
171
|
+
**Important Note on `job_registry`**: The `job_registry` attribute in your `RRQSettings` object is **critical** for RRQ to function. It must be an instance of `JobRegistry` and is used to register job handlers. Without a properly configured `job_registry`, workers will not know how to process jobs, and most operations will fail. Ensure it is set in your settings object to map job names to their respective handler functions.
|
|
195
172
|
|
|
196
|
-
```bash
|
|
197
|
-
rrq worker run --settings myapp.worker_config.rrq_settings
|
|
198
|
-
```
|
|
199
173
|
|
|
200
|
-
|
|
174
|
+
## Core Components
|
|
201
175
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
176
|
+
* **`RRQClient` (`client.py`)**: Used to enqueue jobs onto specific queues. Supports deferring jobs (by time delta or specific datetime), assigning custom job IDs, and enforcing job uniqueness via keys.
|
|
177
|
+
* **`RRQWorker` (`worker.py`)**: The process that polls queues, fetches jobs, executes the corresponding handler functions, and manages the job lifecycle based on success, failure, retries, or timeouts. Handles graceful shutdown via signals (SIGINT, SIGTERM).
|
|
178
|
+
* **`JobRegistry` (`registry.py`)**: A simple registry to map string function names (used when enqueuing) to the actual asynchronous handler functions the worker should execute.
|
|
179
|
+
* **`JobStore` (`store.py`)**: An abstraction layer handling all direct interactions with Redis. It manages job definitions (Hashes), queues (Sorted Sets), processing locks (Strings with TTL), unique job locks, and worker health checks.
|
|
180
|
+
* **`Job` (`job.py`)**: A Pydantic model representing a job, containing its ID, handler name, arguments, status, retry counts, timestamps, results, etc.
|
|
181
|
+
* **`JobStatus` (`job.py`)**: An Enum defining the possible states of a job (`PENDING`, `ACTIVE`, `COMPLETED`, `FAILED`, `RETRYING`).
|
|
182
|
+
* **`RRQSettings` (`settings.py`)**: A Pydantic `BaseSettings` model for configuring RRQ behavior (Redis DSN, queue names, timeouts, retry policies, concurrency, etc.). Loadable from environment variables (prefix `RRQ_`).
|
|
183
|
+
* **`constants.py`**: Defines shared constants like Redis key prefixes and default configuration values.
|
|
184
|
+
* **`exc.py`**: Defines custom exceptions, notably `RetryJob` which handlers can raise to explicitly request a retry, potentially with a custom delay.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
rrq/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
-
rrq/cli.py,sha256=
|
|
2
|
+
rrq/cli.py,sha256=_LbaAH_w2a0VNRR0EctuE4afl-wccvMY2w2VbehFDEQ,16980
|
|
3
3
|
rrq/client.py,sha256=5_bmZ05LKIfY9WFSKU-nYawEupsnrnHT2HewXfC2Ahg,7831
|
|
4
4
|
rrq/constants.py,sha256=F_uZgBI3h00MctnEjBjiCGMrg5jUaz5Bz9I1vkyqNrs,1654
|
|
5
5
|
rrq/exc.py,sha256=NJq3C7pUfcd47AB8kghIN8vdY0l90UrsHQEg4McBHP8,1281
|
|
@@ -8,8 +8,8 @@ rrq/registry.py,sha256=E9W_zx3QiKTBwMOGearaNpDKBDB87JIn0RlMQ3sAcP0,2925
|
|
|
8
8
|
rrq/settings.py,sha256=BPKP4XjG7z475gqRgHZt4-IvvOs8uZefq4fPfD2Bepk,4350
|
|
9
9
|
rrq/store.py,sha256=teO0Af8hzBiu7-dFn6_2lz5X90LAZXmtg0VDZuQoAwk,24972
|
|
10
10
|
rrq/worker.py,sha256=y0UTziZVh4QbOPv24b8cqbm_xDBM0HtJLwPNYsJPWnE,40706
|
|
11
|
-
rrq-0.3.
|
|
12
|
-
rrq-0.3.
|
|
13
|
-
rrq-0.3.
|
|
14
|
-
rrq-0.3.
|
|
15
|
-
rrq-0.3.
|
|
11
|
+
rrq-0.3.7.dist-info/METADATA,sha256=fqMod1pTxebf7d4fCh5vRK0o9gkD4OAYg-02TNHJfN4,10193
|
|
12
|
+
rrq-0.3.7.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
13
|
+
rrq-0.3.7.dist-info/entry_points.txt,sha256=f8eFjk2ygDSyu9USwXGj5IM8xeyQqZgDa1rSrCj4Mis,36
|
|
14
|
+
rrq-0.3.7.dist-info/licenses/LICENSE,sha256=XDvu5hKdS2-_ByiSj3tiu_3zSsrXXoJsgbILGoMpKCw,554
|
|
15
|
+
rrq-0.3.7.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|