Hmbown/CodeWhale · error · anyhow::Error

Unable to compute next CRON run within 5 years

Error message

Unable to compute next CRON run within 5 years

What it means

Cron schedules are resolved by scanning minute-by-minute up to MAX_CRON_SEARCH_MINUTES (five years of minutes). If no minute in that window satisfies the parsed CRON expression and resolves to a real local time, the search bails with this message. It guards the matcher against expressions that can never match (e.g. February 30) from spinning forever.

Solutions

  1. Fix the expression so it can match real dates (e.g. '0 0 30 2 *' → '0 0 28 2 *' or use '*' for day-of-month).
  2. Verify each field's range: minute 0-59, hour 0-23, day-of-month 1-31, month 1-12, day-of-week 0-6.
  3. If the date is genuinely rare (e.g. Feb 29), confirm the intended semantics; consider a WEEKLY/MONTHLY RRULE instead.

Example fix

// before
EXPR="0 0 30 2 *"
// after
EXPR="0 0 28 2 *"
Defensive patterns

Strategy: validation

Validate before calling

fn cron_expr_can_match(expr: &str) -> bool {
    // reject impossible day/month pairs like 30 Feb or 31 Apr
    let f: Vec<&str> = expr.split_whitespace().collect();
    f.len() == 5 && !(f[2] == "30" && f[3] == "2") && !(f[2] == "31" && ["4","6","9","11"].contains(&f[3]))
}

Try / catch

match schedule.next_after_with_anchor(now, anchor) {
    Ok(next) => arm(next),
    Err(e) if e.to_string().contains("CRON run within 5 years") => disable_schedule("expression never matches"),
    Err(e) => report(e),
}

Prevention

When it happens

Trigger: A CRON expression whose field combination can never be true — e.g. '0 0 30 2 *' (Feb 30) or '0 0 31 4 *' (Apr 31) — or an expression matching only instants that fall in DST gaps, so no minute within five years matches and resolves.

Common situations: Users porting cron entries with impossible day-of-month/month combinations; typos like day 32; expressions using February 29 style dates without leap-year alignment (29 Feb matches only in leap years — still fine unless combined so it never matches in the window).

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/aa13d89babe9b52b. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/automation_manager.rs:602

                    .and_then(|dt| dt.with_nanosecond(0))
                    .ok_or_else(|| anyhow::anyhow!("Unable to round CRON search start"))?
                    .checked_add_signed(Duration::minutes(1))
                    .ok_or_else(|| anyhow::anyhow!("CRON schedule exceeded its range"))?;

                for _ in 0..MAX_CRON_SEARCH_MINUTES {
                    if cron.matches(candidate_naive)
                        && let Some(candidate) = resolve_local_datetime(timezone, candidate_naive)
                    {
                        let candidate = candidate.with_timezone(&Utc);
                        if candidate > after {
                            return Ok(candidate);
                        }
                    }
                    candidate_naive = candidate_naive
                        .checked_add_signed(Duration::minutes(1))
                        .ok_or_else(|| anyhow::anyhow!("CRON schedule exceeded its range"))?;
                }
                bail!("Unable to compute next CRON run within 5 years");
            }
        }
    }

    fn next_after_slot(
        &self,
        slot: DateTime<Utc>,
        anchor_reference: DateTime<Utc>,
    ) -> Result<Option<DateTime<Utc>>> {
        match self {
            Self::Once { .. } => Ok(None),
            _ => self
                .next_after_with_anchor(slot, anchor_reference)
                .map(Some),
        }
    }

    /// First slot after `slot` that is still in the future at `now`.

View on GitHub (pinned to 73e0f67d83)