import type { CronJobStatus, CronJobLogEntry } from "@rebasepro/types"; import type { RebaseClient } from "@rebasepro/types"; import type { LoadedCronJob } from "./cron-loader"; import type { CronStore } from "./cron-store"; /** * Validates a standard 5-field cron expression structurally and semantically. * Returns `{ valid: true }` or `{ valid: false, reason: string }`. */ export declare function validateCronExpression(schedule: string): { valid: true; } | { valid: false; reason: string; }; export declare class CronScheduler { private jobs; private started; private store?; private client?; /** * Set the RebaseClient instance to make it available to cron job handlers. */ setClient(client: RebaseClient): void; /** * Attach a persistence store for cron logs. * When set, execution logs are written to the database after each run, * and counters are seeded from the database on start. */ setStore(store: CronStore): void; /** * Register a batch of loaded cron jobs. * * If the scheduler is already started, newly registered jobs are * automatically scheduled (so late-registered jobs don't sit idle). * * Validates the cron schedule on registration — invalid schedules * are rejected with a warning and the job is NOT registered. */ registerJobs(loadedJobs: LoadedCronJob[]): void; /** * Start the scheduler — begins ticking all enabled jobs. */ start(): void; /** * Stop the scheduler and clear all timers. * * Currently-executing handlers run to completion (they are async), * but no further scheduling occurs after stop. */ stop(): void; /** * List all registered jobs with their current status. */ listJobs(): CronJobStatus[]; /** * Get a single job status by ID. */ getJob(id: string): CronJobStatus | undefined; /** * Get log entries for a job. */ getJobLogs(id: string, limit?: number): CronJobLogEntry[]; /** * Get log entries for a job from the database (if store is available). * Falls back to in-memory logs if no store is configured. */ getJobLogsFromDb(id: string, limit?: number): Promise; /** * Enable or disable a job at runtime. */ setJobEnabled(id: string, enabled: boolean): CronJobStatus | undefined; /** * Manually trigger a job execution immediately. * * Returns `undefined` if the job doesn't exist. * If the job is currently executing, returns the log entry with * a `skipped: true` result rather than running concurrently. */ triggerJob(id: string): Promise; /** * Schedule the next execution for a job. * * Safety guarantees: * 1. Clears any existing timer first (prevents leaked/duplicate timers) * 2. Enforces a minimum delay to prevent tight loops from jitter * 3. Unref's the timer so it doesn't prevent process exit * 4. Re-checks enabled & started state before executing * 5. Concurrency guard prevents overlapping handler executions */ private scheduleNext; /** * Stop a single job's timer and clear its next run state. */ private stopJob; /** * Execute a job's handler with full isolation and safety. * * - Sets a concurrency flag to prevent overlapping runs * - Wraps handler in a timeout race * - Captures all logs, errors, and results * - Persists to store (non-blocking) if available * - Always restores state even on catastrophic errors */ private executeJob; private toStatus; }