Skip to main content

Scheduler

Struct Scheduler 

Source
pub struct Scheduler { /* private fields */ }
Expand description

Host-side scheduler skeleton.

One per Uni instance. M11 cutover wires tokio::spawn driving and persistence into uni_system.background_jobs. Currently the scheduler is paused — registered jobs are stored but not executed.

Implementations§

Source§

impl Scheduler

Source

pub fn new() -> Self

Construct a paused scheduler.

Source

pub fn add_job(&self, id: QName)

Register a new job with the legacy Manual schedule.

Equivalent to add_scheduled_job(id, Schedule::Manual). The job becomes eligible immediately and fires on the next tick (no-op while paused).

Source

pub fn add_scheduled_job(&self, id: QName, schedule: Schedule)

Register a new job with an explicit schedule.

The job’s next_fire_at is computed from the schedule plus the current SystemTime. The scheduler picks it up on the first Self::tick / Self::tick_at whose now is at or past next_fire_at (no-op while paused).

Source

pub fn cancel(&self, id: &QName) -> bool

Cancel a scheduled job by id.

Returns true if the job was found and cancelled.

Source

pub fn list(&self) -> Vec<SchedulerJobRecord>

List all known jobs and their statuses (snapshot).

Source

pub fn cancel_token_for(&self, id: &QName) -> Option<CancellationToken>

Look up the cancellation token associated with a registered job.

Returns None if no job matches id. The returned clone shares state with the record’s token, so callers can both await cancelled().await and observe the same cancel signal trip via Self::cancel.

Used by the host driver to wrap each dispatched spawn_blocking in a tokio::select! against cancelled().await, so shutdown / explicit cancel propagates without waiting for the job body to poll CancellationToken::is_cancelled.

Source

pub fn resume(&self)

Resume the scheduler (M11 cutover wires actual driving here).

Source

pub fn tick(&self) -> Vec<QName>

Drive the scheduler with the current wall-clock time.

Equivalent to tick_at(SystemTime::now()). See Self::tick_at for the full semantics.

Source

pub fn tick_at(&self, now: SystemTime) -> Vec<QName>

Pop every pending job whose schedule has fired at or before now, transition each to Running, and return their ids for the caller to dispatch.

M11 substantive driver primitive. This is the synchronous, runtime-free heart of the scheduler — the eventual Tokio driver wraps a poll loop that calls tick_at(SystemTime::now()), dispatches the returned jobs (e.g., via tokio::spawn invoking each job’s BackgroundJobProvider::execute), and calls Scheduler::mark_finished when each completes.

Schedule semantics (delegated to crate::traits::background::Schedule::next_after):

  • A job is “due” iff status == Pending, next_fire_at.is_none() or next_fire_at <= now, and the cancel token is not already triggered.
  • Manual jobs have next_fire_at = now at registration and so are immediately due (matching legacy tick() behavior).
  • Once(at) jobs become due only when now >= at.
  • Periodic(every) jobs become due every after each fire.
  • Cron(expr) jobs become due at the next cron instant computed via the cron crate.

Honors pause: returns empty when Self::is_paused. Honors cancellation: skips jobs whose cancel token is already triggered (filtering them out of the return).

Source

pub fn running_count(&self) -> usize

Number of jobs currently in Running state. Useful for observability (e.g., a metrics gauge).

Source

pub fn pending_count(&self) -> usize

Number of pending jobs ready for the next tick.

Source

pub fn requeue_orphaned_runs(&self) -> usize

Reset every Running job back to Pending — used by the driver to recover from a crash where jobs were started but not finished. The host restores the scheduler state from uni_system.background_jobs and calls this to make all previously-Running jobs eligible for re-dispatch.

Source

pub fn pause(&self)

Pause the scheduler.

Source

pub fn is_paused(&self) -> bool

Returns true if currently paused.

Source

pub fn mark_started(&self, id: &QName)

Mark a job as starting a new run.

Used by tests + the M11 cutover driver. Updates the record’s status to Running and stamps last_started_at.

Source

pub fn mark_finished(&self, id: &QName, success: bool)

Mark a job’s run as finished (success or failure).

Recomputes next_fire_at from the job’s Schedule using SystemTime::now() as the reference point. If the schedule has another fire upcoming (Periodic, Cron, or a Once whose instant is still in the future — which shouldn’t normally happen after it has just fired), the job transitions back to Pending so the next Self::tick_at can pick it up. Otherwise the job stays in its terminal state (Idle on success, FailedRetrying on failure).

Trait Implementations§

Source§

impl Debug for Scheduler

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Scheduler

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl SchedulerControl for Scheduler

Source§

fn add_scheduled_job(&self, id: QName, schedule: Schedule)

Register a job to fire on schedule.
Source§

fn cancel(&self, id: &QName) -> bool

Cancel a job by id. Returns true if it existed.
Source§

fn list(&self) -> Vec<SchedulerJobRecord>

Snapshot of every known job.
Source§

fn submit_cypher(&self, _cypher: &str) -> Result<(), FnError>

Submit an inline write-mode Cypher body for synchronous execution. The default impl returns an error so simple scheduler primitives (without a host) can still satisfy the trait shape; the uni-db::scheduler::SchedulerHost override dispatches through its crate::traits::background::JobHost. Read more
Source§

fn flush_checkpoint(&self) -> Result<(), FnError>

Drive the persistence backend to flush its checkpoint buffer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> PluginState for T
where T: Send + Sync + 'static,

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more