|
| 1 | +============= |
| 2 | +API Reference |
| 3 | +============= |
| 4 | + |
| 5 | +Steady Queue's public API is intentionally small. Most of the interface comes |
| 6 | +from Django's ``django.tasks`` module (see the `Django tasks documentation |
| 7 | +<https://docs.djangoproject.com/en/stable/ref/tasks/>`_). Steady Queue adds |
| 8 | +two decorators and a handful of module-level settings. |
| 9 | + |
| 10 | +.. contents:: On this page |
| 11 | + :local: |
| 12 | + :depth: 2 |
| 13 | + |
| 14 | + |
| 15 | +.. _api-recurring: |
| 16 | + |
| 17 | +@recurring |
| 18 | +---------- |
| 19 | + |
| 20 | +.. autofunction:: steady_queue.recurring_task.recurring |
| 21 | + |
| 22 | +The ``@recurring`` decorator registers a task to be enqueued automatically on |
| 23 | +a cron schedule. It must be applied *outside* the ``@task()`` decorator: |
| 24 | + |
| 25 | +.. code-block:: python |
| 26 | +
|
| 27 | + from django.tasks import task |
| 28 | + from steady_queue.recurring_task import recurring |
| 29 | +
|
| 30 | + @recurring(schedule="0 9 * * 1-5", key="weekly_report") |
| 31 | + @task() |
| 32 | + def weekly_report(): |
| 33 | + ... |
| 34 | +
|
| 35 | +The same task can have multiple recurring schedules: |
| 36 | + |
| 37 | +.. code-block:: python |
| 38 | +
|
| 39 | + @recurring(schedule="0 9 * * *", args=("Alice",), key="greet_alice") |
| 40 | + @recurring(schedule="0 12 * * *", args=("Bob",), key="greet_bob") |
| 41 | + @task() |
| 42 | + def greet(name: str): |
| 43 | + print(f"Hello, {name}!") |
| 44 | +
|
| 45 | +Parameters: |
| 46 | + |
| 47 | +``schedule`` |
| 48 | + A crontab expression (anything understood by the `crontab |
| 49 | + <https://pypi.org/project/crontab/>`_ library). For example: |
| 50 | + |
| 51 | + - ``"* * * * *"`` — every minute |
| 52 | + - ``"0 9 * * 1-5"`` — 9am on weekdays |
| 53 | + - ``"@daily"`` — once a day at midnight |
| 54 | + |
| 55 | +``key`` |
| 56 | + A unique string identifier for this recurring configuration. Must be |
| 57 | + unique across all ``@recurring`` decorators in your codebase. Used to |
| 58 | + prevent duplicate runs when multiple schedulers are active. |
| 59 | + |
| 60 | +``args`` |
| 61 | + Positional arguments to pass to the task when it is enqueued. Defaults to |
| 62 | + no arguments. |
| 63 | + |
| 64 | +``kwargs`` |
| 65 | + Keyword arguments to pass to the task when it is enqueued. |
| 66 | + |
| 67 | +``queue_name`` |
| 68 | + The queue to enqueue the task on. If omitted, uses the queue from the |
| 69 | + ``@task()`` decorator or the default queue. |
| 70 | + |
| 71 | +``priority`` |
| 72 | + Numeric priority for the enqueued task. If omitted, uses the priority from |
| 73 | + the ``@task()`` decorator or ``0``. |
| 74 | + |
| 75 | +``description`` |
| 76 | + Optional human-readable description. Currently unused but stored for |
| 77 | + future tooling. |
| 78 | + |
| 79 | + |
| 80 | +.. _api-limits-concurrency: |
| 81 | + |
| 82 | +@limits_concurrency |
| 83 | +------------------- |
| 84 | + |
| 85 | +.. autofunction:: steady_queue.concurrency.limits_concurrency |
| 86 | + |
| 87 | +The ``@limits_concurrency`` decorator restricts how many instances of a task |
| 88 | +can run at the same time. It must be applied *outside* the ``@task()`` |
| 89 | +decorator: |
| 90 | + |
| 91 | +.. code-block:: python |
| 92 | +
|
| 93 | + from django.tasks import task |
| 94 | + from steady_queue.concurrency import limits_concurrency |
| 95 | +
|
| 96 | + @limits_concurrency(key=lambda user_id: str(user_id), to=1) |
| 97 | + @task() |
| 98 | + def generate_report(user_id: int): |
| 99 | + ... |
| 100 | +
|
| 101 | +Parameters: |
| 102 | + |
| 103 | +``key`` |
| 104 | + **Required.** A string or a callable that accepts the same arguments as |
| 105 | + the task and returns a string. Tasks with the same key value are counted |
| 106 | + together for the concurrency limit. |
| 107 | + |
| 108 | +``to`` |
| 109 | + Maximum number of tasks with the same key that may run simultaneously. |
| 110 | + Defaults to ``1``. |
| 111 | + |
| 112 | +``duration`` |
| 113 | + How long the concurrency guarantee is held. If a task holds a concurrency |
| 114 | + slot for longer than this, the slot may be released by the dispatcher's |
| 115 | + maintenance pass. Defaults to |
| 116 | + ``steady_queue.default_concurrency_control_period`` (3 minutes). |
| 117 | + |
| 118 | +``group`` |
| 119 | + A string used to apply a shared concurrency limit across different task |
| 120 | + types. Tasks from different functions that share the same ``group`` and |
| 121 | + ``key`` value count against the same limit. Defaults to the task's module |
| 122 | + path. |
| 123 | + |
| 124 | + |
| 125 | +Argument serialization |
| 126 | +---------------------- |
| 127 | + |
| 128 | +Task functions accept almost any argument type as positional or keyword |
| 129 | +arguments. Beyond the standard DEP 0014 serializable types, Steady Queue adds |
| 130 | +support for: |
| 131 | + |
| 132 | +- ``datetime`` and ``date`` objects |
| 133 | +- ``timedelta`` objects |
| 134 | +- Django model instances (serialized as content type + primary key) |
| 135 | + |
| 136 | +If a model instance cannot be found in the database when the task is executed, |
| 137 | +a ``steady_queue.arguments.DeserializationError`` is raised. |
| 138 | + |
| 139 | + |
| 140 | +Backend limitations |
| 141 | +------------------- |
| 142 | + |
| 143 | +The ``SteadyQueueBackend`` does not support the following features defined by |
| 144 | +the Django task backend interface: |
| 145 | + |
| 146 | +- **Async enqueueing** — tasks cannot be enqueued from async code. |
| 147 | +- **Result fetching** — ``task_result.return_value`` is not supported. Store |
| 148 | + results directly in your database or file storage if they need to be |
| 149 | + persisted. |
| 150 | + |
| 151 | +These limitations are advertised via the |
| 152 | +`Django task feature flags |
| 153 | +<https://docs.djangoproject.com/en/stable/ref/tasks/#feature-flags>`_. |
0 commit comments