使用异步任务注解

This commit is contained in:
liuwei
2025-05-20 10:21:39 +08:00
parent 628082c93a
commit b44b527346
3 changed files with 137 additions and 49 deletions

View File

@@ -8,10 +8,10 @@ import toml
import wechat_ipad
from loguru import logger
from async_job import async_job
from base.func_epic import is_friday, get_free
from base.func_news import News
from configuration import Config
from job_mgmt import Job
from plugin_common.event_system import EventType, EventSystem
from plugin_common.message_plugin_interface import MessagePluginInterface
from plugin_common.plugin_interface import PluginStatus
@@ -30,7 +30,7 @@ from wechat_ipad.models.message import WxMessage, MessageType
from xiuren.meitu_dl import meitu_dowload_pub_pic
class Robot(Job):
class Robot:
"""个性化自己的机器人
"""
@@ -448,7 +448,7 @@ class Robot(Job):
保持机器人运行,不让进程退出
"""
while True:
self.runPendingJobs()
# self.runPendingJobs()
time.sleep(1)
# 添加一个方法用于刷新联系人信息
@@ -632,70 +632,71 @@ class Robot(Job):
# ============================================== 业务内容==========================================================
def news_baidu_report_auto(self) -> None:
@async_job.at_times(["08:30"])
async def news_baidu_report_auto(self) -> None:
try:
news = News().get_baidu_news()
self.send_group_txt_message(news, Feature.DAILY_NEWS)
await self.send_group_txt_message(news, Feature.DAILY_NEWS)
except Exception as e:
self.LOG.error(f"newsBaiduReportAuto error{e}")
def news_en_report(self, website, sender: str = None) -> None:
async def news_en_report(self, website, sender: str = None) -> None:
try:
news = News().get_eng_news(website)
self.ipad_bot.send_text_message(sender, news)
await self.ipad_bot.send_text_message(sender, news)
except Exception as e:
self.LOG.error(f"newsEnReport error{e}")
# 使用装饰器标记定时任务 星期五 10:30 执行
def send_epic_free_games(self):
@async_job.every_weekday_time(weekday=4, time_str="10:00") # 0=周一4=周五
async def send_epic_free_games(self):
try:
if is_friday():
games = get_free()
asyncio.run(self.send_group_txt_message(games, Feature.EPIC))
await self.send_group_txt_message(games, Feature.EPIC)
except Exception as e:
self.LOG.error(f"sendEpicFreeGames error{e}")
# 使用装饰器标记定时任务
def message_count_to_db(self):
@async_job.at_times(["02:30"])
async def message_count_to_db(self):
try:
self.message_storage.write_to_db()
except Exception as e:
self.LOG.error(f"write_to_db error{e}")
def generate_sehuatang_pdf(self):
@async_job.at_times(["15:30"])
async def generate_sehuatang_pdf(self):
try:
self.LOG.info("开始生成PDF,generate_sehuatang_pdf")
path = pdf_file_path()
# 暂时只发4K群
asyncio.run(self.send_group_file_message(path, Feature.PDF_CAPABILITY))
await self.send_group_file_message(path, Feature.PDF_CAPABILITY)
except Exception as e:
self.LOG.error(f"generateSehuatangPdf error{e}")
def xiu_ren_download_task(self):
@async_job.at_times(["01:30"])
async def xiu_ren_download_task(self):
try:
# 每天下载10组图然后发一个帖子PDF
meitu_dowload_pub_pic()
except Exception as e:
self.LOG.error(f"xiu_ren_download_task error{e}")
def generate_and_send_ranking(self):
@async_job.at_times(["09:30"])
async def generate_and_send_ranking(self):
try:
receivers = self.gbm.get_group_list()
if not receivers:
return
async def send_all():
tasks = []
for r in receivers:
if self.gbm.get_group_permission(r, Feature.DAILY_SUMMARY) == PermissionStatus.ENABLED:
output = self.message_storage.generate_and_send_ranking(r, self.allContacts)
tasks.append(self.ipad_bot.send_text_message(r, output))
await asyncio.gather(*tasks)
asyncio.run(send_all())
for r in receivers:
if self.gbm.get_group_permission(r, Feature.DAILY_SUMMARY) == PermissionStatus.ENABLED:
output = self.message_storage.generate_and_send_ranking(r, self.allContacts)
await self.ipad_bot.send_text_message(r, output)
except Exception as e:
self.LOG.error(f"SendRanking error{e}")
@async_job.at_times(["10:23"])
async def job_test(self):
await self.ipad_bot.send_text_message("Jyunere","测试任务!")