The QueueManager class.
| 15 | |
| 16 | |
| 17 | class QueueManager: |
| 18 | """The QueueManager class.""" |
| 19 | |
| 20 | def __init__(self, hass: HomeAssistant) -> None: |
| 21 | self.hass = hass |
| 22 | self.queue: list[Coroutine] = [] |
| 23 | self.running = False |
| 24 | |
| 25 | @property |
| 26 | def pending_tasks(self) -> int: |
| 27 | """Return a count of pending tasks in the queue.""" |
| 28 | return len(self.queue) |
| 29 | |
| 30 | @property |
| 31 | def has_pending_tasks(self) -> bool: |
| 32 | """Return a count of pending tasks in the queue.""" |
| 33 | return self.pending_tasks != 0 |
| 34 | |
| 35 | def clear(self) -> None: |
| 36 | """Clear the queue.""" |
| 37 | self.queue = [] |
| 38 | |
| 39 | def add(self, task: Coroutine) -> None: |
| 40 | """Add a task to the queue.""" |
| 41 | self.queue.append(task) |
| 42 | |
| 43 | async def execute(self, number_of_tasks: int | None = None) -> None: |
| 44 | """Execute the tasks in the queue.""" |
| 45 | if self.running: |
| 46 | _LOGGER.debug("<QueueManager> Execution is already running") |
| 47 | raise HacsExecutionStillInProgress |
| 48 | if len(self.queue) == 0: |
| 49 | _LOGGER.debug("<QueueManager> The queue is empty") |
| 50 | return |
| 51 | |
| 52 | self.running = True |
| 53 | |
| 54 | _LOGGER.debug("<QueueManager> Checking out tasks to execute") |
| 55 | local_queue = [] |
| 56 | |
| 57 | if number_of_tasks: |
| 58 | for task in self.queue[:number_of_tasks]: |
| 59 | local_queue.append(task) |
| 60 | else: |
| 61 | for task in self.queue: |
| 62 | local_queue.append(task) |
| 63 | |
| 64 | _LOGGER.debug("<QueueManager> Starting queue execution for %s tasks", len(local_queue)) |
| 65 | start = time.time() |
| 66 | result = await asyncio.gather(*local_queue, return_exceptions=True) |
| 67 | for entry in result: |
| 68 | if isinstance(entry, Exception): |
| 69 | _LOGGER.error("<QueueManager> %s", entry) |
| 70 | end = time.time() - start |
| 71 | |
| 72 | for task in local_queue: |
| 73 | self.queue.remove(task) |
| 74 |
no outgoing calls
searching dependent graphs…