forked from langgenius/graphon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_handlers.py
More file actions
89 lines (75 loc) · 2.85 KB
/
Copy pathcommand_handlers.py
File metadata and controls
89 lines (75 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import logging
from typing import TYPE_CHECKING, final, override
from graphon.entities.pause_reason import SchedulingPause
from graphon.runtime.graph_runtime_state import GraphExecutionProtocol
from graphon.runtime.variable_pool import VariablePool
from ..entities.commands import (
AbortCommand,
PauseCommand,
UpdateVariablesCommand,
)
from .command_processor import CommandHandler
logger = logging.getLogger(__name__)
@final
class AbortCommandHandler(CommandHandler[AbortCommand]):
@override
def handle(
self,
command: AbortCommand,
execution: GraphExecutionProtocol,
) -> None:
logger.debug("Aborting workflow %s: %s", execution.workflow_id, command.reason)
execution.abort(command.reason or "User requested abort")
@final
class PauseCommandHandler(CommandHandler[PauseCommand]):
@override
def handle(
self,
command: PauseCommand,
execution: GraphExecutionProtocol,
) -> None:
logger.debug("Pausing workflow %s: %s", execution.workflow_id, command.reason)
# Convert string reason to PauseReason if needed
reason = command.reason
pause_reason = SchedulingPause(message=reason)
execution.pause(pause_reason)
@final
class UpdateVariablesCommandHandler(CommandHandler[UpdateVariablesCommand]):
def __init__(self, variable_pool: VariablePool) -> None:
self._variable_pool = variable_pool
@override
def handle(
self,
command: UpdateVariablesCommand,
execution: GraphExecutionProtocol,
) -> None:
for update in command.updates:
try:
variable = update.value
self._variable_pool.add(variable.selector, variable)
logger.debug(
"Updated variable %s for workflow %s",
variable.selector,
execution.workflow_id,
)
except ValueError as exc:
logger.warning(
"Skipping invalid variable selector %s for workflow %s: %s",
getattr(update.value, "selector", None),
execution.workflow_id,
exc,
)
if TYPE_CHECKING:
# static assertions to ensure command handlers implement CommandHandler.
def _assert_abort_handler_protocol(
handler: AbortCommandHandler,
) -> CommandHandler[AbortCommand]: # pyright: ignore[reportUnusedFunction]
return handler
def _assert_pause_handler_protocol(
handler: PauseCommandHandler,
) -> CommandHandler[PauseCommand]: # pyright: ignore[reportUnusedFunction]
return handler
def _assert_update_variables_handler_protocol(
handler: UpdateVariablesCommandHandler,
) -> CommandHandler[UpdateVariablesCommand]: # pyright: ignore[reportUnusedFunction]
return handler