(
key: String,
count: usize,
channel: mpsc::Sender<api::LogItem>,
)
| 229 | } |
| 230 | |
| 231 | pub async fn get_frame_logs( |
| 232 | key: String, |
| 233 | count: usize, |
| 234 | channel: mpsc::Sender<api::LogItem>, |
| 235 | ) -> Result<()> { |
| 236 | let mut last_id = "0".to_string(); |
| 237 | |
| 238 | loop { |
| 239 | if channel.is_closed() { |
| 240 | debug!("Channel has been closed, returning"); |
| 241 | return Ok(()); |
| 242 | } |
| 243 | |
| 244 | let srr: StreamReadReply = redis::cmd("XREAD") |
| 245 | .arg("COUNT") |
| 246 | .arg(count) |
| 247 | .arg("STREAMS") |
| 248 | .arg(&key) |
| 249 | .arg(&last_id) |
| 250 | .query_async(&mut get_async_redis_conn().await?) |
| 251 | .await |
| 252 | .context("XREAD frame stream")?; |
| 253 | |
| 254 | for stream_key in &srr.keys { |
| 255 | for stream_id in &stream_key.ids { |
| 256 | last_id.clone_from(&stream_id.id); |
| 257 | for (k, v) in &stream_id.map { |
| 258 | let res = handle_stream(&last_id, &channel, k, v).await; |
| 259 | |
| 260 | if let Err(e) = res { |
| 261 | // Return in case of channel error, in any other case we just log |
| 262 | // the error. |
| 263 | if e.downcast_ref::<mpsc::error::SendError<api::LogItem>>() |
| 264 | .is_some() |
| 265 | { |
| 266 | return Err(e); |
| 267 | } |
| 268 | |
| 269 | error!(key = %k, error = %e.full(), "Parsing frame-log error"); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | // If we use xread with block=0, the connection can't be used by other requests. Now we |
| 276 | // check every 1 second if there are new messages, which should be sufficient. |
| 277 | sleep(Duration::from_secs(1)).await; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | async fn handle_stream( |
| 282 | stream_id: &str, |
no test coverage detected