/
base.py
70 lines (52 loc) · 2.2 KB
/
base.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
from typing import Iterable, Set, Any # noqa
import prefect
class RunConfig:
"""
Base class for RunConfigs.
A "run config" is an object for configuring a flow run, which maps to a
specific agent backend.
Args:
- env (dict, optional): Additional environment variables to set
- labels (Iterable[str], optional): an iterable of labels to apply to this
run config. Labels are string identifiers used by Prefect Agents
for selecting valid flow runs when polling for work
"""
def __init__(self, env: dict = None, labels: Iterable[str] = None):
self.env = env
self.labels = set(labels) if labels else set() # Set[str]
def serialize(self) -> dict:
"""
Returns a serialized version of the RunConfig.
Returns:
- dict: the serialized RunConfig
"""
schema = prefect.serialization.run_config.RunConfigSchema()
return schema.dump(self)
def __eq__(self, other: Any) -> Any:
"""This equality check is _only_ exposed for testing"""
if not isinstance(other, RunConfig):
return NotImplemented
return self.serialize() == other.serialize()
class UniversalRun(RunConfig):
"""
Configure a flow-run to run universally on any Agent.
Unlike the other agent-specific `RunConfig` classes (e.g. `LocalRun` for
the Local Agent), the `UniversalRun` run config is compatible with any
agent. This can be useful for flows that don't require any custom
configuration other than flow labels, allowing for transitioning a flow
between agent types without any config changes.
Args:
- env (dict, optional): Additional environment variables to set
- labels (Iterable[str], optional): an iterable of labels to apply to this
run config. Labels are string identifiers used by Prefect Agents
for selecting valid flow runs when polling for work
Examples:
Use the defaults set on the agent:
```python
flow.run_config = UniversalRun()
```
Configure additional labels:
```python
flow.run_config = UniversalRun(env={"SOME_VAR": "value"}, labels=["label-1", "label-2"])
```
"""