use std::{any::type_name, future::Future};
use futures::Stream;
use serde::{Deserialize, Serialize};
use crate::{
codec::Codec,
poller::Poller,
request::State,
worker::{Context, Worker},
};
pub trait Backend<Req> {
type Stream: Stream<Item = Result<Option<Req>, crate::error::Error>>;
type Layer;
type Codec: Codec;
fn poll(self, worker: &Worker<Context>) -> Poller<Self::Stream, Self::Layer>;
}
pub trait BackendExpose<T>
where
Self: Sized,
{
type Request;
type Error;
fn list_workers(
&self,
) -> impl Future<Output = Result<Vec<Worker<WorkerState>>, Self::Error>> + Send;
fn stats(&self) -> impl Future<Output = Result<Stat, Self::Error>> + Send;
fn list_jobs(
&self,
status: &State,
page: i32,
) -> impl Future<Output = Result<Vec<Self::Request>, Self::Error>> + Send;
}
#[derive(Debug, Deserialize, Serialize, Default)]
pub struct Stat {
pub pending: usize,
pub running: usize,
pub dead: usize,
pub failed: usize,
pub success: usize,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct WorkerState {
pub r#type: String,
pub source: String,
}
impl WorkerState {
pub fn new<S>(r#type: String) -> Self {
Self {
r#type,
source: type_name::<S>().to_string(),
}
}
}