use crate::app::context::AppContext;
use async_trait::async_trait;
use axum_core::extract::FromRef;
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(type Error = crate::error::Error;))]
#[async_trait]
pub trait Service<S>: Send + Sync
where
S: 'static + Send + Sync + Clone,
AppContext: FromRef<S>,
{
type Error: Send + Sync + std::error::Error;
fn name(&self) -> String;
fn enabled(&self, state: &S) -> bool;
async fn before_run(&self, #[allow(unused_variables)] state: &S) -> Result<(), Self::Error> {
Ok(())
}
async fn run(
self: Box<Self>,
state: &S,
cancel_token: CancellationToken,
) -> Result<(), Self::Error>;
}
#[cfg_attr(test, mockall::automock(type Error = crate::error::Error;))]
#[async_trait]
pub trait ServiceBuilder<S, Srvc>
where
S: 'static + Send + Sync + Clone,
AppContext: FromRef<S>,
Srvc: Service<S>,
{
type Error: Send + Sync + std::error::Error;
fn name(&self) -> String;
fn enabled(&self, state: &S) -> bool;
async fn build(self, state: &S) -> Result<Srvc, Self::Error>;
}