add apscheduler

This commit is contained in:
yanyongyu 2020-09-12 13:34:36 +08:00
parent bd7aaa94c6
commit 4a890298db
3 changed files with 33 additions and 0 deletions

View File

@ -108,6 +108,7 @@ def get_bots() -> Union[NoReturn, Dict[str, Bot]]:
return driver.bots
from nonebot.sched import scheduler
from nonebot.config import Env, Config
from nonebot.log import logger, default_filter
from nonebot.adapters.cqhttp import Bot as CQBot
@ -169,6 +170,9 @@ def init(*, _env_file: Optional[str] = None, **kwargs):
logger.debug("Loading nonebot test frontend...")
nonebot_test.init()
if scheduler:
_driver.on_startup(_start_scheduler)
def run(host: Optional[str] = None,
port: Optional[int] = None,
@ -201,6 +205,13 @@ def run(host: Optional[str] = None,
get_driver().run(host, port, *args, **kwargs)
async def _start_scheduler():
if scheduler and not scheduler.running:
scheduler.configure(_driver.config.apscheduler_config)
scheduler.start()
logger.opt(colors=True).info("<y>Scheduler Started</y>")
from nonebot.plugin import on_message, on_notice, on_request, on_metaevent
from nonebot.plugin import on_startswith, on_endswith, on_command, on_regex
from nonebot.plugin import load_plugin, load_plugins, load_builtin_plugins, get_loaded_plugins

View File

@ -237,6 +237,16 @@ class Config(BaseConfig):
SESSION_EXPIRE_TIMEOUT=[DD ][HH:MM]SS[.ffffff]
SESSION_EXPIRE_TIMEOUT=P[DD]DT[HH]H[MM]M[SS]S # ISO 8601
"""
apscheduler_config: dict = {"apscheduler.timezone": "Asia/Shanghai"}
"""
- 类型: ``dict``
- 默认值: ``{"apscheduler.timezone": "Asia/Shanghai"}``
- 说明:
APScheduler 的配置对象 `Configuring the Scheduler`_
.. _Configuring the Scheduler:
https://apscheduler.readthedocs.io/en/latest/userguide.html#configuring-the-scheduler
"""
# custom configs
# custom configs can be assigned during nonebot.init

12
nonebot/sched.py Normal file
View File

@ -0,0 +1,12 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
try:
from apscheduler.schedulers.asyncio import AsyncIOScheduler
except ImportError:
AsyncIOScheduler = None
if AsyncIOScheduler:
scheduler = AsyncIOScheduler()
else:
scheduler = None