Sfoglia il codice sorgente

Merge pull request #1135 from BluettDream/dev_fix_scheduler

dev_fix_scheduler
huangrunheng 1 anno fa
parent
commit
42ab1bef79
2 ha cambiato i file con 51 aggiunte e 40 eliminazioni
  1. 6 3
      module/config/config.py
  2. 45 37
      script.py

+ 6 - 3
module/config/config.py

@@ -103,6 +103,7 @@ class Config(ConfigState, ConfigManual, ConfigWatcher, ConfigMenu):
         super(ConfigWatcher, self).__init__()
         super(ConfigMenu, self).__init__()
         self.model = ConfigModel(config_name=config_name)
+        self.scheduler_update_dt = None  # 调度器更新时间
 
     def __getattr__(self, name):
         """
@@ -181,14 +182,14 @@ class Config(ConfigState, ConfigManual, ConfigWatcher, ConfigMenu):
         pending_task = []
         waiting_task = []
         error = []
-        now = datetime.now()
+        self.scheduler_update_dt = datetime.now()
         for key, value in self.model.dict().items():
             func = Function(key, value)
             if not func.enable:
                 continue
             if not isinstance(func.next_run, datetime):
                 error.append(func)
-            elif func.next_run < now:
+            elif func.next_run < self.scheduler_update_dt:
                 pending_task.append(func)
             else:
                 waiting_task.append(func)
@@ -238,8 +239,10 @@ class Config(ConfigState, ConfigManual, ConfigWatcher, ConfigMenu):
         获取调度器的数据, 但是你必须使用update_scheduler来更新信息
         :return:
         """
+        # 根据调度器更新时间来判断是否有可运行的任务,保证逻辑一致性
+        scheduler_update_dt = getattr(self, 'scheduler_update_dt', datetime.now())
         running = {}
-        if self.task is not None and self.task.next_run < datetime.now():
+        if self.task is not None and self.task.next_run < scheduler_update_dt:
             running = {"name": self.task.command, "next_run": str(self.task.next_run)}
 
         pending = []

+ 45 - 37
script.py

@@ -269,9 +269,6 @@ class Script:
             result[key] = item
         return json.dumps(result)
 
-
-
-
     def wait_until(self, future):
         """
         Wait until a specific time.
@@ -303,43 +300,56 @@ class Script:
         获取下一个任务的名字, 大驼峰。
         :return:
         """
-        while 1:
+        while True:
             task = self.config.get_next()
             self.config.task = task
             if self.state_queue:
                 self.state_queue.put({"schedule": self.config.get_schedule_data()})
+            now = datetime.now()
+            # 任务时间到了返回任务名称
+            if task.next_run <= now:
+                return task.command
+            # 根据策略执行等待逻辑
+            if not self._handle_wait_during_idle(task.next_run):
+                # 若等待被打断, 则刷新配置
+                del_cached_property(self, "config")
+
+    def _handle_wait_during_idle(self, next_run: datetime) -> bool:
+        """
+        处理任务空闲期间的行为策略
+        :param next_run: 下一个任务的时间
+        :return: True 表示等待成功完成, False 表示等待被中断
+        """
+        method = self.config.script.optimization.when_task_queue_empty
+        strategy_map = {
+            "close_game": self._wait_close_game,
+            "goto_main": self._wait_goto_main,
+        }
+        func = strategy_map.get(method)
+        if not func:
+            logger.warning(f"Invalid Optimization_WhenTaskQueueEmpty: {method}, fallback to stay_there")
+            func = self._wait_stay_there
+        return func(next_run)
+
+    def _wait_close_game(self, next_run: datetime) -> bool:
+        logger.info("Close game during wait")
+        self.device.app_stop()
+        self.device.release_during_wait()
+        if not self.wait_until(next_run):
+            return False
+        self.run("Restart")
+        return True
+
+    def _wait_goto_main(self, next_run: datetime) -> bool:
+        logger.info("Goto main page during wait")
+        self.run("GotoMain")
+        self.device.release_during_wait()
+        return self.wait_until(next_run)
 
-            # from module.base.resource import release_resources
-            # if self.config.task.command != 'Alas':
-            #     release_resources(next_task=task.command)
-
-            if task.next_run > datetime.now():
-                logger.info(f'Wait until {task.next_run} for task `{task.command}`')
-                method = self.config.script.optimization.when_task_queue_empty
-                if method == 'close_game':
-                    logger.info('Close game during wait')
-                    self.device.app_stop()
-                    self.device.release_during_wait()
-                    if not self.wait_until(task.next_run):
-                        del_cached_property(self, 'config')
-                        continue
-                    self.run('Restart')
-                elif method == 'goto_main':
-                    logger.info('Goto main page during wait')
-                    self.run('GotoMain')
-                    self.device.release_during_wait()
-                    if not self.wait_until(task.next_run):
-                        del_cached_property(self, 'config')
-                        continue
-                else:
-                    logger.warning(f'Invalid Optimization_WhenTaskQueueEmpty: {method}, fallback to stay_there')
-                    self.device.release_during_wait()
-                    if not self.wait_until(task.next_run):
-                        del_cached_property(self, 'config')
-                        continue
-            break
-
-        return task.command
+    def _wait_stay_there(self, next_run: datetime) -> bool:
+        logger.info("Stay_there (no action) during wait")
+        self.device.release_during_wait()
+        return self.wait_until(next_run)
 
     def run(self, command: str) -> bool:
         """
@@ -460,8 +470,6 @@ class Script:
                 continue
 
             # Run
-            if self.state_queue:
-                self.state_queue.put({"schedule": self.config.get_schedule_data()})
             logger.info(f'Scheduler: Start task `{task}`')
             self.device.stuck_record_clear()
             self.device.click_record_clear()