first commit
This commit is contained in:
50
crates/svc/examples/from_async_fn.rs
Normal file
50
crates/svc/examples/from_async_fn.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use thiserror::Error;
|
||||
use tokio::time::sleep;
|
||||
|
||||
use svc::error::SvcResult;
|
||||
use svc::manager::ServiceManager;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum SimpleError {
|
||||
#[error("That didn't work")]
|
||||
Nope,
|
||||
}
|
||||
|
||||
async fn run() -> Result<(), SimpleError> {
|
||||
let dur = Duration::from_millis(800);
|
||||
|
||||
println!("Hello");
|
||||
|
||||
println!("1");
|
||||
sleep(dur).await;
|
||||
println!("2");
|
||||
sleep(dur).await;
|
||||
println!("3");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> SvcResult<()> {
|
||||
pretty_env_logger::formatted_builder()
|
||||
.filter_level(log::LevelFilter::Debug)
|
||||
.parse_default_env()
|
||||
.init();
|
||||
|
||||
let (mut client, future) = ServiceManager::spawn();
|
||||
|
||||
client.register_function("foo", run()).await?;
|
||||
client.start("foo").await?;
|
||||
println!("main: service configured");
|
||||
|
||||
client.wait_for_start("foo").await?;
|
||||
println!("main: service started");
|
||||
|
||||
client.shutdown().await?;
|
||||
future.await??;
|
||||
println!("main: service stopped");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
70
crates/svc/examples/policy.rs
Normal file
70
crates/svc/examples/policy.rs
Normal file
@@ -0,0 +1,70 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use svc::policy::{Policy, Retry};
|
||||
use svc::runservice::StandardService;
|
||||
use thiserror::Error;
|
||||
|
||||
use svc::error::SvcResult;
|
||||
use svc::manager::ServiceManager;
|
||||
use svc::traits::{Service, ServiceState};
|
||||
|
||||
#[derive(Clone)]
|
||||
struct PolicyService {
|
||||
counter: u32,
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Not done yet")]
|
||||
MoreToDo,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Service for PolicyService {
|
||||
type Error = Error;
|
||||
|
||||
async fn run(&mut self) -> Result<(), Error> {
|
||||
println!("Hello {}", self.counter);
|
||||
self.counter += 1;
|
||||
|
||||
// returning an Err will invoke the policy for the Running state
|
||||
Err(Error::MoreToDo)
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> SvcResult<()> {
|
||||
const NAME: &str = "policy-service";
|
||||
|
||||
pretty_env_logger::formatted_builder()
|
||||
.filter_level(log::LevelFilter::Debug)
|
||||
.parse_default_env()
|
||||
.init();
|
||||
|
||||
let (mut client, future) = ServiceManager::spawn();
|
||||
|
||||
let svc = PolicyService { counter: 0 };
|
||||
|
||||
// Manually construct a ServiceRunner, and set a specific policy for
|
||||
// handling errors during .run()
|
||||
let svcr = StandardService::new(NAME, svc).with_run_policy(
|
||||
// Try up to 5 times, waiting 300ms between each attempt
|
||||
Policy::new()
|
||||
.with_retry(Retry::Limit(5))
|
||||
.with_delay(Duration::from_millis(300)),
|
||||
);
|
||||
|
||||
let uuid = client.register(NAME, svcr).await?;
|
||||
client.start(uuid).await?;
|
||||
println!("main: service will attempt to run 5 times");
|
||||
|
||||
client.wait_for_start(uuid).await?;
|
||||
println!("main: service started");
|
||||
|
||||
client.wait_for_state(uuid, ServiceState::Failed).await?;
|
||||
client.shutdown().await?;
|
||||
future.await??;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
56
crates/svc/examples/restart.rs
Normal file
56
crates/svc/examples/restart.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use tokio::time::sleep;
|
||||
|
||||
use svc::error::{RunSvcError, SvcResult};
|
||||
use svc::manager::ServiceManager;
|
||||
|
||||
async fn run() -> Result<(), RunSvcError> {
|
||||
let dur = Duration::from_millis(200);
|
||||
|
||||
println!("Hello");
|
||||
|
||||
let mut counter = 0;
|
||||
|
||||
loop {
|
||||
println!("{counter}");
|
||||
sleep(dur).await;
|
||||
counter += 1;
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> SvcResult<()> {
|
||||
pretty_env_logger::formatted_builder()
|
||||
.filter_level(log::LevelFilter::Debug)
|
||||
.parse_default_env()
|
||||
.init();
|
||||
|
||||
let (mut client, future) = ServiceManager::spawn();
|
||||
|
||||
client.register_function("foo", run()).await?;
|
||||
|
||||
client.start("foo").await?;
|
||||
println!("main: service configured");
|
||||
|
||||
client.wait_for_start("foo").await?;
|
||||
println!("main: service started");
|
||||
|
||||
sleep(Duration::from_millis(1000)).await;
|
||||
|
||||
client.stop("foo").await?;
|
||||
client.wait_for_stop("foo").await?;
|
||||
|
||||
println!("main: service stopped");
|
||||
|
||||
client.start("foo").await?;
|
||||
client.wait_for_start("foo").await?;
|
||||
println!("main: service started");
|
||||
|
||||
sleep(Duration::from_millis(1000)).await;
|
||||
client.shutdown().await?;
|
||||
|
||||
future.await??;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
86
crates/svc/examples/simple.rs
Normal file
86
crates/svc/examples/simple.rs
Normal file
@@ -0,0 +1,86 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use thiserror::Error;
|
||||
use tokio::time::sleep;
|
||||
|
||||
use svc::error::SvcResult;
|
||||
use svc::manager::ServiceManager;
|
||||
use svc::traits::Service;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Simple {
|
||||
name: String,
|
||||
counter: u32,
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum SimpleError {
|
||||
#[error("That didn't work..")]
|
||||
Nope,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Service for Simple {
|
||||
type Error = SimpleError;
|
||||
|
||||
async fn run(&mut self) -> Result<(), SimpleError> {
|
||||
let dur = Duration::from_millis(300);
|
||||
|
||||
println!("Hello from {}", self.name);
|
||||
|
||||
println!("1");
|
||||
sleep(dur).await;
|
||||
println!("2");
|
||||
sleep(dur).await;
|
||||
println!("3");
|
||||
|
||||
println!("Done running. Now going to stop (this will fail the first time)");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn stop(&mut self) -> Result<(), SimpleError> {
|
||||
self.counter += 1;
|
||||
|
||||
// pretend this service doesn't succeed at stopping right away
|
||||
if self.counter > 1 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(SimpleError::Nope)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> SvcResult<()> {
|
||||
pretty_env_logger::formatted_builder()
|
||||
.filter_level(log::LevelFilter::Debug)
|
||||
.parse_default_env()
|
||||
.init();
|
||||
|
||||
let (mut client, future) = ServiceManager::spawn();
|
||||
|
||||
let svc = Simple {
|
||||
name: "Simple Service".to_string(),
|
||||
counter: 0,
|
||||
};
|
||||
|
||||
client.register_service("foo", svc).await?;
|
||||
client.start("foo").await?;
|
||||
|
||||
println!("main: service configured");
|
||||
|
||||
client.wait_for_start("foo").await?;
|
||||
|
||||
println!("main: service started");
|
||||
|
||||
client.wait_for_stop("foo").await?;
|
||||
|
||||
println!("main: service stopped");
|
||||
|
||||
client.shutdown().await?;
|
||||
|
||||
future.await??;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user