taskvisor 0.9.0

In-process Tokio task supervisor: one job per key with queue/replace/reject admission, retries, graceful shutdown, and reliable final outcomes
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! Defines explicit controller submission operations.
//!
//! [`SupervisorHandle::submit`](crate::SupervisorHandle::submit) is the direct entry point to a [`Submit`] operation.
//! [`PreparedSubmission`] provides the same operation with its identity allocated first.
//!
//! ```text
//! ControllerSpec ──► Submit ──► await / execute / try_intake ──► controller intake
//!                         └──► watch ──► TaskWaiter
//! ```
//!
//! Building, configuring, or dropping a submission operation sends no command and starts no work.

use std::future::{Future, IntoFuture};
use std::pin::Pin;
use std::time::Duration;

use super::{
    ControllerError, ControllerSpec,
    engine::{Controller, ControllerHandle},
};
use crate::{
    TaskId, TaskWaiter,
    core::{OwnershipTimed, Unwatched, Waiting, Watched},
};

/// An explicit single-use controller submission operation.
///
/// Configure final-outcome delivery and an optional ownership-admission deadline.
/// Finish with `execute` or `try_intake`. The default waiting, unwatched operation can also be awaited directly.
/// APIs that accept a [`Future`] require `execute()` or [`IntoFuture::into_future`].
/// Every terminal method consumes the operation, including when intake returns an error.
///
/// `Watch` and `Admission` are operation type states.
/// [`watch`](Self::watch) and [`ownership_timeout`](Self::ownership_timeout) select them.
/// Applications do not need to name them.
#[must_use = "await the default submission, call and await `execute`, or call `try_intake`"]
pub struct Submit<'a, Watch = Unwatched, Admission = Waiting> {
    /// Direct or pre-identified submission payload retained until a terminal method runs.
    request: SubmitRequest<'a>,

    /// Typed final-outcome selection.
    watch: Watch,

    /// Typed ownership-admission selection.
    _admission: Admission,
}

/// Payload origin kept inline by a [`Submit`] operation.
enum SubmitRequest<'a> {
    /// Direct submission whose identity is allocated only when a terminal method runs.
    Direct {
        /// Missing when this supervisor was built without a controller.
        controller: Option<&'a Controller>,
        /// Specification retained even when the controller is not configured.
        spec: ControllerSpec,
    },

    /// Prepared submission carrying the identity exposed before intake.
    Prepared {
        controller: ControllerHandle,
        id: TaskId,
        spec: ControllerSpec,
    },
}

impl<'a> Submit<'a> {
    /// Lazy direct submission with no identity or controller command.
    ///
    /// The terminal method reports a missing controller as [`ControllerError::NotConfigured`].
    /// This keeps [`SupervisorHandle::submit`](crate::SupervisorHandle::submit) infallible.
    #[inline]
    pub(crate) fn direct(controller: Option<&'a Controller>, spec: ControllerSpec) -> Self {
        Self {
            request: SubmitRequest::Direct { controller, spec },
            watch: Unwatched,
            _admission: Waiting,
        }
    }
}

impl Submit<'static> {
    #[inline]
    fn prepared(controller: ControllerHandle, id: TaskId, spec: ControllerSpec) -> Self {
        Self {
            request: SubmitRequest::Prepared {
                controller,
                id,
                spec,
            },
            watch: Unwatched,
            _admission: Waiting,
        }
    }
}

impl SubmitRequest<'_> {
    /// Terminal resolution of controller configuration and direct identity allocation.
    #[inline(always)]
    fn into_parts(self) -> Result<(ControllerHandle, TaskId, ControllerSpec), ControllerError> {
        match self {
            Self::Direct {
                controller: Some(controller),
                spec,
            } => Ok((controller.handle(), TaskId::next(), spec)),
            Self::Direct {
                controller: None, ..
            } => Err(ControllerError::NotConfigured),
            Self::Prepared {
                controller,
                id,
                spec,
            } => Ok((controller, id, spec)),
        }
    }

    /// Performs fail-fast unwatched intake while keeping direct setup behind one call boundary.
    #[inline(always)]
    fn try_submit(self) -> Result<TaskId, ControllerError> {
        match self {
            Self::Direct {
                controller: Some(controller),
                spec,
            } => try_submit_direct(controller, spec),
            Self::Direct {
                controller: None, ..
            } => Err(ControllerError::NotConfigured),
            Self::Prepared {
                controller,
                id,
                spec,
            } => controller.try_submit_prepared(id, spec),
        }
    }

    /// Performs fail-fast watched intake while keeping direct setup behind one call boundary.
    #[inline(always)]
    fn try_submit_and_watch(
        self,
    ) -> Result<(TaskId, tokio::sync::oneshot::Receiver<crate::TaskOutcome>), ControllerError> {
        match self {
            Self::Direct {
                controller: Some(controller),
                spec,
            } => try_submit_direct_and_watch(controller, spec),
            Self::Direct {
                controller: None, ..
            } => Err(ControllerError::NotConfigured),
            Self::Prepared {
                controller,
                id,
                spec,
            } => controller.try_submit_prepared_and_watch(id, spec),
        }
    }
}

/// Allocates direct-submission state at the terminal call boundary.
fn try_submit_direct(
    controller: &Controller,
    spec: ControllerSpec,
) -> Result<TaskId, ControllerError> {
    controller
        .handle()
        .try_submit_prepared(TaskId::next(), spec)
}

/// Allocates direct watched-submission state at the terminal call boundary.
fn try_submit_direct_and_watch(
    controller: &Controller,
    spec: ControllerSpec,
) -> Result<(TaskId, tokio::sync::oneshot::Receiver<crate::TaskOutcome>), ControllerError> {
    controller
        .handle()
        .try_submit_prepared_and_watch(TaskId::next(), spec)
}

impl<'a, Admission> Submit<'a, Unwatched, Admission> {
    /// Final-outcome delivery through [`TaskWaiter`].
    ///
    /// Successful intake returns only [`TaskWaiter`].
    /// Its [`TaskWaiter::id`] is the submission identity.
    /// The waiter later reports controller rejection or the admitted task's final outcome.
    #[must_use = "configure or execute the watched submission"]
    #[inline]
    pub fn watch(self) -> Submit<'a, Watched, Admission> {
        Submit {
            request: self.request,
            watch: Watched,
            _admission: self._admission,
        }
    }
}

impl<'a, Watch> Submit<'a, Watch, Waiting> {
    /// Cleanup-ownership admission deadline.
    ///
    /// The deadline stops after ownership succeeds.
    /// `execute` can then wait without a deadline for controller command capacity.
    /// An immediately available permit can succeed when `wait_for` is [`Duration::ZERO`].
    /// A timeout sends no command and publishes no lifecycle event.
    #[inline]
    pub fn ownership_timeout(self, wait_for: Duration) -> Submit<'a, Watch, OwnershipTimed> {
        Submit {
            request: self.request,
            watch: self.watch,
            _admission: OwnershipTimed(wait_for),
        }
    }
}

impl Submit<'_, Unwatched, Waiting> {
    /// Waiting controller command intake with an unwatched [`TaskId`] result.
    ///
    /// `Ok(id)` confirms only command intake.
    /// Slot admission and runtime registration happen later.
    /// Awaiting the default operation directly is equivalent to calling this method.
    /// A successful [`PreparedSubmission`] cannot produce `NotConfigured`.
    ///
    /// # Errors
    ///
    /// - Returns [`ControllerError::NotConfigured`] when a direct submission has no controller;
    /// - [`ControllerError::ThreadStartFailed`] when cleanup workers cannot start;
    /// - [`ControllerError::ResourceLimit`] when cleanup ownership is unavailable;
    /// - [`ControllerError::Closed`] when controller intake is closed.
    #[inline]
    pub async fn execute(self) -> Result<TaskId, ControllerError> {
        let (controller, id, spec) = self.request.into_parts()?;
        controller.submit_prepared(id, spec).await
    }

    /// Fail-fast controller command intake with an unwatched [`TaskId`] result.
    ///
    /// `Ok(id)` has the same intake-only meaning as [`execute`](Self::execute).
    /// A successful [`PreparedSubmission`] cannot produce `NotConfigured`.
    ///
    /// # Errors
    ///
    /// - Returns [`ControllerError::NotConfigured`] when a direct submission has no controller;
    /// - [`ControllerError::ThreadStartFailed`] when cleanup workers cannot start;
    /// - [`ControllerError::ResourceLimit`] when cleanup ownership is unavailable;
    /// - [`ControllerError::Full`] when controller command capacity is unavailable;
    /// - [`ControllerError::Closed`] when controller intake is closed.
    #[inline(always)]
    pub fn try_intake(self) -> Result<TaskId, ControllerError> {
        self.request.try_submit()
    }
}

impl<'a> IntoFuture for Submit<'a, Unwatched, Waiting> {
    type Output = Result<TaskId, ControllerError>;
    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>;

    #[inline]
    fn into_future(self) -> Self::IntoFuture {
        Box::pin(self.execute())
    }
}

impl Submit<'_, Unwatched, OwnershipTimed> {
    /// Ownership-bounded admission followed by normal command-queue backpressure.
    ///
    /// A successful [`PreparedSubmission`] cannot produce `NotConfigured`.
    ///
    /// # Errors
    ///
    /// - Returns [`ControllerError::NotConfigured`] when a direct submission has no controller;
    /// - [`ControllerError::ThreadStartFailed`] when cleanup workers cannot start;
    /// - [`ControllerError::ResourceLimit`] when cleanup ownership cannot be granted;
    /// - [`ControllerError::OwnershipAdmissionTimeout`] when cleanup ownership remains unavailable at the configured deadline;
    /// - [`ControllerError::Closed`] when controller intake is closed.
    #[inline]
    pub async fn execute(self) -> Result<TaskId, ControllerError> {
        let wait_for = self._admission.0;
        let (controller, id, spec) = self.request.into_parts()?;
        controller
            .submit_prepared_with_ownership_timeout(id, spec, wait_for)
            .await
    }
}

impl Submit<'_, Watched, Waiting> {
    /// Waiting controller command intake with a final-outcome waiter.
    ///
    /// Success confirms only controller command intake.
    /// [`TaskWaiter::id`] is the submission identity.
    /// [`TaskWaiter::wait`] reports rejection or the admitted task's final outcome.
    /// A successful [`PreparedSubmission`] cannot produce `NotConfigured`.
    ///
    /// # Errors
    ///
    /// - Returns [`ControllerError::NotConfigured`] when a direct submission has no controller;
    /// - [`ControllerError::ThreadStartFailed`] when cleanup workers cannot start;
    /// - [`ControllerError::ResourceLimit`] when cleanup ownership is unavailable;
    /// - [`ControllerError::Closed`] when controller intake is closed.
    #[inline]
    pub async fn execute(self) -> Result<TaskWaiter, ControllerError> {
        let (controller, id, spec) = self.request.into_parts()?;
        let (id, receiver) = controller.submit_prepared_and_watch(id, spec).await?;
        Ok(TaskWaiter::new(id, receiver))
    }

    /// Fail-fast controller command intake with a final-outcome waiter.
    ///
    /// A successful [`PreparedSubmission`] cannot produce `NotConfigured`.
    ///
    /// # Errors
    ///
    /// - Returns [`ControllerError::NotConfigured`] when a direct submission has no controller;
    /// - [`ControllerError::ThreadStartFailed`] when cleanup workers cannot start;
    /// - [`ControllerError::ResourceLimit`] when cleanup ownership is unavailable;
    /// - [`ControllerError::Full`] when controller command capacity is unavailable;
    /// - [`ControllerError::Closed`] when controller intake is closed.
    #[inline(always)]
    pub fn try_intake(self) -> Result<TaskWaiter, ControllerError> {
        let (id, receiver) = self.request.try_submit_and_watch()?;
        Ok(TaskWaiter::new(id, receiver))
    }
}

impl Submit<'_, Watched, OwnershipTimed> {
    /// Ownership-bounded admission with a final-outcome waiter after command intake.
    ///
    /// A successful [`PreparedSubmission`] cannot produce `NotConfigured`.
    ///
    /// # Errors
    ///
    /// - Returns [`ControllerError::NotConfigured`] when a direct submission has no controller;
    /// - [`ControllerError::ThreadStartFailed`] when cleanup workers cannot start;
    /// - [`ControllerError::ResourceLimit`] when cleanup ownership cannot be granted;
    /// - [`ControllerError::OwnershipAdmissionTimeout`] when cleanup ownership remains unavailable at the configured deadline;
    /// - [`ControllerError::Closed`] when controller intake is closed.
    #[inline]
    pub async fn execute(self) -> Result<TaskWaiter, ControllerError> {
        let wait_for = self._admission.0;
        let (controller, id, spec) = self.request.into_parts()?;
        let (id, receiver) = controller
            .submit_prepared_and_watch_with_ownership_timeout(id, spec, wait_for)
            .await?;
        Ok(TaskWaiter::new(id, receiver))
    }
}

impl<Watch, Admission> std::fmt::Debug for Submit<'_, Watch, Admission> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut debug = f.debug_struct("Submit");

        match &self.request {
            SubmitRequest::Direct { controller, spec } => debug
                .field("controller_configured", &controller.is_some())
                .field("spec", spec),
            SubmitRequest::Prepared { id, spec, .. } => debug.field("id", id).field("spec", spec),
        };

        debug.finish_non_exhaustive()
    }
}

/// A controller request with an identity allocated before intake.
///
/// Obtain this value from [`SupervisorHandle::prepare_submission`](crate::SupervisorHandle::prepare_submission).
/// Record [`id`](Self::id) when the identity is needed before intake. Consume the value with [`submit`](Self::submit).
/// Preparation reserves no task name, slot, queue capacity, or runtime capacity. It publishes no event.
///
/// Dropping this value or the resulting [`Submit`] operation starts no work.
/// Retrying after any terminal intake error requires a new prepared value and a new task identity.
#[must_use = "call submit, then await, execute, or try the resulting operation"]
pub struct PreparedSubmission {
    /// Controller command sender used when the submission operation commits.
    controller: ControllerHandle,

    /// Task ID allocated before any controller command is sent.
    id: TaskId,

    /// Submission specification held until intake.
    spec: ControllerSpec,
}

impl PreparedSubmission {
    pub(crate) fn new(controller: ControllerHandle, spec: ControllerSpec) -> Self {
        Self {
            controller,
            id: TaskId::next(),
            spec,
        }
    }

    /// Preallocated submission identity.
    ///
    /// No event for this identity is published before the operation returned by [`submit`](Self::submit) reaches successful command intake.
    /// The identity does not prove that intake or slot admission occurred.
    #[must_use]
    pub fn id(&self) -> TaskId {
        self.id
    }

    /// Borrowed submission specification before intake.
    #[must_use = "use the prepared controller specification"]
    pub fn spec(&self) -> &ControllerSpec {
        &self.spec
    }

    /// Single-use submission operation preserving the prepared identity.
    ///
    /// Await the default operation directly.
    /// Configure it before finishing with `execute` or `try_intake` when needed.
    #[must_use = "await, execute, or try the prepared submission operation"]
    #[inline]
    pub fn submit(self) -> Submit<'static> {
        let Self {
            controller,
            id,
            spec,
        } = self;
        Submit::prepared(controller, id, spec)
    }
}

impl std::fmt::Debug for PreparedSubmission {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PreparedSubmission")
            .field("id", &self.id)
            .field("spec", &self.spec)
            .finish_non_exhaustive()
    }
}