(&self)
| 158 | } |
| 159 | |
| 160 | async fn start(&self) -> Result<()> { |
| 161 | info!("Getting Redis connection"); |
| 162 | let mut redis_conn = self.redis_client.get_async_connection().await?; |
| 163 | |
| 164 | let key = format!("{}device:stream:event", self.key_prefix); |
| 165 | |
| 166 | // Try to create the consumer group. This will fail in case the consumer group already exists. |
| 167 | let _: usize = match redis::cmd("XGROUP") |
| 168 | .arg("CREATE") |
| 169 | .arg(&key) |
| 170 | .arg(&self.consumer_group) |
| 171 | .arg(0) |
| 172 | .arg("MKSTREAM") |
| 173 | .query_async(&mut redis_conn) |
| 174 | .await |
| 175 | { |
| 176 | Ok(v) => v, |
| 177 | Err(e) => { |
| 178 | warn!(error = %e, "Could not create Redis consumer group, ignore this error if the group already exists"); |
| 179 | 0 |
| 180 | } |
| 181 | }; |
| 182 | |
| 183 | loop { |
| 184 | let srr: redis::streams::StreamReadReply = redis::cmd("XREADGROUP") |
| 185 | .arg("GROUP") |
| 186 | .arg(&self.consumer_group) |
| 187 | .arg(&self.consumer_name) |
| 188 | .arg("COUNT") |
| 189 | .arg(10) |
| 190 | .arg("BLOCK") |
| 191 | .arg(1000) |
| 192 | .arg("STREAMS") |
| 193 | .arg(&key) |
| 194 | .arg(">") |
| 195 | .query_async(&mut redis_conn) |
| 196 | .await?; |
| 197 | |
| 198 | for stream_key in &srr.keys { |
| 199 | for stream_id in &stream_key.ids { |
| 200 | let _: () = redis::cmd("XACK") |
| 201 | .arg(&key) |
| 202 | .arg(&self.consumer_group) |
| 203 | .arg(&stream_id.id) |
| 204 | .query_async(&mut redis_conn) |
| 205 | .await?; |
| 206 | |
| 207 | for (k, v) in &stream_id.map { |
| 208 | let res = || -> Result<()> { |
| 209 | info!(key = %k, "Event received from Redis stream"); |
| 210 | match k.as_ref() { |
| 211 | "up" => { |
| 212 | if let redis::Value::BulkString(b) = v { |
| 213 | let pl = integration_pb::UplinkEvent::decode( |
| 214 | &mut Cursor::new(b), |
| 215 | )?; |
| 216 | tokio::spawn(uplink_event(pl)); |
| 217 | } |
no test coverage detected