pub struct JobStore { /* private fields */ }Expand description
Persistent storage layer for scheduled jobs.
All job definitions and run history are stored in a SQLite database managed by
zeph-db migrations. The scheduled_jobs table schema is defined in migration
051_scheduler_jobs.sql.
§Examples
use zeph_scheduler::JobStore;
// Open from a file path.
let store = JobStore::open("sqlite:scheduler.db").await?;
store.init().await?;
// Query job list.
let jobs = store.list_jobs().await?;
for job in &jobs {
println!("{}: {} ({}) → {}", job.name, job.kind, job.task_mode, job.next_run);
}Implementations§
Source§impl JobStore
impl JobStore
Sourcepub fn new(pool: DbPool) -> Self
pub fn new(pool: DbPool) -> Self
Create a JobStore from an existing zeph_db::DbPool.
You must call JobStore::init before any other operation to ensure the
schema migrations have been applied.
Sourcepub async fn open(path: &str) -> Result<Self, SchedulerError>
pub async fn open(path: &str) -> Result<Self, SchedulerError>
Open (or create) a JobStore from a SQLite file path.
§Errors
Returns SchedulerError::Db if the connection cannot be established.
Sourcepub async fn init(&self) -> Result<(), SchedulerError>
pub async fn init(&self) -> Result<(), SchedulerError>
Run all pending migrations on the underlying pool.
Replaces the former inline CREATE TABLE IF NOT EXISTS DDL. The
scheduled_jobs schema is now managed by migration
051_scheduler_jobs.sql in zeph-db.
§Errors
Returns an error if any migration fails.
Sourcepub async fn upsert_job(
&self,
name: &str,
cron_expr: &str,
kind: &str,
) -> Result<(), SchedulerError>
pub async fn upsert_job( &self, name: &str, cron_expr: &str, kind: &str, ) -> Result<(), SchedulerError>
Sourcepub async fn upsert_job_with_mode(
&self,
name: &str,
cron_expr: &str,
kind: &str,
task_mode: &str,
run_at: Option<&str>,
task_data: &str,
) -> Result<(), SchedulerError>
pub async fn upsert_job_with_mode( &self, name: &str, cron_expr: &str, kind: &str, task_mode: &str, run_at: Option<&str>, task_data: &str, ) -> Result<(), SchedulerError>
Upsert a job definition with explicit task_mode, optional run_at, and task_data.
§Errors
Returns an error if the SQL statement fails.
Sourcepub async fn upsert_job_with_provenance(
&self,
name: &str,
cron_expr: &str,
kind: &str,
task_mode: &str,
run_at: Option<&str>,
task_data: &str,
provenance: &str,
) -> Result<(), SchedulerError>
pub async fn upsert_job_with_provenance( &self, name: &str, cron_expr: &str, kind: &str, task_mode: &str, run_at: Option<&str>, task_data: &str, provenance: &str, ) -> Result<(), SchedulerError>
Upsert a job definition with explicit task_mode, task_data, and RTW-A provenance.
§Errors
Returns an error if the SQL statement fails.
Sourcepub async fn insert_job(
&self,
name: &str,
cron_expr: &str,
kind: &str,
task_mode: &str,
run_at: Option<&str>,
task_data: &str,
) -> Result<(), SchedulerError>
pub async fn insert_job( &self, name: &str, cron_expr: &str, kind: &str, task_mode: &str, run_at: Option<&str>, task_data: &str, ) -> Result<(), SchedulerError>
Insert a new job. Returns SchedulerError::DuplicateJob if a job with this name exists.
This is the CLI write path (zeph schedule add, both the cron and --run-at forms), so
the row is stored with crate::task::TaskProvenance::UserAdded provenance — reflecting
its genuine CLI/user origin — rather than relying on the provenance column’s DB default.
Scheduler::init() hydration still force-downgrades out-of-process rows to
crate::task::TaskProvenance::External regardless of the stored value (#6114), so this
does not change runtime trust decisions; it keeps the stored label accurate for direct
inspection (e.g. a raw DB dump) — zeph schedule list/show do not currently surface
provenance.
§Errors
Returns SchedulerError::DuplicateJob on unique constraint violation,
or SchedulerError::Database on other SQL errors.
Sourcepub async fn record_run(
&self,
name: &str,
timestamp: &str,
next_run: &str,
) -> Result<(), SchedulerError>
pub async fn record_run( &self, name: &str, timestamp: &str, next_run: &str, ) -> Result<(), SchedulerError>
Record a job execution and persist the next scheduled run time.
§Errors
Returns an error if the SQL statement fails.
Sourcepub async fn mark_error(&self, name: &str) -> Result<(), SchedulerError>
pub async fn mark_error(&self, name: &str) -> Result<(), SchedulerError>
Mark a job as permanently errored (e.g. invalid cron expression on hydration).
The job remains visible in JobStore::list_jobs_full with status = "error" so
operators can identify it via zeph scheduler list without reading debug logs.
§Errors
Returns an error if the SQL statement fails.
Sourcepub async fn delete_job(&self, name: &str) -> Result<bool, SchedulerError>
pub async fn delete_job(&self, name: &str) -> Result<bool, SchedulerError>
Sourcepub async fn job_exists(&self, name: &str) -> Result<bool, SchedulerError>
pub async fn job_exists(&self, name: &str) -> Result<bool, SchedulerError>
Sourcepub async fn set_next_run(
&self,
name: &str,
next_run: &str,
) -> Result<(), SchedulerError>
pub async fn set_next_run( &self, name: &str, next_run: &str, ) -> Result<(), SchedulerError>
Persist the next scheduled run time for a job (used during init).
§Errors
Returns an error if the SQL statement fails.
Sourcepub async fn get_next_run(
&self,
name: &str,
) -> Result<Option<String>, SchedulerError>
pub async fn get_next_run( &self, name: &str, ) -> Result<Option<String>, SchedulerError>
Sourcepub async fn list_jobs(
&self,
) -> Result<Vec<ScheduledTaskRecord>, SchedulerError>
pub async fn list_jobs( &self, ) -> Result<Vec<ScheduledTaskRecord>, SchedulerError>
List all active (non-done) jobs.
Returns a ScheduledTaskRecord per active job, ordered by name.
One-shot jobs without a computed next_run fall back to their run_at value;
if neither is set the field is an empty string.
§Errors
Returns an error if the SQL query fails.
Sourcepub async fn list_jobs_full(
&self,
) -> Result<Vec<ScheduledTaskInfo>, SchedulerError>
pub async fn list_jobs_full( &self, ) -> Result<Vec<ScheduledTaskInfo>, SchedulerError>
List all active (non-done) jobs with full details for display.
The cron_expr field of each returned ScheduledTaskInfo is Some for periodic tasks
with a valid expression and None for one-shot tasks or rows whose stored expression
failed validation. Invalid rows are not filtered out — callers can check status to
distinguish error rows.
§Errors
Returns an error if the SQL query fails.
Sourcepub async fn count_active_jobs(&self) -> Result<usize, SchedulerError>
pub async fn count_active_jobs(&self) -> Result<usize, SchedulerError>
Sourcepub async fn list_recent_runs(
&self,
n: usize,
) -> Result<Vec<RecentRunRecord>, SchedulerError>
pub async fn list_recent_runs( &self, n: usize, ) -> Result<Vec<RecentRunRecord>, SchedulerError>
List the n most recently run (non-done) jobs, ordered by last_run descending.
Never-run jobs (last_run IS NULL) sort last. Ordering and truncation are pushed into
SQL via ORDER BY last_run IS NULL, last_run DESC LIMIT ? rather than fetched-then-sorted
in Rust: last_run IS NULL evaluates to a sortable 0/1 (SQLite) or false/true
(PostgreSQL) value, giving “NULLS LAST” semantics on both backends without relying on
PostgreSQL-only NULLS LAST syntax, which SQLite does not support.
§Errors
Returns an error if the SQL query fails.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for JobStore
impl !UnwindSafe for JobStore
impl Freeze for JobStore
impl Send for JobStore
impl Sync for JobStore
impl Unpin for JobStore
impl UnsafeUnpin for JobStore
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more