From b0ff595f60f4d1596599edfc11a8bf9781c89520 Mon Sep 17 00:00:00 2001 From: ManyTheFish Date: Wed, 2 Aug 2023 18:36:36 +0200 Subject: [PATCH] Event Listener: delete local key if deleted on ZK --- meilisearch-auth/src/lib.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/meilisearch-auth/src/lib.rs b/meilisearch-auth/src/lib.rs index 147b1bf5d..bfc39940c 100644 --- a/meilisearch-auth/src/lib.rs +++ b/meilisearch-auth/src/lib.rs @@ -48,6 +48,27 @@ impl AuthController { } } // TODO: If `auth` node already exist, we should synchronize the local data with zk. + + // Zookeeper Event listener loop + let controller_clone = controller.clone(); + let mut watcher = zk.watch("/auth", zk::AddWatchMode::PersistentRecursive).await?; + tokio::spawn(async move { + loop { + let zk::WatchedEvent { event_type, session_state, path } = + dbg!(watcher.changed().await); + + match event_type { + // a key is deleted from zk + zk::EventType::NodeDeleted => { + // TODO: ugly unwraps + let key = path.strip_prefix("/auth/").unwrap(); + let uuid = Uuid::parse_str(&key).unwrap(); + dbg!(controller_clone.store.delete_api_key(uuid).unwrap()); + } + _ => println!("Not yet implemented"), + } + } + }); } None => { if controller.store.is_empty()? {