-
Notifications
You must be signed in to change notification settings - Fork 1
/
tasks.py
218 lines (187 loc) · 6.5 KB
/
tasks.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
import celery
import tagging
from init import mongo_reconnect
from makeGraph import makeGraph
from model import Testcase
import time
from log import log
import asyncio
import io
import logging
from diff import diffGraphs
from datetime import datetime
from celery import Celery
from celery.signals import task_prerun
from config import celeryconfig
# Start the Celery application
celeryapp = Celery('autograph',
result_backend=celeryconfig.result_backend,
broker_url=celeryconfig.broker_url,
broker_host="redis",
broker_port=6379,
redis_host='redis',
redis_port=6379,
SECRET_KEY='not_a_secret',
broker_transport='redis',
cache_backend='memory',
)
celeryapp.config_from_object(celeryconfig)
celeryapp.set_default()
# celeryapp.conf.update(celeryconfig)
celeryapp.autodiscover_tasks()
@task_prerun.connect
def on_task_init(*args, **kwargs):
mongo_reconnect()
@celery.shared_task(max_retries=3,
throws=(ValueError,),
autoretry_for=(ValueError,),
retry_backoff=1,
retry_jitter=True,
retry_backoff_max=60)
def run_testcase(basedomain, inclusionmethod, difference, filetype, browser):
start = time.time()
log(f"Started with {inclusionmethod}-{difference}-{filetype}-{browser}")
try:
# delete the test case so we can re-run it
Testcase.objects(
inclusionmethod=inclusionmethod,
difference=difference,
filetype=filetype,
browser=browser).delete()
except:
pass
result = Testcase(
inclusionmethod=inclusionmethod,
difference=difference,
filetype=filetype,
browser=browser,
)
result.url = f"https://{basedomain}/test/{inclusionmethod}/{difference}/{filetype}/{browser}"
result.time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
result.logs = "Running...\n"
result.save()
# logging is a bit of magic
logger = logging.getLogger()
logger.setLevel(logging.INFO)
log_stream = io.StringIO()
logger.addHandler(logging.StreamHandler(log_stream))
try:
# create first graph
g1 = asyncio.run(makeGraph(
result.url,
browser,
verbose=True,
headless=False,
logger=logger
))
result.logs = log_stream.getvalue()
result.save()
# switch state
result.switch_state()
# create second graph
g2 = asyncio.run(makeGraph(
result.url,
browser,
verbose=True,
headless=False,
logger=logger
))
result.logs = log_stream.getvalue()
result.save()
# switch state back
result.switch_state()
# diff graphs and save results
diff = diffGraphs(g1, g2)
result.diff_results.clear()
result.diff_results.update(diff)
result.logs = log_stream.getvalue()
result.length = len(diff['paths'])
# update result in db
result.duration = f'{time.time() - start:.2f}s'
result.time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
except Exception as e:
log(f"Error {e}")
result.logs += f'An exception occurred: {repr(e)}'
result.save()
raise e
result.save()
def delete_queue():
celeryapp.control.purge()
def status_info():
i = celeryapp.control.inspect()
actives = i.active() or {}
reserved = i.reserved() or {}
return {
'workers': len(actives), # get a list of active tasks
# get a list of tasks registered
'queue': ["/".join(task['args']) for worker in actives.values() for task in worker],
# get a list of tasks registered
'reserved': ["/".join(task['args']) for worker in reserved.values() for task in worker],
}
@celery.shared_task(max_retries=3,
throws=(ValueError,),
autoretry_for=(ValueError,),
retry_backoff=1,
retry_jitter=True,
retry_backoff_max=60,
bind=True)
def debug_task(self):
print(f'Request: {self.request!r}')
return "the debug task return value"
@celery.shared_task(max_retries=1,
throws=(ValueError,),
autoretry_for=(ValueError,),
retry_backoff=1,
retry_jitter=True,
retry_backoff_max=60,
bind=True,
time_limit=3600*3)
def run_tagging(self):
with open('config/tagrules.yml', 'r') as f:
tagrules = tagging.load_tagrules(f)
# only get testcases that have a result
# no_cache otherwise we run into memory issues :/
testcases = Testcase.objects(length__ne=0).no_cache().only(
'diff_results', 'diff_tags').order_by('-time')
n = 0
m = testcases.count()
for testcase in testcases:
n += 1
if (n % 100 == 0):
print(f'[Tagging] Tagged {n}/{m}')
diffpaths = [x['path'] for x in testcase.diff_results.get(
"structural_difference", {}).get("roots_of_change", [])]
try:
if diffpaths:
tags = list(tagging.tag(diffpaths, tagrules))
testcase.diff_tags = tags
testcase.save()
except Exception as e:
print('[Tagging] Error:', e)
print('[Tagging] Done tagging!')
@celery.shared_task(max_retries=1,
throws=(ValueError,),
autoretry_for=(ValueError,),
retry_backoff=1,
retry_jitter=True,
retry_backoff_max=60,
bind=True,
time_limit=600)
def run_tagging_single(self, inclusionmethod, difference, filetype, browser):
with open('config/tagrules.yml', 'r') as f:
tagrules = tagging.load_tagrules(f)
testcase = Testcase.objects(
inclusionmethod=inclusionmethod,
difference=difference,
filetype=filetype,
browser=browser
).first()
diffpaths = [x['path'] for x in testcase.diff_results.get(
"structural_difference", {}).get("roots_of_change", [])]
try:
if diffpaths:
tags = list(tagging.tag(diffpaths, tagrules))
testcase.diff_tags = tags
testcase.save()
except Exception as e:
print('[Tagging] Error:', e)