mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-06-15 08:37:42 +00:00
✨ add apscheduler
This commit is contained in:
parent
bd7aaa94c6
commit
4a890298db
@ -108,6 +108,7 @@ def get_bots() -> Union[NoReturn, Dict[str, Bot]]:
|
|||||||
return driver.bots
|
return driver.bots
|
||||||
|
|
||||||
|
|
||||||
|
from nonebot.sched import scheduler
|
||||||
from nonebot.config import Env, Config
|
from nonebot.config import Env, Config
|
||||||
from nonebot.log import logger, default_filter
|
from nonebot.log import logger, default_filter
|
||||||
from nonebot.adapters.cqhttp import Bot as CQBot
|
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...")
|
logger.debug("Loading nonebot test frontend...")
|
||||||
nonebot_test.init()
|
nonebot_test.init()
|
||||||
|
|
||||||
|
if scheduler:
|
||||||
|
_driver.on_startup(_start_scheduler)
|
||||||
|
|
||||||
|
|
||||||
def run(host: Optional[str] = None,
|
def run(host: Optional[str] = None,
|
||||||
port: Optional[int] = None,
|
port: Optional[int] = None,
|
||||||
@ -201,6 +205,13 @@ def run(host: Optional[str] = None,
|
|||||||
get_driver().run(host, port, *args, **kwargs)
|
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_message, on_notice, on_request, on_metaevent
|
||||||
from nonebot.plugin import on_startswith, on_endswith, on_command, on_regex
|
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
|
from nonebot.plugin import load_plugin, load_plugins, load_builtin_plugins, get_loaded_plugins
|
||||||
|
@ -237,6 +237,16 @@ class Config(BaseConfig):
|
|||||||
SESSION_EXPIRE_TIMEOUT=[DD ][HH:MM]SS[.ffffff]
|
SESSION_EXPIRE_TIMEOUT=[DD ][HH:MM]SS[.ffffff]
|
||||||
SESSION_EXPIRE_TIMEOUT=P[DD]DT[HH]H[MM]M[SS]S # ISO 8601
|
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
|
||||||
# custom configs can be assigned during nonebot.init
|
# custom configs can be assigned during nonebot.init
|
||||||
|
12
nonebot/sched.py
Normal file
12
nonebot/sched.py
Normal 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
|
Loading…
x
Reference in New Issue
Block a user