Skip to content

Commit

Permalink
Add daemonize flag (#36)
Browse files Browse the repository at this point in the history
* Add daemonize flag

* Nop out daemonization on windows
  • Loading branch information
twizmwazin authored Dec 8, 2023
1 parent 342b08a commit ffc4a43
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 19 deletions.
3 changes: 3 additions & 0 deletions crates/bh_agent_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ log = "0.4.20"
env_logger = { version = "0.10.0", default-features = false, features = ["auto-color", "humantime"] }
bimap = "0.6.3"
clap = { version = "4.4.11", features = ["derive"] }

[target.'cfg(not(target_os = "windows"))'.dependencies]
daemonize = "0.5.0"
64 changes: 45 additions & 19 deletions crates/bh_agent_server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,70 @@
use std::net::IpAddr;
use std::{any, net::IpAddr};

use clap::Parser;
use futures::{future, prelude::*};
use tarpc::{
server::{self, Channel},
tokio_serde::formats::Json,
};
use tokio::runtime;

use bh_agent_common::BhAgentService;
use bh_agent_server::BhAgentServer;

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Args {
/// The address to listen on
address: IpAddr,
/// The port to listen on
port: u16,
#[cfg(not(target_os = "windows"))]
#[arg(short, long, default_value = "false", help = "Daemonize the process")]
daemonize: bool,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
fn main() -> anyhow::Result<()> {
env_logger::init();

let args = Args::parse();

let mut listener = tarpc::serde_transport::tcp::listen(&(args.address, args.port), Json::default).await?;
// Setup runtime
let rt = runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap();

// Setup listener
let mut listener = rt.block_on(async {
return tarpc::serde_transport::tcp::listen(&(args.address, args.port), Json::default).await;
})?;
listener.config_mut().max_frame_length(usize::MAX);
listener
// Ignore accept errors.
.filter_map(|r| future::ready(r.ok()))
.map(server::BaseChannel::with_defaults)
// serve is generated by the service attribute. It takes as input any type implementing
// the generated World trait.
.map(|channel| {
let server = BhAgentServer::new(channel.transport().peer_addr().unwrap());
channel.execute(server.serve())
})
// Max 10 channels.
.buffer_unordered(10)
.for_each(|_| async {})
.await;

// Daemonize
#[cfg(not(target_os = "windows"))]
if args.daemonize {
daemonize::Daemonize::new()
.pid_file("/tmp/bh_agent_server.pid")
.start()
.unwrap();
}

// Run the listener
rt.block_on(async {
listener
// Ignore accept errors.
.filter_map(|r| future::ready(r.ok()))
.map(server::BaseChannel::with_defaults)
// serve is generated by the service attribute. It takes as input any type implementing
// the generated World trait.
.map(|channel| {
let server = BhAgentServer::new(channel.transport().peer_addr().unwrap());
channel.execute(server.serve())
})
// Max 10 channels.
.buffer_unordered(10)
.for_each(|_| async {})
.await;
});

Ok(())
}

0 comments on commit ffc4a43

Please sign in to comment.