Update channel state with proper history tracking and logging
(self, channel_id, new_state, additional_fields=None)
| 2231 | logger.debug(f"Refreshed metadata TTL for channel {channel_id}") |
| 2232 | |
| 2233 | def update_channel_state(self, channel_id, new_state, additional_fields=None): |
| 2234 | """Update channel state with proper history tracking and logging""" |
| 2235 | if not self.redis_client: |
| 2236 | return False |
| 2237 | |
| 2238 | try: |
| 2239 | metadata_key = RedisKeys.channel_metadata(channel_id) |
| 2240 | |
| 2241 | # Get current state for logging |
| 2242 | current_state = None |
| 2243 | metadata = self.redis_client.hgetall(metadata_key) |
| 2244 | if metadata and 'state' in metadata: |
| 2245 | current_state = metadata['state'] |
| 2246 | |
| 2247 | # Only update if state is actually changing |
| 2248 | if current_state == new_state: |
| 2249 | logger.debug(f"Channel {channel_id} state unchanged: {current_state}") |
| 2250 | return True |
| 2251 | |
| 2252 | # Prepare update data |
| 2253 | update_data = { |
| 2254 | "state": new_state, |
| 2255 | "state_changed_at": str(time.time()) |
| 2256 | } |
| 2257 | |
| 2258 | # Add optional additional fields |
| 2259 | if additional_fields: |
| 2260 | update_data.update(additional_fields) |
| 2261 | |
| 2262 | # Update the metadata |
| 2263 | self.redis_client.hset(metadata_key, mapping=update_data) |
| 2264 | |
| 2265 | # Log the transition |
| 2266 | logger.info(f"Channel {channel_id} state transition: {current_state or 'None'} -> {new_state}") |
| 2267 | return True |
| 2268 | except Exception as e: |
| 2269 | logger.error(f"Error updating channel state: {e}") |
| 2270 | return False |
| 2271 | |
| 2272 | def _cleanup_local_resources(self, channel_id): |
| 2273 | """Clean up local resources for a channel without affecting Redis keys""" |
no test coverage detected