-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
323 lines (283 loc) · 12.1 KB
/
models.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
# coding: utf-8
from __future__ import print_function, unicode_literals
import calendar
import logging
import sys
import traceback
from datetime import datetime
from datetime import time as datetimetime
from datetime import timedelta
from dateutil.relativedelta import relativedelta
from django.db import models
from django.utils import timezone
from huey import crontab
from huey.contrib.djhuey import db_periodic_task, lock_task
from six import text_type as unicode
from hueylogs.exceptions import HueyMaxTriesException
logger = logging.getLogger("hueylogs")
class HueyExecutionLog(models.Model):
code = models.CharField(max_length=255, db_index=True)
start_time = models.DateTimeField(db_index=True)
end_time = models.DateTimeField(db_index=True)
is_success = models.BooleanField(default=False)
error_description = models.TextField(blank=True)
finnished = models.BooleanField(default=None, null=True)
def __str__(self):
return self.code
@classmethod
def task_to_string(cls, task_class):
"""Return the string representation of a function."""
return "{}.{}".format(task_class.__module__, task_class.__name__)
@classmethod
def logs(
cls,
hours,
minutes_tolerance=15,
max_tries=3,
try_again_delay=5,
lock=True,
):
"""Concentrate all decorators in only one decorator.
Not that have no need to decorate the function with huey decorators.
"""
def _decorator(func):
run_at_times_decorator = HueyExecutionLog.run_at_times(
hours=hours, minutes_tolerance=minutes_tolerance
)
max_tries_decorator = HueyExecutionLog.max_tries(
max_tries=max_tries, try_again_delay=try_again_delay
)
db_periodic_task_decorator = db_periodic_task(lambda dt: True)
lock_task_decorator = lambda func: func
if lock:
lock_task_decorator = lock_task(
HueyExecutionLog.task_to_string(func)
)
return db_periodic_task_decorator(
lock_task_decorator(
run_at_times_decorator(
max_tries_decorator(
HueyExecutionLog.register_log(func)
)
)
)
)
return _decorator
@classmethod
def _reached_max_tries(cls, code, max_tries):
failed_tries = HueyExecutionLog.objects.filter(code=code).order_by(
"-start_time"
)[:max_tries]
if failed_tries.count() < max_tries:
return False
fails = [
not i for i in failed_tries.values_list("is_success", flat=True)
]
return all(fails)
@classmethod
def check_incompatible_hours(cls, hours, minutes_tolerance):
"""True if a hour is less than 'minutes_tolerance' of another."""
_times_copy = list(hours)
while len(_times_copy):
hour_to_check = _times_copy.pop()
for remaining_hour in _times_copy:
now = datetime.now()
dt_a = datetime.combine(now, remaining_hour)
dt_b = datetime.combine(now, hour_to_check)
delta_minutes = abs((dt_a - dt_b).total_seconds()) / 60.0
if delta_minutes <= minutes_tolerance:
raise ValueError(
"Is not possible have hours with less than {} minutes "
"of distance. Incompatible hours: {} and {}".format(
delta_minutes, dt_a, dt_b
)
)
@classmethod
def utc_to_local(cls, utc_dt):
"""Convert datetime with tzinfo to local hour without tzinfo.
Source: https://stackoverflow.com/a/13287083
"""
# get integer timestamp to avoid precision lost
timestamp = calendar.timegm(utc_dt.timetuple())
local_dt = datetime.fromtimestamp(timestamp)
assert utc_dt.resolution >= timedelta(microseconds=1)
return local_dt.replace(microsecond=utc_dt.microsecond)
@classmethod
def its_time(cls, hour, now, minutes_tolerance):
"""Return True if its time.
Will be the time if the hour of 'now' is equal or in range defined
by 'minutes_tolerance'. The 'minutes_tolerance' argument only works
from 'hour' to foward, not backwards.
Arguments:
- hour: datetime.time, with time to be sent
- now: datetime.datetime, now date
- minutes_tolerance: int, how many minutes will be tolerated AFTER
"""
# removing time zone data
if now.tzinfo:
now = HueyExecutionLog.utc_to_local(now)
hour = datetimetime(hour.hour, hour.minute)
assert isinstance(hour, datetimetime), "hour is not datetime.time"
assert isinstance(now, datetime), "now is not datetime.datetime"
# removing seconds info
now = datetime.combine(now.date(), datetimetime(now.hour, now.minute))
start = datetime(now.year, now.month, now.day, hour.hour, hour.minute)
end = datetime(
now.year, now.month, now.day, hour.hour, hour.minute
) + relativedelta(minutes=minutes_tolerance)
return (now >= start) and (now <= end)
@classmethod
def run_at_times(self, hours, minutes_tolerance=15):
"""Make sure that the task will be runned only in some hours.
Note that if 'hours' must have a minimum distance of 'minutes_tolerance'
minutes.
Arguments:
- hours: a list of datetime.time
- minutes_tolerance: if the task manager delay to run your function
you can force the execution of it setting minutes_tolerance
so if the task manager not run the task exactly in 00:00
but a little bit after, you can execute it still
"""
HueyExecutionLog.check_incompatible_hours(hours, minutes_tolerance)
def _decorator(func):
if not getattr(func, "register_log_called", None):
raise AttributeError(
"The function '{}' must have been decorated "
"with 'register_log'".format(
HueyExecutionLog.task_to_string(func)
)
)
def _inner_function(*args, **kwargs):
now = datetime.now()
hour = None
for i in hours:
if HueyExecutionLog.its_time(i, now, minutes_tolerance):
hour = i
break
if hour is None:
return
# verifying if the function was already called today
last_execution = (
HueyExecutionLog.objects.filter(
code=HueyExecutionLog.task_to_string(func),
start_time__day=now.day,
start_time__month=now.month,
start_time__year=now.year,
)
.order_by("-start_time")
.first()
)
if last_execution:
already_runned = HueyExecutionLog.its_time(
hour, last_execution.start_time, minutes_tolerance
)
if already_runned:
return
return func(*args, **kwargs)
_inner_function.__module__ = func.__module__
_inner_function.__name__ = func.__name__
_inner_function.register_log_called = True
return _inner_function
return _decorator
@classmethod
def max_tries(cls, max_tries, try_again_delay):
"""If the function was runned many time in error stop the execution.
Must be used with register_log decorator. The register_Log decorator
must decorate the function BEFORE the max_tries decorator. Example:
@periodic_task(lambda dt: True)
@HueyExecutionLog.max_tries(max_tries=3, try_again_delay=5)
@HueyExecutionLog.register_log
def my_task():
pass
Arguments:
- code: string with the unique name of function
- try_again_delay: how many minutes must be passed since the last
execution to try again
"""
def _decorator(func):
if not getattr(func, "register_log_called", None):
raise AttributeError(
"The function '{}' must have been decorated "
"with 'register_log'".format(
HueyExecutionLog.task_to_string(func)
)
)
def _inner_function(*args, **kwargs):
code = HueyExecutionLog.task_to_string(func)
if HueyExecutionLog._reached_max_tries(code, max_tries):
last_execution = (
HueyExecutionLog.objects.filter(code=code)
.only("start_time")
.order_by("-start_time")
.first()
.start_time
)
minutes = (timezone.now() - last_execution).seconds / 60.0
if minutes < try_again_delay:
print(
"Skipping execution to detriment of "
"'try_again_delay' of {}".format(try_again_delay)
)
return
try:
return func(*args, **kwargs)
except:
reached = HueyExecutionLog._reached_max_tries(
code, max_tries
)
if reached:
raise HueyMaxTriesException(
"The function '{}' have reached the maximum "
"of {} tries".format(code, max_tries)
)
raise
# changing the name of returned function because huey uses it
# as unique names to registry
_inner_function.__name__ = func.__name__
_inner_function.__module__ = func.__module__
_inner_function.register_log_called = True
return _inner_function
return _decorator
@classmethod
def register_log(cls, func):
"""Register the execution of a function."""
def _inner_function(*args, **kwargs):
start_time = timezone.now()
log_instance = HueyExecutionLog.objects.create(
code=HueyExecutionLog.task_to_string(func),
start_time=start_time,
end_time=start_time,
finnished=False,
is_success=False,
)
try:
result = func(*args, **kwargs)
log_instance.finnished = True
log_instance.end_time = timezone.now()
log_instance.is_success = True
log_instance.save()
return result
except Exception as e:
class DummyFile:
def __init__(self):
self.value = ""
def write(self, value):
if type(value) is not unicode:
value = value.decode("utf-8")
self.value += value + "\n"
t, v, trace = sys.exc_info()
dummy_file = DummyFile()
traceback.print_exception(t, v, trace, file=dummy_file)
log_instance.is_success = False
log_instance.finnished = True
log_instance.end_time = timezone.now()
log_instance.error_description = dummy_file.value
log_instance.save()
logger.error(e)
raise
# changing the name of returned function because huey uses it
# as unique names to registry
_inner_function.__name__ = func.__name__
_inner_function.__module__ = func.__module__
_inner_function.register_log_called = True
return _inner_function