This repository was archived by the owner on Apr 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 222
/
Copy pathmessage_types.py
228 lines (174 loc) · 9.11 KB
/
message_types.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import enum
import json
import datetime
def message_dumps(obj):
return json.dumps(obj, cls=MessageJSONEncoder)
def message_loads(obj):
return json.loads(obj, cls=MessageJSONDecoder)
def message_dump(obj, fp):
return json.dump(obj, fp, cls=MessageJSONEncoder)
def message_load(obj):
return json.load(obj, cls=MessageJSONDecoder)
class MessageJSONDecoder(json.JSONDecoder):
""" A custom decoder for decoding JSON strings to Message types. """
def __init__(self, *args, **kwargs):
json.JSONDecoder.__init__(self, object_hook=self.object_hook, *args, **kwargs)
def object_hook(self, o):
if "result" in o and "app_name" in o:
o["status"] = StatusEnum[o["status"]]
return NodeStatusMessage(**o)
elif "workflow_id" in o and "execution_id" in o:
o["status"] = StatusEnum[o["status"]]
return WorkflowStatusMessage(**o)
elif "trigger_data" in o:
return TriggerMessage(**o)
else:
return o
class MessageJSONEncoder(json.JSONEncoder):
""" A custom encoder for encoding Message types to JSON strings. """
def default(self, o):
if isinstance(o, NodeStatusMessage):
r = {"name": o.name, "node_id": o.node_id, "label": o.label, "app_name": o.app_name,
"execution_id": o.execution_id, "result": o.result, "status": o.status,
"started_at": o.started_at, "completed_at": o.completed_at, "combined_id": o.combined_id,
"parameters": o.parameters}
try:
json.dumps(o.result)
except (TypeError, ValueError):
r["result"] = f"Node returned result of type '{type(o.result)}' which is not JSON serializable."
r["status"] = StatusEnum.FAILURE
finally:
return r
elif isinstance(o, WorkflowStatusMessage):
return {"execution_id": o.execution_id, "workflow_id": o.workflow_id, "name": o.name, "status": o.status,
"started_at": o.started_at, "completed_at": o.completed_at, "user": o.user,
"app_name": o.app_name, "action_name": o.action_name, "label": o.label}
elif isinstance(o, TriggerMessage):
return {"trigger_data": o.trigger_data}
elif isinstance(o, JSONPatch):
if o.op in JSONPatchOps:
return {k: getattr(o, k, None) for k in o.__slots__ if getattr(o, k, None) is not None}
else:
raise ValueError("Improper JSON Patch operation")
elif isinstance(o, StatusEnum):
return o.value
elif isinstance(o, JSONPatchOps):
return o.value.lower()
elif isinstance(o, JSONPatch):
return {k: getattr(o, k, None) for k in o.__slots__ if getattr(o, k, None) is not None}
elif isinstance(o, datetime.datetime):
return str(o)
else:
return o
class JSONPatch:
__slots__ = ("op", "path", "value", "from_")
def __init__(self, op=None, path=None, value=None, from_=None):
self.op = op
self.path = path
self.value = value
self.from_ = from_
class JSONPatchOps(str, enum.Enum):
TEST = "test"
REMOVE = "remove"
ADD = "add"
REPLACE = "replace"
MOVE = "move"
COPY = "copy"
class StatusEnum(str, enum.Enum):
""" Holds statuses used for Workflow and Action status messages """
PAUSED = "PAUSED" # not currently implemented but may be if we see a use case
AWAITING_DATA = "AWAITING_DATA" # possibly for triggers?
PENDING = "PENDING"
COMPLETED = "COMPLETED"
ABORTED = "ABORTED"
EXECUTING = "EXECUTING"
SUCCESS = "SUCCESS"
FAILURE = "FAILURE"
class WorkflowStatusMessage(object):
""" Class that formats a WorkflowStatusMessage message """
__slots__ = ("execution_id", "workflow_id", "name", "status", "started_at", "completed_at",
"user", "app_name", "action_name", "label")
def __init__(self, execution_id, workflow_id, name, started_at=None, completed_at=None, status=None,
user=None, app_name=None, action_name=None, label=None):
self.execution_id = execution_id
self.workflow_id = workflow_id
self.name = name
self.status = status
self.started_at = started_at
self.completed_at = completed_at
self.user = user
self.app_name = app_name
self.action_name = action_name
self.label = label
@classmethod
def execution_pending(cls, execution_id, workflow_id, name, user=None, app_name=None, action_name=None, label=None):
return cls(execution_id, workflow_id, name, status=StatusEnum.PENDING, user=user,
app_name=app_name, action_name=action_name, label=label)
@classmethod
def execution_started(cls, execution_id, workflow_id, name, user=None, app_name=None, action_name=None, label=None):
start_time = datetime.datetime.now()
return cls(execution_id, workflow_id, name, started_at=start_time, status=StatusEnum.EXECUTING, user=user,
app_name=app_name, action_name=action_name, label=label)
@classmethod
def execution_continued(cls, execution_id, workflow_id, name, user=None, app_name=None, action_name=None, label=None):
return cls(execution_id, workflow_id, name, status=StatusEnum.EXECUTING, user=user,
app_name=app_name, action_name=action_name, label=label)
@classmethod
def execution_completed(cls, execution_id, workflow_id, name, user=None, app_name=None, action_name=None, label=None):
end_time = datetime.datetime.now()
return cls(execution_id, workflow_id, name, completed_at=end_time, status=StatusEnum.COMPLETED, user=user,
app_name=app_name, action_name=action_name, label=label)
@classmethod
def execution_aborted(cls, execution_id, workflow_id, name, user=None, app_name=None, action_name=None, label=None):
end_time = datetime.datetime.now()
return cls(execution_id, workflow_id, name, completed_at=end_time, status=StatusEnum.ABORTED, user=user,
app_name=app_name, action_name=action_name, label=label)
class NodeStatusMessage(object):
""" Class that formats a NodeStatusMessage message. """
__slots__ = ("name", "node_id", "label", "app_name", "execution_id", "parameters", "combined_id", "result",
"status", "started_at", "completed_at")
def __init__(self, name, node_id, label, app_name, execution_id, combined_id=None, parameters=None, result=None,
status=None, started_at=None, completed_at=None):
self.name = name
self.node_id = node_id
self.label = label
self.app_name = app_name
self.execution_id = execution_id
self.combined_id = combined_id if combined_id is not None else ':'.join((node_id, execution_id))
self.result = result
self.parameters = parameters
self.status = status
self.started_at = started_at
self.completed_at = completed_at
@classmethod
def from_node(cls, node, execution_id, result=None, status=None, started_at=None, completed_at=None, parameters=None):
return cls(node.name, node.id_, node.label, node.app_name, execution_id, result=result,
status=status, started_at=started_at, completed_at=completed_at, parameters=parameters)
@classmethod
def pending_from_node(cls, node, execution_id, parameters=None):
return cls(node.name, node.id_, node.label, node.app_name, execution_id, status=StatusEnum.PENDING,
parameters=parameters)
@classmethod
def executing_from_node(cls, node, execution_id, parameters=None, started_at=None):
return cls(node.name, node.id_, node.label, node.app_name, execution_id, started_at=started_at,
status=StatusEnum.EXECUTING, parameters=parameters)
@classmethod
def success_from_node(cls, node, execution_id, result, parameters=None, started_at=None):
completed_at = datetime.datetime.now()
return cls(node.name, node.id_, node.label, node.app_name, execution_id, result=result, started_at=started_at,
completed_at=completed_at, status=StatusEnum.SUCCESS, parameters=parameters)
@classmethod
def failure_from_node(cls, node, execution_id, result, parameters=None, started_at=None):
completed_at = datetime.datetime.now()
return cls(node.name, node.id_, node.label, node.app_name, execution_id, result=result, started_at=started_at,
completed_at=completed_at, status=StatusEnum.FAILURE, parameters=parameters)
@classmethod
def aborted_from_node(cls, node, execution_id, parameters=None, started_at=None):
completed_at = datetime.datetime.now()
return cls(node.name, node.id_, node.label, node.app_name, execution_id, completed_at=completed_at,
started_at=started_at, status=StatusEnum.ABORTED, parameters=parameters)
class TriggerMessage(object):
""" Class that formats a TriggerMessage. """
__slots__ = ("trigger_data",)
def __init__(self, trigger_data):
self.trigger_data = trigger_data