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
//! Backend store implementations required by workers.
use async_trait::async_trait;
use futures::Stream;
use ora_common::task::{TaskDataFormat, TaskDefinition, WorkerSelector};
use uuid::Uuid;
/// A store interface for workers.
#[async_trait]
pub trait WorkerStore: Send + Sync + Clone {
/// An error type returned by operations.
type Error: std::error::Error + Send + Sync + 'static;
/// An event stream that can be used to watch for changes.
type Events: Stream<Item = Result<WorkerStoreEvent, Self::Error>>;
/// Subscribe for new events with the given worker selectors.
async fn events(&self, selectors: &[WorkerSelector]) -> Result<Self::Events, Self::Error>;
/// Return all tasks that should be executed with any of the given worker selectors.
async fn ready_tasks(
&self,
selectors: &[WorkerSelector],
) -> Result<Vec<ReadyTask>, Self::Error>;
/// Select a task to run, if this function returns `false`, the
/// worker should drop the task instead of running it.
///
/// The worker ID should identify the worker, but any other
/// unique value can be used.
///
/// This function might return `false` for a variety of reasons,
/// the most common one being that an another worker has already started
/// the execution of the task.
async fn select_task(&self, task_id: Uuid, worker_id: Uuid) -> Result<bool, Self::Error>;
/// Update the task status as started.
async fn task_started(&self, task_id: Uuid) -> Result<(), Self::Error>;
/// Update the task status as successful with the given output.
async fn task_succeeded(
&self,
task_id: Uuid,
output: Vec<u8>,
output_format: TaskDataFormat,
) -> Result<(), Self::Error>;
/// Update the task status as failed with the given reason.
async fn task_failed(&self, task_id: Uuid, reason: String) -> Result<(), Self::Error>;
}
/// A task that is ready to be run by a worker.
#[derive(Debug, Clone)]
pub struct ReadyTask {
/// The task's ID.
pub id: Uuid,
/// The task's definition.
pub definition: TaskDefinition,
}
/// An event returned by the store.
#[derive(Debug, Clone)]
pub enum WorkerStoreEvent {
/// A task is ready to be run.
TaskReady(ReadyTask),
/// A task was cancelled.
TaskCancelled(Uuid),
}