(
key: String,
count: usize,
channel: mpsc::Sender<api::LogItem>,
)
| 58 | } |
| 59 | |
| 60 | pub async fn get_event_logs( |
| 61 | key: String, |
| 62 | count: usize, |
| 63 | channel: mpsc::Sender<api::LogItem>, |
| 64 | ) -> Result<()> { |
| 65 | let mut last_id = "0".to_string(); |
| 66 | |
| 67 | loop { |
| 68 | if channel.is_closed() { |
| 69 | debug!("Channel has been closed, returning"); |
| 70 | return Ok(()); |
| 71 | } |
| 72 | |
| 73 | let srr: StreamReadReply = redis::cmd("XREAD") |
| 74 | .arg("COUNT") |
| 75 | .arg(count) |
| 76 | .arg("STREAMS") |
| 77 | .arg(&key) |
| 78 | .arg(&last_id) |
| 79 | .query_async(&mut get_async_redis_conn().await?) |
| 80 | .await |
| 81 | .context("XREAD event stream")?; |
| 82 | |
| 83 | for stream_key in &srr.keys { |
| 84 | for stream_id in &stream_key.ids { |
| 85 | last_id.clone_from(&stream_id.id); |
| 86 | for (k, v) in &stream_id.map { |
| 87 | let res = handle_stream(&last_id, &channel, k, v).await; |
| 88 | |
| 89 | if let Err(e) = res { |
| 90 | // Return in case of channel error, in any other case we just log |
| 91 | // the error. |
| 92 | if e.downcast_ref::<mpsc::error::SendError<api::LogItem>>() |
| 93 | .is_some() |
| 94 | { |
| 95 | return Err(e); |
| 96 | } |
| 97 | |
| 98 | error!(key = %k, error = %e.full(), "Parsing frame-log error"); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // If we use xread with block=0, the connection can't be used by other requests. Now we |
| 105 | // check every 1 second if there are new messages, which should be sufficient. |
| 106 | sleep(Duration::from_secs(1)).await; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | async fn handle_stream( |
| 111 | stream_id: &str, |
no test coverage detected