From ef298f59e539466d8ca53ef77ee73efa31626cc9 Mon Sep 17 00:00:00 2001 From: David Pacheco Date: Fri, 23 Feb 2024 11:07:34 -0800 Subject: [PATCH 1/2] trigger inventory collection after blueprint execution --- .../src/app/background/blueprint_execution.rs | 13 ++++- nexus/src/app/background/init.rs | 56 +++++++++++-------- 2 files changed, 44 insertions(+), 25 deletions(-) diff --git a/nexus/src/app/background/blueprint_execution.rs b/nexus/src/app/background/blueprint_execution.rs index 32797facbf..5ceff5d717 100644 --- a/nexus/src/app/background/blueprint_execution.rs +++ b/nexus/src/app/background/blueprint_execution.rs @@ -20,6 +20,8 @@ pub struct BlueprintExecutor { datastore: Arc, rx_blueprint: watch::Receiver>>, nexus_label: String, + tx: watch::Sender, + count: usize, } impl BlueprintExecutor { @@ -30,7 +32,12 @@ impl BlueprintExecutor { >, nexus_label: String, ) -> BlueprintExecutor { - BlueprintExecutor { datastore, rx_blueprint, nexus_label } + let (tx, _) = watch::channel(0); + BlueprintExecutor { datastore, rx_blueprint, nexus_label, tx, count: 0 } + } + + pub fn watcher(&self) -> watch::Receiver { + self.tx.subscribe() } } @@ -71,6 +78,10 @@ impl BackgroundTask for BlueprintExecutor { ) .await; + // Trigger anybody waiting for this to finish. + self.count = self.count + 1; + self.tx.send(self.count); + // Return the result as a `serde_json::Value` match result { Ok(()) => json!({}), diff --git a/nexus/src/app/background/init.rs b/nexus/src/app/background/init.rs index 846051a068..9ba30bab64 100644 --- a/nexus/src/app/background/init.rs +++ b/nexus/src/app/background/init.rs @@ -170,30 +170,6 @@ impl BackgroundTasks { ) }; - // Background task: inventory collector - let task_inventory_collection = { - let collector = inventory_collection::InventoryCollector::new( - datastore.clone(), - resolver, - &nexus_id.to_string(), - config.inventory.nkeep, - config.inventory.disable, - ); - let task = driver.register( - String::from("inventory_collection"), - String::from( - "collects hardware and software inventory data from the \ - whole system", - ), - config.inventory.period_secs, - Box::new(collector), - opctx.child(BTreeMap::new()), - vec![], - ); - - task - }; - // Background task: phantom disk detection let task_phantom_disks = { let detector = @@ -230,6 +206,7 @@ impl BackgroundTasks { rx_blueprint.clone(), nexus_id.to_string(), ); + let rx_blueprint_exec = blueprint_executor.watcher(); let task_blueprint_executor = driver.register( String::from("blueprint_executor"), String::from("Executes the target blueprint"), @@ -239,6 +216,37 @@ impl BackgroundTasks { vec![Box::new(rx_blueprint)], ); + // Background task: inventory collector + // + // This currently depends on the "output" of the blueprint executor in + // order to automatically trigger inventory collection whenever the + // blueprint executor runs. In the limit, this could become a problem + // because the blueprint executor might also depend indirectly on the + // inventory collector. In that case, we may need to do something more + // complicated. But for now, this works. + let task_inventory_collection = { + let collector = inventory_collection::InventoryCollector::new( + datastore.clone(), + resolver, + &nexus_id.to_string(), + config.inventory.nkeep, + config.inventory.disable, + ); + let task = driver.register( + String::from("inventory_collection"), + String::from( + "collects hardware and software inventory data from the \ + whole system", + ), + config.inventory.period_secs, + Box::new(collector), + opctx.child(BTreeMap::new()), + vec![Box::new(rx_blueprint_exec)], + ); + + task + }; + let task_service_zone_nat_tracker = { driver.register( "service_zone_nat_tracker".to_string(), From 70dfed3dff440d3f0c168c492a064216b4eccbf9 Mon Sep 17 00:00:00 2001 From: David Pacheco Date: Fri, 23 Feb 2024 11:36:58 -0800 Subject: [PATCH 2/2] fix --- nexus/src/app/background/blueprint_execution.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/nexus/src/app/background/blueprint_execution.rs b/nexus/src/app/background/blueprint_execution.rs index 5ceff5d717..2ad82e8c68 100644 --- a/nexus/src/app/background/blueprint_execution.rs +++ b/nexus/src/app/background/blueprint_execution.rs @@ -21,7 +21,6 @@ pub struct BlueprintExecutor { rx_blueprint: watch::Receiver>>, nexus_label: String, tx: watch::Sender, - count: usize, } impl BlueprintExecutor { @@ -33,7 +32,7 @@ impl BlueprintExecutor { nexus_label: String, ) -> BlueprintExecutor { let (tx, _) = watch::channel(0); - BlueprintExecutor { datastore, rx_blueprint, nexus_label, tx, count: 0 } + BlueprintExecutor { datastore, rx_blueprint, nexus_label, tx } } pub fn watcher(&self) -> watch::Receiver { @@ -79,8 +78,7 @@ impl BackgroundTask for BlueprintExecutor { .await; // Trigger anybody waiting for this to finish. - self.count = self.count + 1; - self.tx.send(self.count); + self.tx.send_modify(|count| *count = *count + 1); // Return the result as a `serde_json::Value` match result {