|
| 1 | +""" |
| 2 | +pgSTAC source plugin. |
| 3 | +
|
| 4 | +Connects to pgSTAC and listens for item change notifications using |
| 5 | +LISTEN/NOTIFY. |
| 6 | +""" |
| 7 | + |
| 8 | +import asyncio |
| 9 | +import json |
| 10 | +from collections.abc import AsyncIterator |
| 11 | +from datetime import UTC, datetime |
| 12 | +from typing import Any |
| 13 | + |
| 14 | +import asyncpg |
| 15 | +from pydantic import ConfigDict, field_validator |
| 16 | + |
| 17 | +from ..core.event import NotificationEvent |
| 18 | +from ..core.plugin import BasePluginConfig, BaseSource, PluginMetadata |
| 19 | + |
| 20 | + |
| 21 | +class PgSTACSourceConfig(BasePluginConfig): |
| 22 | + """Configuration for pgSTAC notification source.""" |
| 23 | + |
| 24 | + model_config = ConfigDict(extra="forbid") |
| 25 | + |
| 26 | + # Connection parameters |
| 27 | + host: str = "localhost" |
| 28 | + port: int = 5432 |
| 29 | + database: str = "pgstac" |
| 30 | + user: str = "postgres" |
| 31 | + password: str = "" |
| 32 | + |
| 33 | + # LISTEN/NOTIFY settings |
| 34 | + channel: str = "pgstac_items" |
| 35 | + queue_size: int = 1000 |
| 36 | + listen_timeout: float = 1.0 |
| 37 | + |
| 38 | + # Reconnection settings |
| 39 | + max_reconnect_attempts: int = -1 # -1 for infinite |
| 40 | + reconnect_delay: float = 5.0 |
| 41 | + reconnect_backoff_factor: float = 2.0 |
| 42 | + max_reconnect_delay: float = 60.0 |
| 43 | + |
| 44 | + # Event configuration |
| 45 | + event_source: str = "/eoapi/stac/pgstac" |
| 46 | + event_type: str = "org.eoapi.stac.item" |
| 47 | + |
| 48 | + @field_validator("port") |
| 49 | + @classmethod |
| 50 | + def validate_port(cls, v: int) -> int: |
| 51 | + if not (1 <= v <= 65535): |
| 52 | + raise ValueError("Port must be between 1 and 65535") |
| 53 | + return v |
| 54 | + |
| 55 | + @classmethod |
| 56 | + def get_sample_config(cls) -> dict[str, Any]: |
| 57 | + """Get sample configuration for this source.""" |
| 58 | + return { |
| 59 | + "host": "localhost", |
| 60 | + "port": 5432, |
| 61 | + "database": "pgstac", |
| 62 | + "user": "postgres", |
| 63 | + "password": "your-password", |
| 64 | + "channel": "pgstac_items", |
| 65 | + "max_reconnect_attempts": -1, |
| 66 | + "reconnect_delay": 5.0, |
| 67 | + } |
| 68 | + |
| 69 | + @classmethod |
| 70 | + def get_metadata(cls) -> PluginMetadata: |
| 71 | + """Get structured metadata for this source type.""" |
| 72 | + return PluginMetadata( |
| 73 | + name="pgstac", |
| 74 | + description="Resilient pgSTAC LISTEN/NOTIFY source with auto-reconnection", |
| 75 | + version="2.0.0", |
| 76 | + category="database", |
| 77 | + tags=["postgresql", "pgstac", "stac", "listen-notify", "resilient"], |
| 78 | + priority=10, |
| 79 | + ) |
| 80 | + |
| 81 | + def get_connection_info(self) -> str: |
| 82 | + """Get connection info string for display.""" |
| 83 | + return f"postgresql://{self.user}@{self.host}:{self.port}/{self.database}" |
| 84 | + |
| 85 | + def get_status_info(self) -> dict[str, Any]: |
| 86 | + """Get status information for display.""" |
| 87 | + return { |
| 88 | + "Host": f"{self.host}:{self.port}", |
| 89 | + "Database": self.database, |
| 90 | + "Channel": self.channel, |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | +class PgSTACSource(BaseSource): |
| 95 | + """ |
| 96 | + Resilient pgSTAC source for notifications with auto-reconnection. |
| 97 | + """ |
| 98 | + |
| 99 | + def __init__(self, config: PgSTACSourceConfig): |
| 100 | + """Initialize pgSTAC source.""" |
| 101 | + super().__init__(config) |
| 102 | + self._connection: asyncpg.Connection | None = None |
| 103 | + self._notification_queue: asyncio.Queue | None = None |
| 104 | + self._reconnect_attempts: int = 0 |
| 105 | + self._current_delay: float = config.reconnect_delay |
| 106 | + self._connected: bool = False |
| 107 | + |
| 108 | + async def start(self) -> None: |
| 109 | + """Start pgSTAC connection and setup listener.""" |
| 110 | + self.logger.info(f"Starting pgSTAC source: {self.config.get_connection_info()}") |
| 111 | + |
| 112 | + self._notification_queue = asyncio.Queue(maxsize=self.config.queue_size) |
| 113 | + await self._connect() |
| 114 | + await super().start() |
| 115 | + |
| 116 | + async def stop(self) -> None: |
| 117 | + """Stop pgSTAC connection and cleanup.""" |
| 118 | + await self._disconnect() |
| 119 | + self._notification_queue = None |
| 120 | + await super().stop() |
| 121 | + self.logger.info("pgSTAC source stopped") |
| 122 | + |
| 123 | + async def listen(self) -> AsyncIterator[NotificationEvent]: |
| 124 | + """Listen for pgSTAC notifications with automatic reconnection.""" |
| 125 | + if not self._notification_queue: |
| 126 | + raise RuntimeError("pgSTAC source not started") |
| 127 | + |
| 128 | + while self._running: |
| 129 | + try: |
| 130 | + # Ensure we're connected |
| 131 | + if not self._connected: |
| 132 | + await self._reconnect_if_needed() |
| 133 | + if not self._connected: |
| 134 | + await asyncio.sleep(1.0) |
| 135 | + continue |
| 136 | + |
| 137 | + # Wait for notification |
| 138 | + payload = await asyncio.wait_for( |
| 139 | + self._notification_queue.get(), |
| 140 | + timeout=self.config.listen_timeout, |
| 141 | + ) |
| 142 | + |
| 143 | + if payload: |
| 144 | + event = self._process_notification_payload(payload) |
| 145 | + if event: |
| 146 | + yield event |
| 147 | + |
| 148 | + except TimeoutError: |
| 149 | + continue |
| 150 | + except Exception as e: |
| 151 | + self.logger.error(f"Error in listen loop: {e}") |
| 152 | + self._connected = False |
| 153 | + await asyncio.sleep(1.0) |
| 154 | + |
| 155 | + async def _connect(self) -> bool: |
| 156 | + """Establish connection to PostgreSQL.""" |
| 157 | + try: |
| 158 | + self.logger.info("Connecting to PostgreSQL...") |
| 159 | + |
| 160 | + self._connection = await asyncpg.connect( |
| 161 | + host=self.config.host, |
| 162 | + port=self.config.port, |
| 163 | + database=self.config.database, |
| 164 | + user=self.config.user, |
| 165 | + password=self.config.password, |
| 166 | + ) |
| 167 | + |
| 168 | + # Setup listener |
| 169 | + if self._connection is None: |
| 170 | + raise RuntimeError("Connection is None after successful connect") |
| 171 | + await self._connection.execute(f"LISTEN {self.config.channel}") |
| 172 | + await self._connection.add_listener( |
| 173 | + self.config.channel, self._notification_callback |
| 174 | + ) |
| 175 | + |
| 176 | + self._connected = True |
| 177 | + self._reconnect_attempts = 0 |
| 178 | + self._current_delay = self.config.reconnect_delay |
| 179 | + |
| 180 | + self.logger.info( |
| 181 | + f"Connected and listening to channel: {self.config.channel}" |
| 182 | + ) |
| 183 | + return True |
| 184 | + |
| 185 | + except Exception as e: |
| 186 | + self.logger.error(f"Failed to connect: {e}") |
| 187 | + self._connected = False |
| 188 | + await self._disconnect() |
| 189 | + return False |
| 190 | + |
| 191 | + async def _disconnect(self) -> None: |
| 192 | + """Close PostgreSQL connection safely.""" |
| 193 | + self._connected = False |
| 194 | + if self._connection: |
| 195 | + try: |
| 196 | + await self._connection.remove_listener( |
| 197 | + self.config.channel, self._notification_callback |
| 198 | + ) |
| 199 | + await self._connection.execute(f"UNLISTEN {self.config.channel}") |
| 200 | + await self._connection.close() |
| 201 | + except Exception as e: |
| 202 | + self.logger.warning(f"Error during disconnect: {e}") |
| 203 | + finally: |
| 204 | + self._connection = None |
| 205 | + |
| 206 | + async def _reconnect_if_needed(self) -> None: |
| 207 | + """Attempt reconnection with exponential backoff.""" |
| 208 | + if not self._should_reconnect(): |
| 209 | + return |
| 210 | + |
| 211 | + self.logger.info( |
| 212 | + f"Reconnecting in {self._current_delay:.1f}s " |
| 213 | + f"(attempt {self._reconnect_attempts + 1})" |
| 214 | + ) |
| 215 | + await asyncio.sleep(self._current_delay) |
| 216 | + |
| 217 | + self._reconnect_attempts += 1 |
| 218 | + |
| 219 | + if await self._connect(): |
| 220 | + self.logger.info("Reconnection successful") |
| 221 | + else: |
| 222 | + # Exponential backoff |
| 223 | + self._current_delay = min( |
| 224 | + self._current_delay * self.config.reconnect_backoff_factor, |
| 225 | + self.config.max_reconnect_delay, |
| 226 | + ) |
| 227 | + |
| 228 | + def _should_reconnect(self) -> bool: |
| 229 | + """Check if we should attempt to reconnect.""" |
| 230 | + if not self._running: |
| 231 | + return False |
| 232 | + |
| 233 | + if self.config.max_reconnect_attempts == -1: |
| 234 | + return True |
| 235 | + |
| 236 | + return bool(self._reconnect_attempts < self.config.max_reconnect_attempts) |
| 237 | + |
| 238 | + def _notification_callback( |
| 239 | + self, connection: Any, pid: int, channel: str, payload: str |
| 240 | + ) -> None: |
| 241 | + """Callback for asyncpg notifications.""" |
| 242 | + if self._notification_queue: |
| 243 | + try: |
| 244 | + self._notification_queue.put_nowait(payload) |
| 245 | + except asyncio.QueueFull: |
| 246 | + self.logger.warning("Notification queue full, dropping notification") |
| 247 | + |
| 248 | + def _from_pgstac_notification(self, payload: dict[str, Any]) -> NotificationEvent: |
| 249 | + """Create NotificationEvent from pgSTAC LISTEN/NOTIFY payload.""" |
| 250 | + operation = payload.get("operation") or payload.get("event", "INSERT") |
| 251 | + collection = payload.get("collection", "unknown") |
| 252 | + item_id = payload.get("item_id") or payload.get("id") |
| 253 | + |
| 254 | + # Handle timestamp parsing |
| 255 | + timestamp = payload.get("timestamp") or payload.get("datetime") |
| 256 | + if timestamp and isinstance(timestamp, str): |
| 257 | + try: |
| 258 | + timestamp = datetime.fromisoformat(timestamp.replace("Z", "+00:00")) |
| 259 | + except ValueError: |
| 260 | + timestamp = datetime.now(UTC) |
| 261 | + elif not timestamp: |
| 262 | + timestamp = datetime.now(UTC) |
| 263 | + |
| 264 | + return NotificationEvent( |
| 265 | + source=self.config.event_source, |
| 266 | + type=self.config.event_type, |
| 267 | + operation=operation, |
| 268 | + collection=collection, |
| 269 | + item_id=item_id, |
| 270 | + timestamp=timestamp, |
| 271 | + data=payload, |
| 272 | + ) |
| 273 | + |
| 274 | + def _process_notification_payload(self, payload: str) -> NotificationEvent | None: |
| 275 | + """Process a pgSTAC notification payload into a NotificationEvent.""" |
| 276 | + try: |
| 277 | + payload_data = json.loads(payload) |
| 278 | + event = self._from_pgstac_notification(payload_data) |
| 279 | + self.logger.debug( |
| 280 | + f"Processed: {event.operation} on {event.collection}/{event.item_id}" |
| 281 | + ) |
| 282 | + return event |
| 283 | + except json.JSONDecodeError as e: |
| 284 | + self.logger.error(f"Failed to parse notification payload: {e}") |
| 285 | + return None |
| 286 | + except Exception as e: |
| 287 | + self.logger.error(f"Error processing notification: {e}") |
| 288 | + return None |
0 commit comments