ora_worker/
lib.rs

1//! Worker and worker implementations for Ora.
2
3#![warn(clippy::pedantic, missing_docs)]
4#![allow(clippy::module_name_repetitions, clippy::ignored_unit_patterns)]
5
6use async_trait::async_trait;
7use ora_common::task::{TaskDataFormat, TaskDefinition, WorkerSelector};
8use registry::SupportedTask;
9use tokio_util::sync::CancellationToken;
10use uuid::Uuid;
11
12pub mod store;
13pub mod worker;
14
15#[cfg(feature = "registry")]
16pub mod registry;
17
18/// A context that is passed to each worker task execution.
19#[derive(Debug, Clone)]
20pub struct TaskContext {
21    task_id: Uuid,
22    cancellation: CancellationToken,
23}
24
25impl TaskContext {
26    /// Return the task's ID.
27    #[must_use]
28    #[inline]
29    pub const fn task_id(&self) -> Uuid {
30        self.task_id
31    }
32
33    /// Return whether the task was cancelled.
34    #[must_use]
35    pub fn is_cancelled(&self) -> bool {
36        self.cancellation.is_cancelled()
37    }
38
39    /// Wait for task cancellation.
40    pub async fn cancelled(&self) {
41        self.cancellation.cancelled().await;
42    }
43}
44
45/// A handler that works with raw input and output
46/// without task type attached.
47#[async_trait]
48pub trait RawHandler {
49    /// Return the selector that should be used to
50    /// match tasks to this handler.
51    fn selector(&self) -> &WorkerSelector;
52
53    /// The data format of the task output.
54    fn output_format(&self) -> TaskDataFormat;
55
56    /// Optional information about the supported task
57    /// expected by this handler.
58    fn supported_task(&self) -> Option<SupportedTask>;
59
60    /// Execute a task.
61    async fn run(&self, context: TaskContext, task: TaskDefinition) -> eyre::Result<Vec<u8>>;
62}
63
64// Not public API, do not use!
65#[doc(hidden)]
66pub mod _private {
67    use tokio_util::sync::CancellationToken;
68    use uuid::Uuid;
69
70    use crate::TaskContext;
71
72    #[must_use]
73    pub fn new_context(task_id: Uuid, cancellation: CancellationToken) -> TaskContext {
74        TaskContext {
75            task_id,
76            cancellation,
77        }
78    }
79}