Queue reference¶
Sheppy provides a Queue class to manage and execute background tasks. The queue supports adding tasks, scheduling them for future execution, retrying failed tasks, and managing periodic tasks using cron expressions.
See Getting Started guide for more details and examples.
sheppy.Queue
¶
Queue(backend: Backend, name: str = 'default')
Queue class provides an easy way to manage task queue.
| PARAMETER | DESCRIPTION |
|---|---|
backend
|
An instance of task backend (e.g.
TYPE:
|
name
|
Name of the queue
TYPE:
|
Source code in src/sheppy/queue.py
19 20 21 | |
add
¶
Add task into the queue. Accept list of tasks for batch add.
| PARAMETER | DESCRIPTION |
|---|---|
task
|
Instance of a Task, or list of Task instances for batch mode. |
| RETURNS | DESCRIPTION |
|---|---|
bool | list[bool]
|
Success boolean, or list of booleans in batch mode. |
Example
q = Queue(...)
success = await q.add(task)
assert success is True
# batch mode
success = await q.add([task1, task2])
assert success == [True, True] # returns list of booleans in batch mode
Source code in src/sheppy/queue.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
schedule
¶
schedule(task: Task, at: datetime | timedelta) -> bool
Schedule task to be processed after certain time.
| PARAMETER | DESCRIPTION |
|---|---|
task
|
Instance of a Task
TYPE:
|
at
|
When to process the task.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
Success boolean |
Example
from datetime import datetime, timedelta
q = Queue(...)
# schedule task to be processed after 10 minutes
await q.schedule(task, timedelta(minutes=10))
# ... or at specific time
await q.schedule(task, datetime.fromisoformat("2026-01-01 00:00:00 +00:00"))
Source code in src/sheppy/queue.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
get_task
¶
Get task by id.
| PARAMETER | DESCRIPTION |
|---|---|
task
|
Instance of a Task or its ID, or list of Task instances/IDs for batch mode. |
| RETURNS | DESCRIPTION |
|---|---|
Task | None
|
Instance of a Task or None if not found. |
dict[UUID, Task]
|
(In batch mode) Returns Dictionary of Task IDs to Task instances. |
Source code in src/sheppy/queue.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
wait_for
¶
Wait for task to complete and return updated task instance.
| PARAMETER | DESCRIPTION |
|---|---|
task
|
Instance of a Task or its ID, or list of Task instances/IDs for batch mode. |
timeout
|
Maximum time to wait in seconds. Default is 0 (wait indefinitely).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[UUID, Task] | Task | None
|
Instance of a Task or None if not found or timeout reached. |
| RAISES | DESCRIPTION |
|---|---|
TimeoutError
|
If timeout is reached and no task is found (only in non-batch mode). |
Example
q = Queue(...)
# wait indefinitely for task to complete
updated_task = await q.wait_for(task)
assert updated_task.completed is True
# wait up to 5 seconds for task to complete
try:
updated_task = await q.wait_for(task, timeout=5)
if updated_task:
assert updated_task.completed is True
else:
print("Task not found or still pending after timeout")
except TimeoutError:
print("Task did not complete within timeout")
# batch mode
updated_tasks = await q.wait_for([task1, task2, task3], timeout=10)
for task_id, task in updated_tasks.items():
print(f"Task {task_id} completed: {task.completed}")
# Note: updated_tasks may contain only a subset of tasks if timeout is reached
Source code in src/sheppy/queue.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | |
get_all_tasks
¶
get_all_tasks() -> list[Task]
Get all tasks, including completed/failed ones.
| RETURNS | DESCRIPTION |
|---|---|
list[Task]
|
List of all tasks |
Source code in src/sheppy/queue.py
91 92 93 94 95 96 97 98 99 | |
get_scheduled
¶
get_scheduled() -> list[Task]
List scheduled tasks.
| RETURNS | DESCRIPTION |
|---|---|
list[Task]
|
List of scheduled tasks |
Source code in src/sheppy/queue.py
153 154 155 156 157 158 159 160 | |
get_pending
¶
get_pending(count: int = 1) -> list[Task]
List pending tasks.
| PARAMETER | DESCRIPTION |
|---|---|
count
|
Number of pending tasks to retrieve.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Task]
|
List of pending tasks |
Source code in src/sheppy/queue.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
retry
¶
retry(
task: Task | UUID | str,
at: datetime | timedelta | None = None,
force: bool = False,
) -> bool
Retry failed task.
| PARAMETER | DESCRIPTION |
|---|---|
task
|
Instance of a Task or its ID
TYPE:
|
at
|
When to retry the task.
TYPE:
|
force
|
If True, allows retrying even if task has completed successfully. Defaults to False.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
Success boolean |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If task has already completed successfully and force is not set to True. |
TypeError
|
If provided datetime is not offset-aware. |
Example
q = Queue(...)
# retry task immediately
success = await q.retry(task)
assert success is True
# or retry after 5 minutes
await q.retry(task, at=timedelta(minutes=5))
# or at specific time
await q.retry(task, at=datetime.fromisoformat("2026-01-01 00:00:00 +00:00"))
# force retry even if task is completed (= finished successfully)
await q.retry(task, force=True)
Source code in src/sheppy/queue.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | |
size
¶
size() -> int
Get number of pending tasks in the queue.
| RETURNS | DESCRIPTION |
|---|---|
int
|
Number of pending tasks |
Example
q = Queue(...)
await q.add(task)
count = await q.size()
assert count == 1
Source code in src/sheppy/queue.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | |
clear
¶
clear() -> int
Clear all tasks, including completed ones.
Source code in src/sheppy/queue.py
308 309 310 311 | |
add_cron
¶
add_cron(task: Task, cron: str) -> bool
Add a cron job to run a task on a schedule.
| PARAMETER | DESCRIPTION |
|---|---|
task
|
Instance of a Task
TYPE:
|
cron
|
Cron expression string (e.g. "*/5 * * * *" to run every 5 minutes)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
Success boolean |
Example
q = Queue(...)
@task
async def say_hello(to: str) -> str:
print(f"[{datetime.now()}] Hello, {to}!")
# schedule task to run every minute
await q.add_cron(say_hello("World"), "* * * * *")
Source code in src/sheppy/queue.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | |
delete_cron
¶
delete_cron(task: Task, cron: str) -> bool
Delete a cron job.
| PARAMETER | DESCRIPTION |
|---|---|
task
|
Instance of a Task
TYPE:
|
cron
|
Cron expression string used when adding the cron job
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
Success boolean |
Example
q = Queue(...)
# delete previously added cron job
success = await q.delete_cron(say_hello("World"), "* * * * *")
assert success is True
Source code in src/sheppy/queue.py
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | |
get_crons
¶
get_crons() -> list[TaskCron]
List all cron jobs.
| RETURNS | DESCRIPTION |
|---|---|
list[TaskCron]
|
List of TaskCron instances |
Example
q = Queue(...)
crons = await q.get_crons()
for cron in crons:
print(f"Cron ID: {cron.id}, Expression: {cron.expression}, TaskSpec: {cron.spec}")
Source code in src/sheppy/queue.py
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | |