Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

通过 env 调节调度周期 #684

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/usage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ next: /usage/cookie
所有支持的主题请参见[主题](#主题)一节
:::

- `BISON_SITE_SCHEDULE`: 设置特定 site 的调度间隔,它是一个 {site_name => {type: Literal["interval", "date", "cron"], settings: dict}} 为结构的配置项。内层的 dict 为 apscheduler 调用 add_jobs 时传入的参数。
举例将 bilibili 的周期调节到 90s 一次可以这样设置:
`BISON_SITE_SCHEDULE__bilibili.com={"type":"interval","settings":{"seconds":90}}`
或者可以在一个环境变量中完成所有 site 的设置:
`BISON_SITE_SCHEDULE={"bilibili.com":{"type":"interval","settings":{"seconds":90}},"weibo.com":{"type":"interval","settings":{"seconds":5}}}`

## 使用

::: warning
Expand Down
12 changes: 11 additions & 1 deletion nonebot_bison/plugin_config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Literal

import nonebot
from nonebot import get_plugin_config
from nonebot.compat import PYDANTIC_V2, ConfigDict
Expand All @@ -9,6 +11,11 @@
ThemeName = str


class ScheduleConfig(BaseModel):
type: Literal["date", "interval", "cron"]
settings: dict


class PlugConfig(BaseModel):
if PYDANTIC_V2:
model_config = ConfigDict(populate_by_name=True)
Expand All @@ -23,7 +30,9 @@ class Config:
description="发送消息时将所有文本转换为图片,防止风控,仅需要推送文转图可以为 platform 指定 theme",
)
bison_use_browser: bool = Field(
default=False, description="是否使用环境中的浏览器", alias="bison_theme_use_browser"
default=False,
description="是否使用环境中的浏览器",
alias="bison_theme_use_browser",
)
bison_init_filter: bool = True
bison_use_queue: bool = True
Expand All @@ -42,6 +51,7 @@ class Config:
)
bison_show_network_warning: bool = True
bison_platform_theme: dict[PlatformName, ThemeName] = {}
bison_site_schedule: dict[str, ScheduleConfig] = {}

@property
def outer_url(self) -> URL:
Expand Down
15 changes: 14 additions & 1 deletion nonebot_bison/scheduler/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from nonebot_bison.config import config
from nonebot_bison.platform import platform_manager
from nonebot_bison.plugin_config import plugin_config
from nonebot_bison.send import send_msgs
from nonebot_bison.types import SubUnit, Target
from nonebot_bison.utils import ClientManager, ProcessContext, Site
Expand Down Expand Up @@ -47,12 +48,19 @@ def __init__(
if use_batch:
self.batch_platform_name_targets_cache[platform_name].append(target)
self.schedulable_list.append(
Schedulable(platform_name=platform_name, target=target, current_weight=0, use_batch=use_batch)
Schedulable(
platform_name=platform_name,
target=target,
current_weight=0,
use_batch=use_batch,
)
)
self._refresh_batch_api_target_cache()

self.platform_name_list = platform_name_list
self.pre_weight_val = 0 # 轮调度中“本轮”增加权重和的初值

self._load_scehdule_rule_from_config()
logger.info(
f"register scheduler for {self.name} with "
f"{self.scheduler_config.schedule_type} {self.scheduler_config.schedule_setting}"
Expand All @@ -63,6 +71,11 @@ def __init__(
**self.scheduler_config.schedule_setting,
)

def _load_scehdule_rule_from_config(self):
Copy link
Preview

Copilot AI Mar 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method name '_load_scehdule_rule_from_config' contains a spelling error. Consider renaming it to '_load_schedule_rule_from_config' for clarity.

Suggested change
def _load_scehdule_rule_from_config(self):
def _load_schedule_rule_from_config(self):

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

if schedule_rule := plugin_config.bison_site_schedule.get(self.name):
self.scheduler_config.schedule_type = schedule_rule.type
self.scheduler_config.schedule_setting = schedule_rule.settings

def _refresh_batch_api_target_cache(self):
self.batch_api_target_cache = defaultdict(dict)
for platform_name, targets in self.batch_platform_name_targets_cache.items():
Expand Down
Loading