Skip to main content

Operations

Struct Operations 

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

User-visible long work: provided and exported by QueueModule (it needs DatabaseModule).

use navree_queue::{Operation, Operations};

let op = operations
    .start(uow, Operation::new("deals.export").owner(user))
    .await?;
operations
    .dispatch(uow, &op, ExportDeals { operation: op.id })
    .await?;
// The client follows GET /api/operations/{op.id}/events.
  • start stores it pending; dispatch and dispatch_batch queue its work.
  • Its job’s ctx.progress(done, total, note) makes it running and records the progress; while the job runs the worker beats its heartbeat every queue.operations.heartbeat_secs.
  • It finishes when its job ends: succeeded (keeping any result set with complete), failed, or cancelled. With a batch it finishes once the batch and its callbacks have run.
  • cancel is cooperative: jobs not started are skipped, running ones get JobCancelled from their next ctx.progress.
  • Every change is broadcast as operation.updated on private-operations.{id} (when the app has a dyn Broadcaster).

Implementations§

Source§

impl Operations

Source

pub fn options(&self) -> &OperationsOptions

The options operations run with (queue.operations).

Source

pub async fn start( &self, uow: &Uow, operation: Operation, ) -> Result<OperationRecord, QueueError>

Stores operation as pending, as part of uow; returns it.

Source

pub async fn dispatch<J: Job>( &self, uow: &Uow, operation: &OperationRecord, job: J, ) -> Result<String, QueueError>

Queues job as the work of operation, as part of uow; returns the job id. The job’s progress reports go to the operation, and the operation finishes when the job ends. For an idempotent operation the job is kept so the watchdog can dispatch it again.

Source

pub async fn dispatch_batch( &self, uow: &Uow, operation: &OperationRecord, batch: Batch, ) -> Result<JobBatch, QueueError>

Dispatches batch as the work of operation, as part of uow (see Batch::operation): the operation’s progress follows the batch’s finished jobs, and it finishes once the batch and its callbacks have run.

Source

pub async fn find( &self, id: Uuid, ) -> Result<Option<OperationRecord>, QueueError>

The operation id.

Source

pub async fn for_owner( &self, owner: Uuid, limit: usize, ) -> Result<Vec<OperationRecord>, QueueError>

The operations owner started, newest first, at most limit.

Source

pub async fn progress( &self, id: Uuid, done: u64, total: u64, note: impl Into<String>, ) -> Result<Option<OperationRecord>, QueueError>

Records progress: the operation becomes running with done of total and note, and its heartbeat is renewed. A finished operation does not change. Returns the operation afterwards.

Source

pub async fn heartbeat(&self, id: Uuid) -> Result<(), QueueError>

Renews the heartbeat of a pending or running operation (work that runs outside the queue calls this to show it is alive).

Source

pub async fn complete( &self, id: Uuid, result: impl Serialize, ) -> Result<Option<OperationRecord>, QueueError>

Finishes the operation as succeeded with result (what a front end needs: a download URL, counts). A finished operation does not change. Returns the operation afterwards.

Source

pub async fn fail( &self, id: Uuid, error: impl Into<String>, ) -> Result<Option<OperationRecord>, QueueError>

Finishes the operation as failed with error, a message for people. A finished operation does not change.

Source

pub async fn cancel( &self, id: Uuid, ) -> Result<Option<OperationRecord>, QueueError>

Cancels the operation: it becomes cancelled at once, its batch is cancelled, its jobs that have not started are skipped, and running ones stop at their next ctx.progress (cooperative). A finished operation does not change. Returns the operation afterwards (None when there is none).

Source

pub async fn sweep(&self) -> Result<SweepReport, QueueError>

The watchdog (command queue:watchdog; schedule it every minute). For each pending or running operation without a heartbeat for queue.operations.stale_after_secs:

  • a job of it still in the queue means the queue still owns the work (a crashed worker’s reservation is taken over by another): left alone;
  • its batch finished: it finishes the way the batch did;
  • idempotent with a job: the job is dispatched again (at most max_redispatches times);
  • otherwise: failed, “The operation stopped responding.”.

Each step first claims the operation on its row (it must still be stale), so sweeps that overlap (two nodes, or the schedule and a manual queue:watchdog) restart or fail an operation once, and a heartbeat that arrives during the sweep keeps it alive.

Source

pub async fn prune(&self, older_than: Duration) -> Result<u64, QueueError>

Deletes the operations that finished more than older_than ago; returns how many.

Source

pub async fn can_view( &self, principal: &Principal, operation: &OperationRecord, ) -> Result<bool, BoxError>

Whether principal may see operation: its owner, or a holder of queue.operations.view_permission (operations.view) app-wide or in the operation’s workspace.

Source

pub async fn can_cancel( &self, principal: &Principal, operation: &OperationRecord, ) -> Result<bool, BoxError>

Whether principal may cancel operation: its owner, or a holder of queue.operations.cancel_permission (operations.cancel) app-wide or in the operation’s workspace.

Trait Implementations§

Source§

impl Debug for Operations

Source§

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

Formats the value using the given formatter. 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<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> 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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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<T> ValueFor<Option<T>> for T

Source§

fn into_value_for(self) -> Option<T>

The value as the column’s type.
Source§

impl<T> ValueFor<T> for T

Source§

fn into_value_for(self) -> T

The value as the column’s type.
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