mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2026-05-09 18:27:56 +08:00
🎨 按ruff的检查调整程序代码
This commit is contained in:
@@ -1 +1,3 @@
|
||||
from .manager import *
|
||||
from .manager import init_scheduler, scheduler_dict, handle_delete_target, handle_insert_new_target
|
||||
|
||||
__all__ = ["init_scheduler", "handle_delete_target", "handle_insert_new_target", "scheduler_dict"]
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
from typing import Type
|
||||
|
||||
from ..config import config
|
||||
from ..config.db_model import Target
|
||||
from ..platform import platform_manager
|
||||
from ..types import Target as T_Target
|
||||
from ..utils import SchedulerConfig
|
||||
from .scheduler import Scheduler
|
||||
from ..utils import SchedulerConfig
|
||||
from ..config.db_model import Target
|
||||
from ..types import Target as T_Target
|
||||
from ..platform import platform_manager
|
||||
|
||||
scheduler_dict: dict[Type[SchedulerConfig], Scheduler] = {}
|
||||
scheduler_dict: dict[type[SchedulerConfig], Scheduler] = {}
|
||||
|
||||
|
||||
async def init_scheduler():
|
||||
_schedule_class_dict: dict[Type[SchedulerConfig], list[Target]] = {}
|
||||
_schedule_class_platform_dict: dict[Type[SchedulerConfig], list[str]] = {}
|
||||
_schedule_class_dict: dict[type[SchedulerConfig], list[Target]] = {}
|
||||
_schedule_class_platform_dict: dict[type[SchedulerConfig], list[str]] = {}
|
||||
for platform in platform_manager.values():
|
||||
scheduler_config = platform.scheduler
|
||||
if not hasattr(scheduler_config, "name") or not scheduler_config.name:
|
||||
@@ -33,9 +31,7 @@ async def init_scheduler():
|
||||
for target in target_list:
|
||||
schedulable_args.append((target.platform_name, T_Target(target.target)))
|
||||
platform_name_list = _schedule_class_platform_dict[scheduler_config]
|
||||
scheduler_dict[scheduler_config] = Scheduler(
|
||||
scheduler_config, schedulable_args, platform_name_list
|
||||
)
|
||||
scheduler_dict[scheduler_config] = Scheduler(scheduler_config, schedulable_args, platform_name_list)
|
||||
config.register_add_target_hook(handle_insert_new_target)
|
||||
config.register_delete_target_hook(handle_delete_target)
|
||||
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional, Type
|
||||
|
||||
from nonebot.log import logger
|
||||
from nonebot_plugin_apscheduler import scheduler
|
||||
from nonebot_plugin_saa.utils.exceptions import NoBotFound
|
||||
|
||||
from ..config import config
|
||||
from ..platform import platform_manager
|
||||
from ..send import send_msgs
|
||||
from ..types import Target
|
||||
from ..config import config
|
||||
from ..send import send_msgs
|
||||
from ..platform import platform_manager
|
||||
from ..utils import ProcessContext, SchedulerConfig
|
||||
|
||||
|
||||
@@ -20,12 +19,11 @@ class Schedulable:
|
||||
|
||||
|
||||
class Scheduler:
|
||||
|
||||
schedulable_list: list[Schedulable]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
scheduler_config: Type[SchedulerConfig],
|
||||
scheduler_config: type[SchedulerConfig],
|
||||
schedulables: list[tuple[str, Target]],
|
||||
platform_name_list: list[str],
|
||||
):
|
||||
@@ -37,15 +35,12 @@ class Scheduler:
|
||||
self.scheduler_config_obj = self.scheduler_config()
|
||||
self.schedulable_list = []
|
||||
for platform_name, target in schedulables:
|
||||
self.schedulable_list.append(
|
||||
Schedulable(
|
||||
platform_name=platform_name, target=target, current_weight=0
|
||||
)
|
||||
)
|
||||
self.schedulable_list.append(Schedulable(platform_name=platform_name, target=target, current_weight=0))
|
||||
self.platform_name_list = platform_name_list
|
||||
self.pre_weight_val = 0 # 轮调度中“本轮”增加权重和的初值
|
||||
logger.info(
|
||||
f"register scheduler for {self.name} with {self.scheduler_config.schedule_type} {self.scheduler_config.schedule_setting}"
|
||||
f"register scheduler for {self.name} with "
|
||||
f"{self.scheduler_config.schedule_type} {self.scheduler_config.schedule_setting}"
|
||||
)
|
||||
scheduler.add_job(
|
||||
self.exec_fetch,
|
||||
@@ -53,7 +48,7 @@ class Scheduler:
|
||||
**self.scheduler_config.schedule_setting,
|
||||
)
|
||||
|
||||
async def get_next_schedulable(self) -> Optional[Schedulable]:
|
||||
async def get_next_schedulable(self) -> Schedulable | None:
|
||||
if not self.schedulable_list:
|
||||
return None
|
||||
cur_weight = await config.get_current_weight_val(self.platform_name_list)
|
||||
@@ -61,16 +56,9 @@ class Scheduler:
|
||||
self.pre_weight_val = 0
|
||||
cur_max_schedulable = None
|
||||
for schedulable in self.schedulable_list:
|
||||
schedulable.current_weight += cur_weight[
|
||||
f"{schedulable.platform_name}-{schedulable.target}"
|
||||
]
|
||||
weight_sum += cur_weight[
|
||||
f"{schedulable.platform_name}-{schedulable.target}"
|
||||
]
|
||||
if (
|
||||
not cur_max_schedulable
|
||||
or cur_max_schedulable.current_weight < schedulable.current_weight
|
||||
):
|
||||
schedulable.current_weight += cur_weight[f"{schedulable.platform_name}-{schedulable.target}"]
|
||||
weight_sum += cur_weight[f"{schedulable.platform_name}-{schedulable.target}"]
|
||||
if not cur_max_schedulable or cur_max_schedulable.current_weight < schedulable.current_weight:
|
||||
cur_max_schedulable = schedulable
|
||||
assert cur_max_schedulable
|
||||
cur_max_schedulable.current_weight -= weight_sum
|
||||
@@ -80,9 +68,7 @@ class Scheduler:
|
||||
context = ProcessContext()
|
||||
if not (schedulable := await self.get_next_schedulable()):
|
||||
return
|
||||
logger.trace(
|
||||
f"scheduler {self.name} fetching next target: [{schedulable.platform_name}]{schedulable.target}"
|
||||
)
|
||||
logger.trace(f"scheduler {self.name} fetching next target: [{schedulable.platform_name}]{schedulable.target}")
|
||||
send_userinfo_list = await config.get_platform_target_subscribers(
|
||||
schedulable.platform_name, schedulable.target
|
||||
)
|
||||
@@ -92,9 +78,7 @@ class Scheduler:
|
||||
|
||||
try:
|
||||
platform_obj = platform_manager[schedulable.platform_name](context, client)
|
||||
to_send = await platform_obj.do_fetch_new_post(
|
||||
schedulable.target, send_userinfo_list
|
||||
)
|
||||
to_send = await platform_obj.do_fetch_new_post(schedulable.target, send_userinfo_list)
|
||||
except Exception as err:
|
||||
records = context.gen_req_records()
|
||||
for record in records:
|
||||
@@ -107,7 +91,7 @@ class Scheduler:
|
||||
|
||||
for user, send_list in to_send:
|
||||
for send_post in send_list:
|
||||
logger.info("send to {}: {}".format(user, send_post))
|
||||
logger.info(f"send to {user}: {send_post}")
|
||||
try:
|
||||
await send_msgs(
|
||||
user,
|
||||
@@ -119,19 +103,14 @@ class Scheduler:
|
||||
def insert_new_schedulable(self, platform_name: str, target: Target):
|
||||
self.pre_weight_val += 1000
|
||||
self.schedulable_list.append(Schedulable(platform_name, target, 1000))
|
||||
logger.info(
|
||||
f"insert [{platform_name}]{target} to Schduler({self.scheduler_config.name})"
|
||||
)
|
||||
logger.info(f"insert [{platform_name}]{target} to Schduler({self.scheduler_config.name})")
|
||||
|
||||
def delete_schedulable(self, platform_name, target: Target):
|
||||
if not self.schedulable_list:
|
||||
return
|
||||
to_find_idx = None
|
||||
for idx, schedulable in enumerate(self.schedulable_list):
|
||||
if (
|
||||
schedulable.platform_name == platform_name
|
||||
and schedulable.target == target
|
||||
):
|
||||
if schedulable.platform_name == platform_name and schedulable.target == target:
|
||||
to_find_idx = idx
|
||||
break
|
||||
if to_find_idx is not None:
|
||||
|
||||
Reference in New Issue
Block a user