-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
jobs.py
63 lines (52 loc) 路 1.98 KB
/
jobs.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
import asyncio
import logging
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from pytz import UnknownTimeZoneError, utc
import rasa.shared.utils.io
__scheduler = None
logger = logging.getLogger(__name__)
async def scheduler() -> AsyncIOScheduler:
"""Thread global scheduler to handle all recurring tasks.
If no scheduler exists yet, this will instantiate one.
"""
global __scheduler
if not __scheduler:
try:
__scheduler = AsyncIOScheduler(event_loop=asyncio.get_event_loop())
__scheduler.start()
return __scheduler
except UnknownTimeZoneError:
rasa.shared.utils.io.raise_warning(
"apscheduler could not find a timezone and is "
"defaulting to utc. This is probably because "
"your system timezone is not set. "
'Set it with e.g. echo "Europe/Berlin" > '
"/etc/timezone"
)
__scheduler = AsyncIOScheduler(
event_loop=asyncio.get_event_loop(), timezone=utc
)
__scheduler.start()
return __scheduler
else:
# scheduler already created, make sure it is running on
# the correct loop
# noinspection PyProtectedMember
if not __scheduler._eventloop == asyncio.get_event_loop():
raise RuntimeError(
"Detected inconsistent loop usage. "
"Trying to schedule a task on a new event "
"loop, but scheduler was created with a "
"different event loop. Make sure there "
"is only one event loop in use and that the "
"scheduler is running on that one."
)
return __scheduler
def kill_scheduler() -> None:
"""Terminate the scheduler if started.
Another call to `scheduler` will create a new scheduler.
"""
global __scheduler
if __scheduler:
__scheduler.shutdown()
__scheduler = None