Skip to main content

ora_worker/
store.rs

1//! Backend store implementations required by workers.
2
3use async_trait::async_trait;
4use futures::Stream;
5use ora_common::task::{TaskDataFormat, TaskDefinition, WorkerSelector};
6use uuid::Uuid;
7
8/// A store interface for workers.
9#[async_trait]
10pub trait WorkerStore: Send + Sync + Clone {
11    /// An error type returned by operations.
12    type Error: std::error::Error + Send + Sync + 'static;
13
14    /// An event stream that can be used to watch for changes.
15    type Events: Stream<Item = Result<WorkerStoreEvent, Self::Error>>;
16
17    /// Subscribe for new events with the given worker selectors.
18    async fn events(&self, selectors: &[WorkerSelector]) -> Result<Self::Events, Self::Error>;
19
20    /// Return all tasks that should be executed with any of the given worker selectors.
21    async fn ready_tasks(
22        &self,
23        selectors: &[WorkerSelector],
24    ) -> Result<Vec<ReadyTask>, Self::Error>;
25
26    /// Select a task to run, if this function returns `false`, the
27    /// worker should drop the task instead of running it.
28    ///
29    /// The worker ID should identify the worker, but any other
30    /// unique value can be used.
31    ///
32    /// This function might return `false` for a variety of reasons,
33    /// the most common one being that an another worker has already started
34    /// the execution of the task.
35    async fn select_task(&self, task_id: Uuid, worker_id: Uuid) -> Result<bool, Self::Error>;
36
37    /// Update the task status as started.
38    async fn task_started(&self, task_id: Uuid) -> Result<(), Self::Error>;
39
40    /// Update the task status as successful with the given output.
41    async fn task_succeeded(
42        &self,
43        task_id: Uuid,
44        output: Vec<u8>,
45        output_format: TaskDataFormat,
46    ) -> Result<(), Self::Error>;
47
48    /// Update the task status as failed with the given reason.
49    async fn task_failed(&self, task_id: Uuid, reason: String) -> Result<(), Self::Error>;
50
51    /// The task was cancelled, possibly due to the worker shutting down.
52    async fn task_cancelled(&self, task_id: Uuid) -> Result<(), Self::Error>;
53}
54
55/// A task that is ready to be run by a worker.
56#[derive(Debug, Clone)]
57pub struct ReadyTask {
58    /// The task's ID.
59    pub id: Uuid,
60    /// The task's definition.
61    pub definition: TaskDefinition,
62}
63
64/// An event returned by the store.
65#[derive(Debug, Clone)]
66pub enum WorkerStoreEvent {
67    /// A task is ready to be run.
68    TaskReady(ReadyTask),
69    /// A task was cancelled.
70    TaskCancelled(Uuid),
71}