nonebot / nonebot2

跨平台 Python 异步聊天机器人框架 / Asynchronous multi-platform chatbot framework written in Python
https://nonebot.dev
MIT License
5.95k stars 557 forks source link

Bug: 按文档创建定时任务有问题 #595

Closed Shimada666 closed 2 years ago

Shimada666 commented 2 years ago

描述问题:

按文档创建定时任务(路径:bot/src/plugins/notice/__init__.py)

import nonebot
from apscheduler.schedulers.background import BackgroundScheduler
from nonebot import require

scheduler: BackgroundScheduler = require("nonebot_plugin_apscheduler").scheduler

@scheduler.scheduled_job("cron", hour="13,17")
async def notice():
    bot = nonebot.get_bot()
    await bot.send_group_msg(group_id=123456, message=f'打卡!')

在主文件中加载(路径:bot/bot.py)

import nonebot
from nonebot.adapters.cqhttp import Bot as CQHTTPBot

nonebot.init()

driver = nonebot.get_driver()
driver.register_adapter("cqhttp", CQHTTPBot)
nonebot.load_plugins("src/plugins")

if __name__ == "__main__":
    nonebot.run()

报以下错误

11-19 13:00:00 [ERROR] apscheduler | Job "notice (trigger: cron[hour='13,17'], next run at: 2021-11-19 17:00:00 CST)" raised an exception
Traceback (most recent call last):
  File "/usr/src/app/bot.py", line 11, in <module>
    nonebot.run()
  File "/usr/local/lib/python3.9/site-packages/nonebot/__init__.py", line 278, in run
    get_driver().run(host, port, *args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/nonebot/drivers/fastapi.py", line 256, in run
    uvicorn.run(
  File "/usr/local/lib/python3.9/site-packages/uvicorn/main.py", line 447, in run
    server.run()
  File "/usr/local/lib/python3.9/site-packages/uvicorn/server.py", line 68, in run
    return asyncio.run(self.serve(sockets=sockets))
  File "/usr/local/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
> File "/usr/local/lib/python3.9/site-packages/apscheduler/executors/base_py3.py", line 30, in run_coroutine_job
    retval = await job.func(*job.args, **job.kwargs)
  File "/usr/src/app/src/plugins/notice/__init__.py", line 10, in notice
    bot = nonebot.get_bot()
  File "/usr/local/lib/python3.9/site-packages/nonebot/__init__.py", line 171, in get_bot
    raise ValueError("There are no bots to get.")
ValueError: There are no bots to get.

如何复现?

创建上述两个文件即可复现

bot/src/plugins/notice/init.py bot/bot.py

期望的结果

定时任务正常运行

环境信息:

协议端信息:

截图或日志

If applicable, add screenshots to help explain your problem.

yanyongyu commented 2 years ago

你定时任务运行时bot没连上,自然报错。运行时,请注意对bot是否存在的判断

yanyongyu commented 2 years ago

还有scheduler不是BackgroundScheduler是async的那个scheduler(

Shimada666 commented 2 years ago

你定时任务运行时bot没连上,自然报错。运行时,请注意对bot是否存在的判断

还有请问一下,我已经按要求添加了 .env.prod 的 apscheduler 配置,但是还是有 pytz warning,如下

11-19 13:51:33 [SUCCESS] nonebot | NoneBot is initializing...
11-19 13:51:33 [INFO] nonebot | Current Env: prod
11-19 13:51:33 [SUCCESS] nonebot | Succeeded to import "rp"
11-19 13:51:33 [SUCCESS] nonebot | Succeeded to import "monitor"
11-19 13:51:33 [SUCCESS] nonebot | Succeeded to import "nonebot_plugin_apscheduler"
/Users/zhengmingzhang/PycharmProjects/ErGoThin/bot/venv/lib/python3.8/site-packages/apscheduler/util.py:95: PytzUsageWarning: The zone attribute is specific to pytz's interface; please migrate to a new time zone provider. For more details on how to do so, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html
  if obj.zone == 'local':
11-19 13:51:33 [SUCCESS] nonebot | Succeeded to import "card"
11-19 13:51:33 [SUCCESS] nonebot | Succeeded to import "notice"
11-19 13:51:33 [SUCCESS] nonebot | Running NoneBot...
11-19 13:51:33 [INFO] uvicorn | Started server process [96207]
11-19 13:51:33 [INFO] uvicorn | Waiting for application startup.
/Users/zhengmingzhang/PycharmProjects/ErGoThin/bot/venv/lib/python3.8/site-packages/apscheduler/triggers/cron/__init__.py:159: PytzUsageWarning: The localize method is no longer necessary, as this time zone supports the fold attribute (PEP 495). For more details on migrating to a PEP 495-compliant implementation, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html
  return self.timezone.localize(datetime(**values))
11-19 13:51:34 [INFO] nonebot_plugin_apscheduler | Scheduler Started
11-19 13:51:34 [INFO] uvicorn | Application startup complete.
11-19 13:51:34 [INFO] uvicorn | Uvicorn running on http://0.0.0.0:9080 (Press CTRL+C to quit)
/Users/zhengmingzhang/PycharmProjects/ErGoThin/bot/venv/lib/python3.8/site-packages/apscheduler/triggers/cron/__init__.py:146: PytzUsageWarning: The normalize method is no longer necessary, as this time zone supports the fold attribute (PEP 495). For more details on migrating to a PEP 495-compliant implementation, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html
  return self.timezone.normalize(dateval + difference), fieldnum

我尝试在 bot.py 直接 init 初始化时直接进行配置,仍报错,请问有解决办法吗

Shimada666 commented 2 years ago

我的 .env.prod

HOST=0.0.0.0
PORT=9080

APSCHEDULER_AUTOSTART=true
APSCHEDULER_CONFIG={"apscheduler.timezone": "Asia/Shanghai"}
yanyongyu commented 2 years ago

这个忽略就好,上游未更新,不影响使用