/
repeat_at.py
73 lines (60 loc) 路 2.23 KB
/
repeat_at.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
import asyncio
import logging
from datetime import datetime
from functools import wraps
from asyncio import ensure_future
from starlette.concurrency import run_in_threadpool
from croniter import croniter
def get_delta(cron):
"""
This function returns the time delta between now and the next cron execution time.
"""
now = datetime.now()
cron = croniter(cron, now)
return (cron.get_next(datetime) - now).total_seconds()
def repeat_at(
*,
cron: str,
logger: logging.Logger = None,
raise_exceptions: bool = False,
max_repetitions: int = None,
):
"""
This function returns a decorator that makes a function execute periodically as per the cron expression provided.
:: Params ::
------------
cron: str
Cron-style string for periodic execution, eg. '0 0 * * *' every midnight
logger: logging.Logger (default None)
Logger object to log exceptions
raise_exceptions: bool (default False)
Whether to raise exceptions or log them
max_repetitions: int (default None)
Maximum number of times to repeat the function. If None, repeat indefinitely.
"""
def decorator(func):
is_coroutine = asyncio.iscoroutinefunction(func)
@wraps(func)
def wrapper(*args, **kwargs):
repititions = 0
if not croniter.is_valid(cron):
raise ValueError("Invalid cron expression")
async def loop(*args, **kwargs):
nonlocal repititions
while max_repetitions is None or repititions < max_repetitions:
try:
sleepTime = get_delta(cron)
await asyncio.sleep(sleepTime)
if is_coroutine:
await func(*args, **kwargs)
else:
await run_in_threadpool(func, *args, **kwargs)
except Exception as e:
if logger is not None:
logger.exception(e)
if raise_exceptions:
raise e
repititions += 1
ensure_future(loop(*args, **kwargs))
return wrapper
return decorator