use crate::app::context::AppContext;
use crate::error::RoadsterResult;
use async_trait::async_trait;
use axum_core::extract::FromRef;
use std::any::Any;
use tokio_util::sync::CancellationToken;
pub mod function;
#[cfg(feature = "grpc")]
pub mod grpc;
#[cfg(feature = "http")]
pub mod http;
pub mod registry;
pub(crate) mod runner;
#[cfg(feature = "worker")]
pub mod worker;
#[cfg_attr(
feature = "http",
doc = r"- [HTTP API][crate::service::http::service::HttpService]"
)]
#[cfg_attr(
feature = "worker-sidekiq",
doc = r"- [Sidekiq worker processor][crate::service::worker::backend::sidekiq::SidekiqWorkerService]"
)]
#[cfg_attr(
feature = "worker-pg",
doc = r"- [Postgres worker processor][crate::service::worker::backend::pg::PgWorkerService]"
)]
#[cfg_attr(
feature = "grpc",
doc = r"- [gRPC API][crate::service::grpc::service::GrpcService]"
)]
#[cfg_attr(test, mockall::automock)]
#[async_trait]
pub trait Service<S>: Send + Sync + ServiceAsAny<S>
where
S: Clone + Send + Sync + 'static,
AppContext: FromRef<S>,
{
fn name(&self) -> String;
fn enabled(&self, state: &S) -> bool;
async fn before_run(&self, _state: &S) -> RoadsterResult<()> {
Ok(())
}
async fn run(self: Box<Self>, state: &S, cancel_token: CancellationToken)
-> RoadsterResult<()>;
}
#[cfg_attr(test, mockall::automock)]
#[async_trait]
pub trait ServiceBuilder<S, Srvc>
where
S: Clone + Send + Sync + 'static,
AppContext: FromRef<S>,
Srvc: Service<S>,
{
fn name(&self) -> String;
fn enabled(&self, state: &S) -> bool;
async fn build(self, state: &S) -> RoadsterResult<Srvc>;
}
pub trait ServiceAsAny<S>
where
S: Clone + Send + Sync + 'static,
AppContext: FromRef<S>,
{
fn as_any(&self) -> &dyn Any;
}
impl<T, S> ServiceAsAny<S> for T
where
S: Clone + Send + Sync + 'static,
AppContext: FromRef<S>,
T: Service<S> + 'static,
{
fn as_any(&self) -> &dyn Any {
self
}
}