25 lines
692 B
Rust
25 lines
692 B
Rust
use std::sync::Arc;
|
|
|
|
use crate::{context::Context, log_err, log_warn, settings::Result};
|
|
|
|
#[async_trait::async_trait]
|
|
pub trait Service: Sync + Send + 'static {
|
|
fn new() -> Arc<Self>
|
|
where
|
|
Self: Sized;
|
|
|
|
async fn run(self: Arc<Self>, context: Context) {
|
|
let mut code = 0;
|
|
if let Err(e) = self.clone().run_inner(context.clone()).await {
|
|
log_err!("{} error: {}", self.service_name(), e);
|
|
code = 1;
|
|
}
|
|
log_warn!("{} stopped: exiting...", self.service_name());
|
|
std::process::exit(code);
|
|
}
|
|
|
|
async fn run_inner(self: Arc<Self>, context: Context) -> Result<()>;
|
|
|
|
fn service_name(&self) -> &'static str;
|
|
}
|