-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathagent.rs
More file actions
69 lines (60 loc) · 2 KB
/
agent.rs
File metadata and controls
69 lines (60 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use iroh::{
protocol::{DynProtocolHandler, Router},
SecretKey,
};
use tokio::sync::mpsc::UnboundedReceiver;
use tracing::info;
use crate::{admin::cli::AgentArgs, protocols::enrollment::enrollment_agent};
pub async fn run(
secret_key: SecretKey,
endpoint: iroh::Endpoint,
agent_args: AgentArgs,
maybe_shutdown_rx: Option<UnboundedReceiver<()>>,
) -> anyhow::Result<serde_json::Value> {
let node_id = endpoint.id();
let bind_info = endpoint.bound_sockets();
info!("node_id: {node_id} listening on {bind_info:?}");
// Enable iroh-docs and its dependencies
let (blobs, blob_store, gossip, docs) =
crate::util::setup_iroh_docs_and_deps(&endpoint, &agent_args.persistence_args.mode())
.await?;
let router_builder = Router::builder(endpoint.clone())
.accept(iroh_gossip::ALPN, gossip.clone())
.accept(iroh_blobs::ALPN, blobs.clone())
.accept(iroh_docs::ALPN, docs.clone())
.accept(
enrollment_agent::ALPN,
enrollment_agent::EnrollmentAgentApi::spawn(
secret_key,
endpoint,
blobs.clone(),
docs.clone(),
agent_args,
)
.await?
.expose()?,
);
let router = router_builder.spawn();
tokio::select! {
_ = tokio::signal::ctrl_c() => {
tracing::info!("received CTRL+C signal.");
}
_ = async move {
if let Some(mut rx) = maybe_shutdown_rx {
rx.recv().await;
} else {
std::future::pending::<()>().await;
}
} => {
tracing::info!("received shutdown message");
}
}
tracing::info!("initiating shutdown...");
let _ = docs.shutdown().await;
let _ = gossip.shutdown().await;
let _ = blobs.shutdown().await;
let _ = blob_store.shutdown().await;
let _ = router.shutdown().await;
tracing::info!("shutdown complete, bye!");
Ok(Default::default())
}