Skip to content

Commit

Permalink
taskman up mobile config
Browse files Browse the repository at this point in the history
  • Loading branch information
andymck committed Aug 23, 2023
1 parent 3a3552a commit 0b953ce
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 24 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions mobile_config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ retainer = {workspace = true}
thiserror = {workspace = true}
tokio = {workspace = true}
tokio-stream = {workspace = true}
tokio-util = { workspace = true }
tonic = {workspace = true}
tracing = {workspace = true}
tracing-subscriber = {workspace = true}
triggered = {workspace = true}
task-manager = { path = "../task_manager" }
64 changes: 40 additions & 24 deletions mobile_config/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::{Error, Result};
use clap::Parser;
use futures::future::LocalBoxFuture;
use futures_util::TryFutureExt;
use helium_proto::services::mobile_config::{
AdminServer, AuthorizationServer, EntityServer, GatewayServer,
Expand All @@ -9,8 +10,8 @@ use mobile_config::{
entity_service::EntityService, gateway_service::GatewayService, key_cache::KeyCache,
settings::Settings,
};
use std::{path::PathBuf, time::Duration};
use tokio::signal;
use std::{net::SocketAddr, path::PathBuf, time::Duration};
use task_manager::{ManagedTask, TaskManager};
use tonic::transport;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

Expand Down Expand Up @@ -58,16 +59,6 @@ impl Daemon {
.with(tracing_subscriber::fmt::layer())
.init();

// Configure shutdown trigger
let (shutdown_trigger, shutdown_listener) = triggered::trigger();
let mut sigterm = signal::unix::signal(signal::unix::SignalKind::terminate())?;
tokio::spawn(async move {
tokio::select! {
_ = sigterm.recv() => shutdown_trigger.trigger(),
_ = signal::ctrl_c() => shutdown_trigger.trigger(),
}
});

// Install prometheus metrics exporter
poc_metrics::start_metrics(&settings.metrics)?;

Expand Down Expand Up @@ -96,18 +87,43 @@ impl Daemon {
settings.signing_keypair()?,
);

transport::Server::builder()
.http2_keepalive_interval(Some(Duration::from_secs(250)))
.http2_keepalive_timeout(Some(Duration::from_secs(60)))
.add_service(AdminServer::new(admin_svc))
.add_service(GatewayServer::new(gateway_svc))
.add_service(AuthorizationServer::new(auth_svc))
.add_service(EntityServer::new(entity_svc))
.serve_with_shutdown(listen_addr, shutdown_listener)
.map_err(Error::from)
.await?;

Ok(())
let grpc_server = GrpcServer {
listen_addr,
admin_svc,
gateway_svc,
auth_svc,
entity_svc,
};

TaskManager::builder().add_task(grpc_server).start().await
}
}

pub struct GrpcServer {
listen_addr: SocketAddr,
admin_svc: AdminService,
gateway_svc: GatewayService,
auth_svc: AuthorizationService,
entity_svc: EntityService,
}

impl ManagedTask for GrpcServer {
fn start_task(
self: Box<Self>,
shutdown: triggered::Listener,
) -> LocalBoxFuture<'static, anyhow::Result<()>> {
Box::pin(async move {
transport::Server::builder()
.http2_keepalive_interval(Some(Duration::from_secs(250)))
.http2_keepalive_timeout(Some(Duration::from_secs(60)))
.add_service(AdminServer::new(self.admin_svc))
.add_service(GatewayServer::new(self.gateway_svc))
.add_service(AuthorizationServer::new(self.auth_svc))
.add_service(EntityServer::new(self.entity_svc))
.serve_with_shutdown(self.listen_addr, shutdown)
.map_err(Error::from)
.await
})
}
}

Expand Down

0 comments on commit 0b953ce

Please sign in to comment.