Send a worker management message to the dirty arbiter. Args: operation: "add" or "remove" count: Number of workers to add/remove Returns: Dictionary with result or error
(self, operation: str, count: int)
| 348 | return self._send_manage_message("remove", count) |
| 349 | |
| 350 | def _send_manage_message(self, operation: str, count: int) -> dict: |
| 351 | """ |
| 352 | Send a worker management message to the dirty arbiter. |
| 353 | |
| 354 | Args: |
| 355 | operation: "add" or "remove" |
| 356 | count: Number of workers to add/remove |
| 357 | |
| 358 | Returns: |
| 359 | Dictionary with result or error |
| 360 | """ |
| 361 | # Get socket path from arbiter object or environment |
| 362 | dirty_socket_path = None |
| 363 | if hasattr(self.arbiter, 'dirty_arbiter') and self.arbiter.dirty_arbiter: |
| 364 | dirty_socket_path = getattr( |
| 365 | self.arbiter.dirty_arbiter, 'socket_path', None |
| 366 | ) |
| 367 | if not dirty_socket_path: |
| 368 | dirty_socket_path = os.environ.get('GUNICORN_DIRTY_SOCKET') |
| 369 | if not dirty_socket_path: |
| 370 | return { |
| 371 | "success": False, |
| 372 | "error": "Cannot find dirty arbiter socket path", |
| 373 | } |
| 374 | |
| 375 | try: |
| 376 | from gunicorn.dirty.protocol import ( |
| 377 | DirtyProtocol, MANAGE_OP_ADD, MANAGE_OP_REMOVE |
| 378 | ) |
| 379 | |
| 380 | op = MANAGE_OP_ADD if operation == "add" else MANAGE_OP_REMOVE |
| 381 | |
| 382 | sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 383 | sock.settimeout(10.0) |
| 384 | sock.connect(dirty_socket_path) |
| 385 | |
| 386 | # Send manage request |
| 387 | request = { |
| 388 | "type": DirtyProtocol.MSG_TYPE_MANAGE, |
| 389 | "id": 1, |
| 390 | "op": op, |
| 391 | "count": count, |
| 392 | } |
| 393 | DirtyProtocol.write_message(sock, request) |
| 394 | |
| 395 | # Read response |
| 396 | response = DirtyProtocol.read_message(sock) |
| 397 | sock.close() |
| 398 | |
| 399 | if response.get("type") == DirtyProtocol.MSG_TYPE_RESPONSE: |
| 400 | return response.get("result", {"success": True}) |
| 401 | elif response.get("type") == DirtyProtocol.MSG_TYPE_ERROR: |
| 402 | error = response.get("error", {}) |
| 403 | return { |
| 404 | "success": False, |
| 405 | "error": error.get("message", str(error)), |
| 406 | } |
| 407 | else: |
no test coverage detected