Skip to content

Commit

Permalink
feat(sdk/rust): add with_embedded_oomagent api
Browse files Browse the repository at this point in the history
  • Loading branch information
wfxr committed Jan 27, 2022
1 parent 7d9c2b8 commit 3d370e6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
12 changes: 12 additions & 0 deletions sdk/rust/examples/client_with_embedded_oomagent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use oomclient::Client;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut client = Client::with_default_embedded_oomagent().await?;

let response = client.health_check().await?;

println!("RESPONSE={:?}", response);

Ok(())
}
16 changes: 16 additions & 0 deletions sdk/rust/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{
SnapshotRequest,
SyncRequest,
},
server::ServerWrapper,
util::{parse_raw_feature_values, parse_raw_values},
EntityRow,
Result,
Expand All @@ -34,6 +35,7 @@ pub struct Client {
inner: OomAgentClient<transport::Channel>,
}

// TODO: Add a Builder to create the client
impl Client {
pub async fn connect<D>(dst: D) -> Result<Self>
where
Expand All @@ -43,6 +45,20 @@ impl Client {
Ok(Self { inner: OomAgentClient::connect(dst).await? })
}

pub async fn with_embedded_oomagent<P1, P2>(bin_path: Option<P1>, cfg_path: Option<P2>) -> Result<Self>
where
P1: AsRef<Path>,
P2: AsRef<Path>,
{
let oomagent = ServerWrapper::new(bin_path, cfg_path, None).await?;
Self::connect(oomagent.address().to_string()).await
}

pub async fn with_default_embedded_oomagent() -> Result<Self> {
let oomagent = ServerWrapper::default().await?;
Self::connect(format!("http://{}", oomagent.address())).await
}

pub async fn health_check(&mut self) -> Result<()> {
Ok(self.inner.health_check(HealthCheckRequest {}).await.map(|_| ())?)
}
Expand Down
7 changes: 4 additions & 3 deletions sdk/rust/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@ pub struct ServerWrapper {
handle: Handle,
}

// TODO: Add a Builder to create the server wrapper
impl ServerWrapper {
pub async fn new<P1, P2>(cmd_path: Option<P1>, cfg_path: Option<P2>, port: Option<u16>) -> Result<Self>
pub async fn new<P1, P2>(bin_path: Option<P1>, cfg_path: Option<P2>, port: Option<u16>) -> Result<Self>
where
P1: AsRef<Path>,
P2: AsRef<Path>,
{
let cmd_path = cmd_path.map(|p| p.as_ref().to_owned());
let bin_path = bin_path.map(|p| p.as_ref().to_owned());
let cfg_path = cfg_path.map(|p| p.as_ref().to_owned());
let port = port.unwrap_or(0);

let mut signals = Signals::new(&[SIGHUP, SIGTERM, SIGINT, SIGQUIT])?;
let handle = signals.handle();

let mut oomagent = Command::new(cmd_path.clone().unwrap_or_else(|| "oomagent".into()));
let mut oomagent = Command::new(bin_path.clone().unwrap_or_else(|| "oomagent".into()));
oomagent.arg("--port").arg(port.to_string());
if let Some(cfg_path) = cfg_path.clone() {
oomagent.arg("--config").arg(cfg_path);
Expand Down

0 comments on commit 3d370e6

Please sign in to comment.