diff --git a/alas.py b/alas.py index d97d6761d..7802c6a2e 100644 --- a/alas.py +++ b/alas.py @@ -589,6 +589,23 @@ class AzurLaneAutoScript: from module.luahook.crack import * from module.scheduler_watcher import * from datetime import datetime, timedelta +from module.counter import MaxCounter +from typing import Dict + + +class FailedTaskCounter: + def __init__(self, max_retries): + self.max_retries = max_retries + self.tasks: Dict[str, MaxCounter] = {} + + def count_task(self, task) -> bool: + if task not in self.tasks: + self.tasks[task] = MaxCounter(self.max_retries) + return self.tasks[task].count_once(throw=False) + + def reset_task(self, task): + if task in self.tasks: + self.tasks[task].reset() class AzurLaneAutoScript(AzurLaneAutoScript): @@ -608,6 +625,8 @@ class AzurLaneAutoScript(AzurLaneAutoScript): self.is_fatal_error_restart = full_config.Restart_GameRestart_FatalErrorRestart self.max_retry_times_for_same_task_failed = full_config.Restart_GameRestart_MaxRetryTimesForSameTaskFailed + self.failed_task_counter = FailedTaskCounter(self.max_retry_times_for_same_task_failed) + def pre_init(self): from PIL import Image Image.init() @@ -932,24 +951,23 @@ class AzurLaneAutoScript(AzurLaneAutoScript): self.is_first_task = False # Check failures - failed = deep_get(self.failure_record, keys=task, default=0) - failed = 0 if success else failed + 1 - deep_set(self.failure_record, keys=task, value=failed) - if failed >= self.max_retry_times_for_same_task_failed: - logger.critical(f"Task `{task}` failed {self.max_retry_times_for_same_task_failed} or more times.") - logger.critical("Possible reason #1: You haven't used it correctly. " - "Please read the help text of the options.") - logger.critical("Possible reason #2: There is a problem with this task. " - "Please contact developers or try to fix it yourself.") - logger.critical('Request human takeover') - handle_notify( - self.config.Error_OnePushConfig, - title=f"Alas <{self.config_name}> crashed", - content=f"<{self.config_name}> RequestHumanTakeover\nTask `{task}` failed {self.max_retry_times_for_same_task_failed} or more times.", - ) - exit(1) + if not success: + if not self.failed_task_counter.count_task(task): + logger.critical(f"Task `{task}` failed {self.max_retry_times_for_same_task_failed} or more times.") + logger.critical("Possible reason #1: You haven't used it correctly. " + "Please read the help text of the options.") + logger.critical("Possible reason #2: There is a problem with this task. " + "Please contact developers or try to fix it yourself.") + logger.critical('Request human takeover') + handle_notify( + self.config.Error_OnePushConfig, + title=f"Alas <{self.config_name}> crashed", + content=f"<{self.config_name}> RequestHumanTakeover\nTask `{task}` failed {self.max_retry_times_for_same_task_failed} or more times.", + ) + exit(1) if success: + self.failed_task_counter.reset_task(task) del_cached_property(self, 'config') continue elif self.config.Error_HandleError: