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
//! Task middleware for pre/post execution hooks.
//!
//! Middleware runs runner-local (not distributed). For distributed status
//! callbacks, use the trigger system instead.
use async_trait;
use ;
use crate;
/// Hook that runs before and after each task invocation on this runner.
///
/// Middlewares are called in registration order (FIFO for `before`,
/// LIFO for `after`). If any `before` hook returns `Err`, the task
/// is not executed and the error is propagated.
///
/// # Example
///
/// ```rust
/// use async_trait::async_trait;
/// use rustvello_core::middleware::TaskMiddleware;
/// use rustvello_core::error::{RustvelloError, RustvelloResult};
/// use rustvello_proto::identifiers::{InvocationId, TaskId};
///
/// struct LoggingMiddleware;
///
/// #[async_trait]
/// impl TaskMiddleware for LoggingMiddleware {
/// async fn before(&self, inv_id: &InvocationId, task_id: &TaskId) -> RustvelloResult<()> {
/// println!("Starting {} for {}", task_id, inv_id);
/// Ok(())
/// }
///
/// async fn after(
/// &self,
/// inv_id: &InvocationId,
/// task_id: &TaskId,
/// result: &Result<String, RustvelloError>,
/// ) -> RustvelloResult<()> {
/// println!("Finished {} for {}: {}", task_id, inv_id, result.is_ok());
/// Ok(())
/// }
/// }
/// ```