feat: automatically discover inventory (#127)
All checks were successful
Run Check Script / check (pull_request) Successful in 1m15s
All checks were successful
Run Check Script / check (pull_request) Successful in 1m15s
## Fully automated inventory gathering now works! Boot up harmony_inventory_agent with `cargo run -p harmony_inventory_agent` Launch the DiscoverInventoryAgentScore , currently available this way : `RUST_LOG=info cargo run -p example-cli -- -f Discover -y` And you will have automatically all hosts saved to the database. Run `cargo sqlx setup` if you have not done it yet. Co-authored-by: Ian Letourneau <ian@noma.to> Reviewed-on: #127 Co-authored-by: Jean-Gabriel Gill-Couture <jg@nationtech.io> Co-committed-by: Jean-Gabriel Gill-Couture <jg@nationtech.io>
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
use log::{debug, error};
|
||||
use mdns_sd::{ServiceDaemon, ServiceEvent};
|
||||
|
||||
use crate::local_presence::SERVICE_NAME;
|
||||
|
||||
pub type DiscoveryEvent = ServiceEvent;
|
||||
|
||||
pub fn discover_agents(timeout: Option<u64>, on_event: impl Fn(DiscoveryEvent) + Send + 'static) {
|
||||
pub async fn discover_agents<F>(timeout: Option<u64>, on_event: F)
|
||||
where
|
||||
F: FnOnce(DiscoveryEvent) -> Result<(), String> + Send + 'static + Copy,
|
||||
{
|
||||
// Create a new mDNS daemon.
|
||||
let mdns = ServiceDaemon::new().expect("Failed to create mDNS daemon");
|
||||
|
||||
@@ -12,23 +16,24 @@ pub fn discover_agents(timeout: Option<u64>, on_event: impl Fn(DiscoveryEvent) +
|
||||
// The receiver will be a stream of events.
|
||||
let receiver = mdns.browse(SERVICE_NAME).expect("Failed to browse");
|
||||
|
||||
std::thread::spawn(move || {
|
||||
tokio::task::spawn_blocking(move || {
|
||||
while let Ok(event) = receiver.recv() {
|
||||
on_event(event.clone());
|
||||
if let Err(e) = on_event(event.clone()) {
|
||||
error!("Event callback failed : {e}");
|
||||
}
|
||||
match event {
|
||||
ServiceEvent::ServiceResolved(resolved) => {
|
||||
println!("Resolved a new service: {}", resolved.fullname);
|
||||
debug!("Resolved a new service: {}", resolved.fullname);
|
||||
}
|
||||
other_event => {
|
||||
println!("Received other event: {:?}", &other_event);
|
||||
debug!("Received other event: {:?}", &other_event);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if let Some(timeout) = timeout {
|
||||
// Gracefully shutdown the daemon.
|
||||
std::thread::sleep(std::time::Duration::from_secs(timeout));
|
||||
tokio::time::sleep(std::time::Duration::from_secs(timeout)).await;
|
||||
mdns.shutdown().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user