#![allow(missing_docs)]
use std::collections::BTreeMap;
use async_trait::async_trait;
use ora_common::{
task::{TaskDataFormat, TaskMetadata, WorkerSelector},
timeout::TimeoutPolicy,
};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use time::OffsetDateTime;
use uuid::Uuid;
pub mod noop;
#[async_trait]
pub trait WorkerRegistry {
type Error: std::error::Error + Send + Sync + 'static;
async fn register_worker(
&self,
worker_id: Uuid,
metadata: &WorkerMetadata,
) -> Result<(), Self::Error>;
async fn unregister_worker(&self, worker_id: Uuid) -> Result<(), Self::Error>;
async fn heartbeat(
&self,
worker_id: Uuid,
data: &HeartbeatData,
) -> Result<HeartbeatResponse, Self::Error>;
async fn workers(&self) -> Result<Vec<WorkerInfo>, Self::Error>;
fn heartbeat_interval(&self) -> time::Duration {
time::Duration::seconds(30)
}
fn enabled(&self) -> bool {
true
}
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct WorkerMetadata {
pub name: Option<String>,
pub description: Option<String>,
pub version: Option<String>,
pub supported_tasks: Vec<SupportedTask>,
#[serde(flatten)]
pub other: BTreeMap<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SupportedTask {
pub worker_selector: WorkerSelector,
pub default_data_format: TaskDataFormat,
pub default_timeout: TimeoutPolicy,
pub metadata: TaskMetadata,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkerInfo {
pub id: Uuid,
pub metadata: WorkerMetadata,
#[serde(with = "time::serde::rfc3339")]
pub registered: OffsetDateTime,
#[serde(with = "time::serde::rfc3339")]
pub last_seen: OffsetDateTime,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HeartbeatData {}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HeartbeatResponse {
pub should_register: bool,
}