ZhuLinsen/daily_stock_analysis · error · ValueError

无效的定时执行时间: {self.schedule_time!r}

Error message

无效的定时执行时间: {self.schedule_time!r}

What it means

ValueError from StockScheduler.set_daily_task when _configure_daily_tasks fails for the configured schedule times. Each time string must match strict HH:MM 24-hour format ((?:[01]\d|2[0-3]):[0-5]\d); if none of the configured times validate, configuration fails and this error names the offending schedule_time value.

Source

Thrown at src/scheduler.py:138

        self._schedule_times_provider = schedule_times_provider
        self.shutdown_handler = GracefulShutdown(register_signals=register_signals)
        self._task_callback: Optional[Callable] = None
        self._daily_job: Optional[Any] = None
        self._daily_jobs: List[Any] = []
        self._background_tasks: List[Dict[str, Any]] = []
        self._running = False

    def set_daily_task(self, task: Callable, run_immediately: bool = True):
        """
        设置每日定时任务

        Args:
            task: 要执行的任务函数(无参数)
            run_immediately: 是否在设置后立即执行一次
        """
        self._task_callback = task
        if not self._configure_daily_tasks(self.schedule_times):
            raise ValueError(f"无效的定时执行时间: {self.schedule_time!r}")

        if run_immediately:
            logger.info("立即执行一次任务...")
            self._safe_run_task()

    @staticmethod
    def _is_valid_schedule_time(schedule_time: str) -> bool:
        """Validate time string in HH:MM 24-hour format."""
        candidate = (schedule_time or "").strip()
        if not re.fullmatch(r"(?:[01]\d|2[0-3]):[0-5]\d", candidate):
            return False
        return True

    def _cancel_daily_job(self) -> None:
        """Remove the currently registered daily job if one exists."""
        if self._daily_job is None and not self._daily_jobs:
            return

View on GitHub (pinned to 5159bd72e8)

Solutions

  1. Set the time to zero-padded 24-hour HH:MM, e.g. SCHEDULE_TIME=09:30
  2. Fix every entry if schedule_times is a list — one bad entry can fail configuration
  3. Trim whitespace and remove AM/PM suffixes or colon-seconds
  4. Validate at startup with a regex like ^(?:[01]\d|2[0-3]):[0-5]\d$ before constructing the scheduler

Example fix

# before
SCHEDULE_TIME=9:30am

# after
SCHEDULE_TIME=09:30
Defensive patterns

Strategy: validation

Validate before calling

import re

def is_valid_schedule_time(value: str) -> bool:
    return bool(re.fullmatch(r"(?:[01]\d|2[0-3]):[0-5]\d", (value or "").strip()))

Try / catch

try:
    scheduler.set_daily_task(task)
except ValueError as exc:
    log.error("Bad schedule time %s — use zero-padded HH:MM 24h", exc)
    raise

Prevention

When it happens

Trigger: Constructing the scheduler with schedule_time like '9:00' (missing leading zero), '09:60', '24:00', '9am', or an empty string when schedule_times is None. The fallback list ends up containing no valid HH:MM value, _configure_daily_tasks returns falsy, and set_daily_task raises.

Common situations: CRON-style or 12-hour values pasted into SCHEDULE_TIME; missing zero-padding; whitespace-only env value; passing schedule_times as a list of malformed strings; daylight-saving or locale-specific time formats.

Related errors


AI-assisted analysis of ZhuLinsen/daily_stock_analysis@5159bd72e8 (2026-08-15). Data as JSON: /api/errors/e55f15b730ebe1fc. Report an issue: GitHub.