From 345d59268dfea565985dea8027978d4267d981f5 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 31 Jul 2023 12:32:55 +0200 Subject: [PATCH] Implement `BoardOperation::store_or_500` --- todored/src/op.rs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/todored/src/op.rs b/todored/src/op.rs index 5a71842..6eb071b 100644 --- a/todored/src/op.rs +++ b/todored/src/op.rs @@ -27,22 +27,27 @@ impl ClientOperation { } impl BoardOperation { - fn store(&self, rconn: redis::aio::Connection, board: &str) -> Result<()> { + /// Store this in Redis. + pub(crate) async fn store_or_500(&self, rconn: &mut redis::aio::Connection, board: &str) -> Result { log::debug!("Storing BoardOperation in Redis: {:?}", &self); - log::trace!("Serializing BoardOperation to JSON..."); - let data = serde_json::ser::to_string(self) - .expect_or_500_and_log("Failed to serialize BoardOperation"); - log::trace!("Computing Redis key..."); let stream_key = format!("board:{{{board}}}:stream"); + log::trace!("Serializing BoardOperation to JSON..."); + let operation = serde_json::ser::to_string(self) + .expect_or_500_and_log("Failed to serialize BoardOperation")?; + log::trace!("Adding to the Redis stream {stream_key:?}..."); - let response = redis::cmd("XADD") + let id = redis::cmd("XADD") .arg(stream_key) - .arg() + .arg("*") + .arg("operation") + .arg(operation) + .query_async::(rconn).await + .expect_or_500_and_log("Failed to XADD to Redis")?; - - Ok(()) + log::trace!("Added to Redis stream with id {id:?}!"); + Ok(id) } }