-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
state.py
181 lines (130 loc) · 4.46 KB
/
state.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
import json
from typing import Any, Dict
from marshmallow import fields, post_load, ValidationError
from prefect.engine import state
from prefect.utilities.collections import DotDict
from prefect.utilities.serialization import (
JSONCompatible,
OneOfSchema,
ObjectSchema,
to_qualified_name,
)
def reset_value(value, is_raw):
if is_raw:
value = None
else:
try:
json.dumps(value)
except TypeError:
raise TypeError(
"The serialized result of a ResultSerializer must be JSON-compatible."
)
return value
class ResultSerializerField(fields.Field):
def _serialize(self, value, attr, obj, **kwargs):
if hasattr(obj, "_metadata"):
if attr == "cached_inputs":
for var in value or {}:
value[var] = reset_value(
value[var], obj._metadata[attr][var]["raw"]
)
else:
value = reset_value(value, obj._metadata[attr]["raw"])
return super()._serialize(value, attr, obj, **kwargs)
class BaseStateSchema(ObjectSchema):
class Meta:
object_class = state.State
message = fields.String(allow_none=True)
_metadata = fields.Dict(keys=fields.Str())
result = ResultSerializerField(allow_none=True)
@post_load
def create_object(self, data):
_metadata = data.pop("_metadata", {})
base_obj = super().create_object(data)
base_obj._metadata.update(_metadata)
return base_obj
class PendingSchema(BaseStateSchema):
class Meta:
object_class = state.Pending
cached_inputs = ResultSerializerField(allow_none=True)
class SubmittedSchema(BaseStateSchema):
class Meta:
object_class = state.Submitted
state = fields.Nested("StateSchema", allow_none=True)
class ScheduledSchema(PendingSchema):
class Meta:
object_class = state.Scheduled
start_time = fields.DateTime(allow_none=True)
class ResumeSchema(ScheduledSchema):
class Meta:
object_class = state.Resume
class RetryingSchema(ScheduledSchema):
class Meta:
object_class = state.Retrying
run_count = fields.Int(allow_none=True)
class RunningSchema(BaseStateSchema):
class Meta:
object_class = state.Running
class FinishedSchema(BaseStateSchema):
class Meta:
object_class = state.Finished
class SuccessSchema(FinishedSchema):
class Meta:
object_class = state.Success
class CachedSchema(SuccessSchema):
class Meta:
object_class = state.Cached
cached_inputs = ResultSerializerField(allow_none=True)
cached_parameters = JSONCompatible(allow_none=True)
cached_result_expiration = fields.DateTime(allow_none=True)
class MappedSchema(SuccessSchema):
class Meta:
exclude = ["result", "map_states"]
object_class = state.Mapped
# though this field is excluded from serialization, it must be present in the schema
map_states = fields.Nested("StateSchema", many=True)
n_map_states = fields.Integer()
@post_load
def create_object(self, data):
n_map_states = data.pop("n_map_states", 0)
data["map_states"] = [None for _ in range(n_map_states)]
return super().create_object(data)
class FailedSchema(FinishedSchema):
class Meta:
object_class = state.Failed
class TimedOutSchema(FinishedSchema):
class Meta:
object_class = state.TimedOut
cached_inputs = ResultSerializerField(allow_none=True)
class TriggerFailedSchema(FailedSchema):
class Meta:
object_class = state.TriggerFailed
class SkippedSchema(SuccessSchema):
class Meta:
object_class = state.Skipped
exclude_fields = ["cached"]
class PausedSchema(PendingSchema):
class Meta:
object_class = state.Paused
class StateSchema(OneOfSchema):
"""
Field that chooses between several nested schemas
"""
# map class name to schema
type_schemas = {
"Cached": CachedSchema,
"Failed": FailedSchema,
"Finished": FinishedSchema,
"Mapped": MappedSchema,
"Paused": PausedSchema,
"Pending": PendingSchema,
"Resume": ResumeSchema,
"Retrying": RetryingSchema,
"Running": RunningSchema,
"Scheduled": ScheduledSchema,
"Skipped": SkippedSchema,
"Submitted": SubmittedSchema,
"Success": SuccessSchema,
"TimedOut": TimedOutSchema,
"TriggerFailed": TriggerFailedSchema,
}