2022-12-01 05:33:20 +00:00
|
|
|
# Copyright (c) 2022 Kyle Schouviller (https://github.com/kyle0654)
|
|
|
|
|
2023-04-13 04:23:15 +00:00
|
|
|
import time
|
2022-12-01 05:33:20 +00:00
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
from queue import Queue
|
2023-04-14 04:56:17 +00:00
|
|
|
|
|
|
|
from pydantic import BaseModel, Field
|
2023-07-03 16:17:45 +00:00
|
|
|
from typing import Optional
|
2022-12-01 05:33:20 +00:00
|
|
|
|
2023-07-27 14:54:01 +00:00
|
|
|
|
2023-04-13 07:44:44 +00:00
|
|
|
class InvocationQueueItem(BaseModel):
|
2023-04-16 03:29:18 +00:00
|
|
|
graph_execution_state_id: str = Field(description="The ID of the graph execution state")
|
|
|
|
invocation_id: str = Field(description="The ID of the node being invoked")
|
2023-04-16 03:19:30 +00:00
|
|
|
invoke_all: bool = Field(default=False)
|
2023-04-13 07:44:44 +00:00
|
|
|
timestamp: float = Field(default_factory=time.time)
|
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
|
|
|
|
class InvocationQueueABC(ABC):
|
|
|
|
"""Abstract base class for all invocation queues"""
|
2023-03-03 06:02:00 +00:00
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
@abstractmethod
|
|
|
|
def get(self) -> InvocationQueueItem:
|
|
|
|
pass
|
2023-03-03 06:02:00 +00:00
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
@abstractmethod
|
2023-07-03 16:17:45 +00:00
|
|
|
def put(self, item: Optional[InvocationQueueItem]) -> None:
|
2022-12-01 05:33:20 +00:00
|
|
|
pass
|
|
|
|
|
2023-03-17 03:05:36 +00:00
|
|
|
@abstractmethod
|
|
|
|
def cancel(self, graph_execution_state_id: str) -> None:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def is_canceled(self, graph_execution_state_id: str) -> bool:
|
|
|
|
pass
|
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
|
|
|
|
class MemoryInvocationQueue(InvocationQueueABC):
|
|
|
|
__queue: Queue
|
2023-03-17 03:05:36 +00:00
|
|
|
__cancellations: dict[str, float]
|
2022-12-01 05:33:20 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.__queue = Queue()
|
2023-03-17 03:05:36 +00:00
|
|
|
self.__cancellations = dict()
|
2023-03-03 06:02:00 +00:00
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
def get(self) -> InvocationQueueItem:
|
2023-03-17 03:05:36 +00:00
|
|
|
item = self.__queue.get()
|
|
|
|
|
|
|
|
while (
|
|
|
|
isinstance(item, InvocationQueueItem)
|
|
|
|
and item.graph_execution_state_id in self.__cancellations
|
|
|
|
and self.__cancellations[item.graph_execution_state_id] > item.timestamp
|
|
|
|
):
|
|
|
|
item = self.__queue.get()
|
|
|
|
|
|
|
|
# Clear old items
|
|
|
|
for graph_execution_state_id in list(self.__cancellations.keys()):
|
|
|
|
if self.__cancellations[graph_execution_state_id] < item.timestamp:
|
|
|
|
del self.__cancellations[graph_execution_state_id]
|
|
|
|
|
|
|
|
return item
|
2023-03-03 06:02:00 +00:00
|
|
|
|
2023-07-03 16:17:45 +00:00
|
|
|
def put(self, item: Optional[InvocationQueueItem]) -> None:
|
2022-12-01 05:33:20 +00:00
|
|
|
self.__queue.put(item)
|
2023-03-17 03:05:36 +00:00
|
|
|
|
|
|
|
def cancel(self, graph_execution_state_id: str) -> None:
|
|
|
|
if graph_execution_state_id not in self.__cancellations:
|
|
|
|
self.__cancellations[graph_execution_state_id] = time.time()
|
|
|
|
|
|
|
|
def is_canceled(self, graph_execution_state_id: str) -> bool:
|
|
|
|
return graph_execution_state_id in self.__cancellations
|