-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
171 lines (126 loc) · 4.98 KB
/
run.py
File metadata and controls
171 lines (126 loc) · 4.98 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
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
#!/usr/bin/env python
import os
import time
import requests
from operator import itemgetter
import sys
DEVLAKE_ENDPOINT = os.getenv('DEVLAKE_ENDPOINT')
def error_handler(func):
def wrapper(*args, **kwargs):
try:
result = func(*args, **kwargs)
return result
except Exception as e:
print(f'Function error "{func.__name__}": {str(e)}')
sys.exit(1)
return wrapper
class DevlakeQueue:
def __init__(self):
self.precheck()
@error_handler
def precheck(self):
assert os.environ.get('DEVLAKE_ENDPOINT') is not None, 'environment "DEVLAKE_ENDPOINT" is not set'
@error_handler
def request_handler(self, **kwargs):
path = kwargs.get('path', None)
method = kwargs.get('method', None)
page = kwargs.get('page', 1)
page_size = kwargs.get('page_size', None)
assert path is not None, '"path" must not be None!'
assert method is not None, '"method" must not be None!'
assert page_size is not None, '"page_size" must not be None!'
params = {
'pageSize': page_size,
'page': page
}
url = f'{DEVLAKE_ENDPOINT}{path}'
req = requests.Request(method, url, params=params)
prep = req.prepare()
res = requests.Session().send(prep)
if res.status_code >= 200 and res.status_code < 300:
data = res.json()
if isinstance(data, dict) or isinstance(data, list):
return res.json()
else:
raise ValueError("Invalid data type")
else:
raise ValueError(f'({res.status_code}) {res.reason}')
@error_handler
def get_blueprints(self):
# set pageSize by the number of blueprints
page_size = self.request_handler(
path='/blueprints?pageSize=1',
method='GET',
page_size=1)['count']
# get all blueprints
blueprints = self.request_handler(
path='/blueprints',
method='GET',
page_size=page_size)['blueprints']
# get blueprints ids
blueprint_ids = [x['id'] for x in blueprints]
return blueprint_ids
@error_handler
def get_pipelines(self):
# set pageSize by the number of pipelines
page_size = self.request_handler(
path='/pipelines?pageSize=1',
method='GET',
page_size=1)['count']
# get all pipelines
pipelines = self.request_handler(
path='/pipelines',
method='GET',
page_size=page_size)['pipelines']
# order blueprint by pipeline execution time
last_pipeline_by_blueprints = {}
for pipeline in pipelines:
blueprint_id = pipeline['blueprintId']
finished_at = pipeline['finishedAt']
if finished_at is not None:
if blueprint_id not in last_pipeline_by_blueprints:
last_pipeline_by_blueprints[blueprint_id] = finished_at
else:
if finished_at > last_pipeline_by_blueprints[blueprint_id]:
last_pipeline_by_blueprints[blueprint_id] = finished_at
# remove from blueprints that not exists
blueprints_to_remove = list(set(last_pipeline_by_blueprints.keys()) ^ set(self.get_blueprints()))
for b in blueprints_to_remove:
last_pipeline_by_blueprints.pop(b)
# sort by datime
sorted_blueprints = dict(sorted(last_pipeline_by_blueprints.items(), key=itemgetter(1)))
return list(sorted_blueprints.keys())
@error_handler
def there_some_pipeline_running(self):
pipeline_running = self.request_handler(
path='/pipelines?status=TASK_RUNNING',
method='GET',
page_size=10)['pipelines']
if len(pipeline_running) > 0:
self.blueprint_in_execution = [ x['blueprintId'] for x in pipeline_running]
return True
else:
return False
@error_handler
def trigget_blueprint(self, blueprint_id):
self.request_handler(
path=f'/blueprints/{blueprint_id}/trigger',
method='POST',
page_size=1)
time.sleep(5) # wait five seconds to prevent the loop to trigger another blueprint
@error_handler
def run_queue(self):
queue = self.get_pipelines()
if len(queue) == 0:
print('Queue is empty!')
else:
for blueprint_id in queue:
while self.there_some_pipeline_running():
print(f'Waiting for pipeline running! (blueprintId: {self.blueprint_in_execution})')
time.sleep(5)
self.trigget_blueprint(blueprint_id)
if __name__ == '__main__':
dq = DevlakeQueue()
while True:
dq.run_queue()
time.sleep(60)