2023-02-27 18:01:07 +00:00
|
|
|
import traceback
|
2023-03-03 06:02:00 +00:00
|
|
|
from threading import Event, Thread
|
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
from ..invocations.baseinvocation import InvocationContext
|
|
|
|
from .invocation_queue import InvocationQueueItem
|
|
|
|
from .invoker import InvocationProcessorABC, Invoker
|
|
|
|
|
|
|
|
|
|
|
|
class DefaultInvocationProcessor(InvocationProcessorABC):
|
|
|
|
__invoker_thread: Thread
|
|
|
|
__stop_event: Event
|
|
|
|
__invoker: Invoker
|
|
|
|
|
|
|
|
def start(self, invoker) -> None:
|
|
|
|
self.__invoker = invoker
|
|
|
|
self.__stop_event = Event()
|
|
|
|
self.__invoker_thread = Thread(
|
2023-03-03 06:02:00 +00:00
|
|
|
name="invoker_processor",
|
|
|
|
target=self.__process,
|
|
|
|
kwargs=dict(stop_event=self.__stop_event),
|
|
|
|
)
|
|
|
|
self.__invoker_thread.daemon = (
|
|
|
|
True # TODO: probably better to just not use threads?
|
2022-12-01 05:33:20 +00:00
|
|
|
)
|
|
|
|
self.__invoker_thread.start()
|
|
|
|
|
|
|
|
def stop(self, *args, **kwargs) -> None:
|
|
|
|
self.__stop_event.set()
|
|
|
|
|
|
|
|
def __process(self, stop_event: Event):
|
|
|
|
try:
|
|
|
|
while not stop_event.is_set():
|
2023-02-25 04:11:28 +00:00
|
|
|
queue_item: InvocationQueueItem = self.__invoker.services.queue.get()
|
2023-03-03 06:02:00 +00:00
|
|
|
if not queue_item: # Probably stopping
|
2022-12-01 05:33:20 +00:00
|
|
|
continue
|
|
|
|
|
2023-03-03 06:02:00 +00:00
|
|
|
graph_execution_state = (
|
|
|
|
self.__invoker.services.graph_execution_manager.get(
|
|
|
|
queue_item.graph_execution_state_id
|
|
|
|
)
|
|
|
|
)
|
|
|
|
invocation = graph_execution_state.execution_graph.get_node(
|
|
|
|
queue_item.invocation_id
|
|
|
|
)
|
2022-12-01 05:33:20 +00:00
|
|
|
|
|
|
|
# Send starting event
|
|
|
|
self.__invoker.services.events.emit_invocation_started(
|
2023-03-03 06:02:00 +00:00
|
|
|
graph_execution_state_id=graph_execution_state.id,
|
|
|
|
invocation_id=invocation.id,
|
2022-12-01 05:33:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Invoke
|
|
|
|
try:
|
2023-03-03 06:02:00 +00:00
|
|
|
outputs = invocation.invoke(
|
|
|
|
InvocationContext(
|
|
|
|
services=self.__invoker.services,
|
|
|
|
graph_execution_state_id=graph_execution_state.id,
|
|
|
|
)
|
|
|
|
)
|
2022-12-01 05:33:20 +00:00
|
|
|
|
|
|
|
# Save outputs and history
|
|
|
|
graph_execution_state.complete(invocation.id, outputs)
|
|
|
|
|
|
|
|
# Save the state changes
|
2023-03-03 06:02:00 +00:00
|
|
|
self.__invoker.services.graph_execution_manager.set(
|
|
|
|
graph_execution_state
|
|
|
|
)
|
2022-12-01 05:33:20 +00:00
|
|
|
|
|
|
|
# Send complete event
|
|
|
|
self.__invoker.services.events.emit_invocation_complete(
|
2023-03-03 06:02:00 +00:00
|
|
|
graph_execution_state_id=graph_execution_state.id,
|
|
|
|
invocation_id=invocation.id,
|
|
|
|
result=outputs.dict(),
|
2022-12-01 05:33:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
2023-02-27 18:01:07 +00:00
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
except Exception as e:
|
2023-02-27 18:01:07 +00:00
|
|
|
error = traceback.format_exc()
|
|
|
|
|
|
|
|
# Save error
|
|
|
|
graph_execution_state.set_node_error(invocation.id, error)
|
|
|
|
|
|
|
|
# Save the state changes
|
2023-03-03 06:02:00 +00:00
|
|
|
self.__invoker.services.graph_execution_manager.set(
|
|
|
|
graph_execution_state
|
|
|
|
)
|
2023-02-27 18:01:07 +00:00
|
|
|
|
|
|
|
# Send error event
|
|
|
|
self.__invoker.services.events.emit_invocation_error(
|
2023-03-03 06:02:00 +00:00
|
|
|
graph_execution_state_id=graph_execution_state.id,
|
|
|
|
invocation_id=invocation.id,
|
|
|
|
error=error,
|
|
|
|
)
|
2023-02-27 18:01:07 +00:00
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
pass
|
|
|
|
|
2023-02-27 18:01:07 +00:00
|
|
|
# Queue any further commands if invoking all
|
|
|
|
is_complete = graph_execution_state.is_complete()
|
|
|
|
if queue_item.invoke_all and not is_complete:
|
2023-03-03 06:02:00 +00:00
|
|
|
self.__invoker.invoke(graph_execution_state, invoke_all=True)
|
2023-02-27 18:01:07 +00:00
|
|
|
elif is_complete:
|
2023-03-03 06:02:00 +00:00
|
|
|
self.__invoker.services.events.emit_graph_execution_complete(
|
|
|
|
graph_execution_state.id
|
|
|
|
)
|
2023-02-27 18:01:07 +00:00
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
except KeyboardInterrupt:
|
2023-03-03 06:02:00 +00:00
|
|
|
... # Log something?
|