mirror of
https://github.com/YunoHost/yunorunner.git
synced 2024-09-03 20:05:52 +02:00
[enh] add decorator to always relaunch a function
This commit is contained in:
parent
30353c1375
commit
cfda7c79b0
1 changed files with 21 additions and 0 deletions
21
schedule.py
Normal file
21
schedule.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
import asyncio
|
||||
from functools import wraps
|
||||
|
||||
|
||||
def always_relaunch(sleep):
|
||||
def decorator(function):
|
||||
@wraps(function)
|
||||
async def wrap(*args, **kwargs):
|
||||
while True:
|
||||
try:
|
||||
await function(*args, **kwargs)
|
||||
except KeyboardInterrupt:
|
||||
return
|
||||
except Exception as e:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
print(f"Error: exception in function '{function.__name__}', relaunch in {sleep} seconds")
|
||||
finally:
|
||||
await asyncio.sleep(sleep)
|
||||
return wrap
|
||||
return decorator
|
Loading…
Add table
Reference in a new issue