-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_dag_from_filename.py
335 lines (279 loc) · 9.46 KB
/
gen_dag_from_filename.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import asyncio
import datetime
import json
import logging
import time
from datetime import timedelta
from pathlib import Path
from typing import Any, AnyStr, AsyncIterator, Dict, Tuple
import httpx as httpx
import requests as requests
import slugify as slugify
from airflow import DAG
from airflow.models.baseoperator import BaseOperator
from airflow.operators.bash import BashOperator
from airflow.triggers.base import BaseTrigger, TriggerEvent
def fullname(o: object) -> AnyStr:
"""
Return the fully-qualified (dotted form) name of the object from its class.
Serializers often need to know the full name of a class, so this can compute it
from an object.
"""
klass = o.__class__
module = klass.__module__
print(f"MODULE NAME = {module}")
# ^^ module is returning values like "unusual_prefix_8fa4f0feec59eb1abae934dc98f4950682768154_bailey_llc_full"
# but only on the scheduler, not the triggerer
if module.startswith("unusual_prefix"):
parts = module.split("_")
module = "_".join(parts[3:])
name = parts[3][0] + "." + module + "." + klass.__qualname__
else:
name = module + "." + klass.__qualname__
print(f"{__file__}: FULL PYTHON NAME = {name}")
return name
def load_cfg_for_dagfile(f: str) -> (dict, str):
file = Path(f)
root_generator = (
p for p in (Path("../../configs"), Path("./configs")) if p.exists()
)
root = next(root_generator, None)
if not root:
print(f"no root in {list(root_generator)}")
return dict(), None
# print(f"config root is {root}")
subdir = Path(file.name[0])
_parts = file.stem.split("_")
dag_type = _parts[-1].lower()
dag_slug = "_".join(_parts[:-1])
cfg_ext = "json"
cfg_file = root / subdir / Path(f"{dag_slug}_{dag_type}.{cfg_ext}")
cfg = json.load(cfg_file.open("r"))
return cfg, dag_type
cfg, dag_type = load_cfg_for_dagfile(__file__)
if not cfg:
exit(-1)
def tree(l: list) -> list:
if not len(l):
return
nextlevel = []
if l:
left = l.pop(0)
nextlevel.append([left, []])
if l:
right = l.pop(0)
nextlevel.append([right, []])
if len(l):
for idx, n in enumerate(nextlevel):
sub = tree(l)
if sub:
nextlevel[idx][1].append(sub)
else:
del nextlevel[idx][1] # remove empty list
else:
for idx, n in enumerate(nextlevel):
del nextlevel[idx][1] # remove empty list
if nextlevel:
while isinstance(nextlevel, list) and len(nextlevel) == 1:
nextlevel = nextlevel[0]
return nextlevel
return
def unwrap(l):
# print(f"unwrapping {l} --> ", end='')
if not isinstance(l, list):
pass
else:
while isinstance(l, list) and len(l) == 1:
l = unwrap(l[0])
# print(f" {l}")
return l
def emit(s: BaseOperator, b: BaseOperator, e: BaseOperator):
out = f"{s} >> {b}"
b.set_upstream(s)
if e:
out += f" {e}"
e.set_upstream(b)
print(out)
def ct2(start, t, end, indent=0):
l = unwrap(t)
if not isinstance(l, list):
emit(start, l, end)
else:
for n in l:
if len(n[1:]):
ct2(start, n[0], None, indent=indent + 3)
else:
ct2(start, n[0], end, indent=indent + 3)
for m in n[1:]:
ct2(n[0], m, end, indent=indent + 3)
class BlockingRemoteOperator(BaseOperator):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.remote_base_url = kwargs.get("remote_base_url", "http://mock-api:5000/")
def execute(self, context):
print(f"RemoteOperator.execute({self})")
self.submit_to_remote(context)
result = self.wait_for_remote(context)
return result
def get_job_id(self, context):
dag_id = self.dag_id
task_id = self.task_id
run_id = context.get("run_id")
job_id = f"{dag_id}:{task_id}:{run_id}"
return job_id
def submit_to_remote(self, context):
print(f"RemoteOperator.submit_remote({self})")
response = requests.get(
self.remote_base_url + "submit/" + self.get_job_id(context)
)
result = response.content.decode("utf-8")
print(f"result = {result}")
return result
def wait_for_remote(self, context):
result = None
while result in ("Running", None):
print(f"RemoteOperator.wait_for_remote({self}) - {result}")
if result != "Complete":
time.sleep(1)
result = self.poll_remote(context)
print(result)
return result
def poll_remote(self, context):
print(f"RemoteOperator.poll_remote({self})")
response = requests.get(
self.remote_base_url + "status/" + self.get_job_id(context)
)
result = response.content.decode("utf-8")
return result
class DeferredRemoteOperator(BlockingRemoteOperator):
"""
Synchronous job submission, but asynchronous job completion polling via Trigger.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.remote_base_url = kwargs.get("remote_base_url", "http://mock-api:5000/")
def execute(self, context):
logging.warning(f"DeferredRemoteOperator.execute({self})")
submit_result = self.submit_to_remote(context)
trigger = DeferrableRemoteTrigger(
base_url=self.remote_base_url,
task_id=self.task_id,
dag_id=self.dag_id,
job_id=self.get_job_id(context),
run_id=context.get("run_id"),
submmit_result=submit_result,
)
self.defer(
trigger=trigger,
method_name="execute_complete",
kwargs={},
timeout=datetime.timedelta(
minutes=2
), # this appears to the the pause before the deferred execution resumes
)
# execution stops here and frees up the worker
# the trigger will asynchronously poll for completion then call back to execute_complete
def execute_complete(self, context, event=None):
print(f"DeferredRemoteOperator.execute_complete({self}) -> {event}")
return
class DeferrableRemoteTrigger(BaseTrigger):
def __init__(
self,
*args,
base_url=None,
job_id=None,
dag_id=None,
run_id=None,
submit_result=None,
**kwargs,
):
super().__init__(*args, **kwargs)
self.base_url = base_url
self.job_id = job_id
self.dag_id = dag_id
self.run_id = run_id
self.submit_result = submit_result
print("DeferrableRemoteTrigger.__init__")
def serialize(self) -> Tuple[str, Dict[str, Any]]:
print("DeferrableRemoteTrigger.serialize")
return (
fullname(self),
{
"base_url": self.base_url,
"job_id": self.job_id,
"dag_id": self.dag_id,
"run_id": self.run_id,
"submit_result": self.submit_result,
},
)
async def run(self) -> AsyncIterator["TriggerEvent"]:
while True:
print("DeferrableRemoteTrigger.run loop")
async with httpx.AsyncClient() as client: # TODO: aiohttp is likely even faster here
print(f"DeferrableRemoteTrigger.run({self}) - polling for status.")
response = await client.get(self.base_url + "status/" + self.job_id)
print(response.text)
result = response.text
print(f"DeferrableRemoteTrigger.run({self}) - {result}")
if result == "Complete":
print(f"DeferrableRemoteTrigger.run({self}) - complete")
yield TriggerEvent(result)
break
else:
print(f"DeferrableRemoteTrigger.run({self}) - looping after 1 second.")
await asyncio.sleep(1)
with DAG(
slugify.slugify(f'{cfg["name"]} - {dag_type}'),
start_date=datetime.datetime.fromtimestamp(Path(__file__).stat().st_mtime),
description=cfg["description"],
schedule=None,
default_args={
"depends_on_past": False,
"email": [
cfg["on_fail_notify"],
],
"email_on_failure": True,
"email_on_retry": False,
"retries": 1,
"retry_delay": timedelta(minutes=1),
},
) as dag:
start = BashOperator(
task_id="start",
bash_command="date",
)
preconditions = BashOperator(
task_id="preconditions",
bash_command="date",
)
end = BashOperator(
task_id="end-cleanup",
bash_command="date",
)
steps = []
for step_num in range(cfg["steps"]):
if dag_type == "full":
step = DeferredRemoteOperator(
task_id=f"DStep-{step_num+1}",
)
else:
step = BlockingRemoteOperator(
task_id=f"BStep-{step_num+1}",
)
steps.append(step)
if dag_type == "full":
# parallel
t = tree(steps) # consumes steps
# t = t[0]
print(t)
# start >> preconditions >> t >> end
start >> preconditions
ct2(preconditions, t, end)
else:
# serial
start >> preconditions
last_step = preconditions
for step in steps:
step.set_upstream(last_step)
last_step = step
step >> end