Return tasks from a queue. Args: tiger: TaskTiger instance. queue: Name of the queue. state: State of the task (QUEUED, ACTIVE, SCHEDULED, ERROR). limit: Maximum number of tasks to return. load_executions: Maximum number o
(
cls,
tiger: "TaskTiger",
queue: str,
state: str,
skip: int = 0,
limit: int = 1000,
load_executions: int = 0,
include_not_found: bool = False,
)
| 497 | |
| 498 | @classmethod |
| 499 | def tasks_from_queue( |
| 500 | cls, |
| 501 | tiger: "TaskTiger", |
| 502 | queue: str, |
| 503 | state: str, |
| 504 | skip: int = 0, |
| 505 | limit: int = 1000, |
| 506 | load_executions: int = 0, |
| 507 | include_not_found: bool = False, |
| 508 | ) -> Tuple[int, List["Task"]]: |
| 509 | """ |
| 510 | Return tasks from a queue. |
| 511 | |
| 512 | Args: |
| 513 | tiger: TaskTiger instance. |
| 514 | queue: Name of the queue. |
| 515 | state: State of the task (QUEUED, ACTIVE, SCHEDULED, ERROR). |
| 516 | limit: Maximum number of tasks to return. |
| 517 | load_executions: Maximum number of executions to load for each task |
| 518 | (starting from the latest). |
| 519 | include_not_found: Whether to include tasks that cannot be loaded. |
| 520 | |
| 521 | Returns: |
| 522 | Tuple with the following information: |
| 523 | * total items in the queue |
| 524 | * tasks from the given queue in the given state, latest first. |
| 525 | """ |
| 526 | |
| 527 | key = tiger._key(state, queue) |
| 528 | pipeline = tiger.connection.pipeline() |
| 529 | pipeline.zcard(key) |
| 530 | pipeline.zrange(key, -limit - skip, -1 - skip, withscores=True) |
| 531 | n, items = pipeline.execute() |
| 532 | |
| 533 | tasks = [] |
| 534 | |
| 535 | if items: |
| 536 | tss = [datetime.datetime.utcfromtimestamp(item[1]) for item in items] |
| 537 | if load_executions: |
| 538 | pipeline = tiger.connection.pipeline() |
| 539 | pipeline.mget([tiger._key("task", item[0]) for item in items]) |
| 540 | for item in items: |
| 541 | pipeline.lrange( |
| 542 | tiger._key("task", item[0], "executions"), |
| 543 | -load_executions, |
| 544 | -1, |
| 545 | ) |
| 546 | results = pipeline.execute() |
| 547 | |
| 548 | for idx, serialized_data, serialized_executions, ts in zip( |
| 549 | range(len(items)), results[0], results[1:], tss |
| 550 | ): |
| 551 | if serialized_data is None: |
| 552 | if include_not_found: |
| 553 | data = {"id": items[idx][0]} |
| 554 | else: |
| 555 | data = json.loads(serialized_data) |
| 556 |