Browse Source

chatgpt 修改后

yEs1do 2 năm trước cách đây
mục cha
commit
d467155134
2 tập tin đã thay đổi với 31 bổ sung và 17 xóa
  1. 14 15
      module/config/config.py
  2. 17 2
      module/config/utils.py

+ 14 - 15
module/config/config.py

@@ -338,16 +338,21 @@ class Config(ConfigState, ConfigManual, ConfigWatcher, ConfigMenu):
                 "Missing argument in delay_next_run, should set at least one"
             )
 
-        # 计算浮动时间
-        float_seconds = (
-            scheduler.float_time.hour * 3600 +
-            scheduler.float_time.minute * 60 +
-            scheduler.float_time.second
-        )
-        random_float = random.randint(-float_seconds, float_seconds)
-        run = min(run) + timedelta(seconds=random_float)
-        run = run.replace(microsecond=0)
+        run = min(run).replace(microsecond=0)
         next_run = run
+
+        if server and hasattr(scheduler, 'server_update'):
+            # 加入随机浮动时间
+            float_seconds = (scheduler.float_time.hour * 3600 +
+                             scheduler.float_time.minute * 60 +
+                             scheduler.float_time.second)
+            random_float = random.randint(-float_seconds, float_seconds)
+            # 如果有强制运行时间
+            if scheduler.server_update == time(hour=9):
+                next_run += timedelta(seconds=random_float)
+            else:
+                next_run = parse_tomorrow_server(scheduler.server_update, random_float)
+
         # 将这些连接起来,方便日志输出
         kv = dict_to_kv(
             {
@@ -359,12 +364,6 @@ class Config(ConfigState, ConfigManual, ConfigWatcher, ConfigMenu):
         )
         logger.info(f"Delay task `{task}` to {next_run} ({kv})")
 
-        # 强制设定下一次的运行时间
-        if server and hasattr(scheduler, 'server_update') and scheduler.server_update != time(hour=9):
-            if target is None:
-                next_run = parse_tomorrow_server(scheduler.server_update)
-
-
         # 保证线程安全的
         self.lock_config.acquire()
         try:

+ 17 - 2
module/config/utils.py

@@ -244,17 +244,32 @@ def dict_to_kv(dictionary, allow_none=True):
     return ', '.join([f'{k}={repr(v)}' for k, v in dictionary.items() if allow_none or v is not None])
 
 
-def parse_tomorrow_server(server_update: time) -> datetime:
+def parse_tomorrow_server(server_update: time, float_seconds: int = 0) -> datetime:
     """
     获取明天的日期,给这个日期加上server_update的时间,返回datetime
     :param server_update:
+    :param float_seconds: 浮动的秒数,可为正或负值
     :return:
     """
     if isinstance(server_update, str):
         server_update = time.fromisoformat(server_update)
     now = datetime.now()
     tomorrow = now + timedelta(days=1)
-    return datetime.combine(tomorrow, server_update)
+    next_run = datetime.combine(tomorrow, server_update)
+    
+    # 应用浮动时间
+    next_run += timedelta(seconds=float_seconds)
+    
+    # 确保时间在第二天内,不回退到前一天,不跨越到第三天(考虑任务时间,最早 00:00,最晚 23:50)
+    start_of_tomorrow = datetime.combine(tomorrow, time.min)
+    end_of_tomorrow = datetime.combine(tomorrow, time(hour=23, minute=50))
+    
+    if next_run < start_of_tomorrow:
+        next_run = start_of_tomorrow
+    elif next_run > end_of_tomorrow:
+        next_run = end_of_tomorrow
+    
+    return next_run
 
 def deep_get(d, keys, default=None):
     """