Initialize the dirty arbiter. Args: cfg: Gunicorn config log: Logger socket_path: Path to the arbiter's Unix socket pidfile: Well-known PID file location for orphan detection
(self, cfg, log, socket_path=None, pidfile=None)
| 64 | WORKER_BOOT_ERROR = 3 |
| 65 | |
| 66 | def __init__(self, cfg, log, socket_path=None, pidfile=None): |
| 67 | """ |
| 68 | Initialize the dirty arbiter. |
| 69 | |
| 70 | Args: |
| 71 | cfg: Gunicorn config |
| 72 | log: Logger |
| 73 | socket_path: Path to the arbiter's Unix socket |
| 74 | pidfile: Well-known PID file location for orphan detection |
| 75 | """ |
| 76 | self.cfg = cfg |
| 77 | self.log = log |
| 78 | self.pid = None |
| 79 | self.ppid = os.getpid() |
| 80 | self.pidfile = pidfile # Well-known location for orphan detection |
| 81 | |
| 82 | # Use a temp directory for sockets |
| 83 | self.tmpdir = tempfile.mkdtemp(prefix="gunicorn-dirty-") |
| 84 | self.socket_path = socket_path or os.path.join( |
| 85 | self.tmpdir, "arbiter.sock" |
| 86 | ) |
| 87 | |
| 88 | self.workers = {} # pid -> DirtyWorker |
| 89 | self.worker_sockets = {} # pid -> socket_path |
| 90 | self.worker_connections = {} # pid -> (reader, writer) |
| 91 | self.worker_queues = {} # pid -> asyncio.Queue |
| 92 | self.worker_consumers = {} # pid -> asyncio.Task |
| 93 | self._worker_rr_index = 0 # Round-robin index for worker selection |
| 94 | self.worker_age = 0 |
| 95 | self.alive = True |
| 96 | self.num_workers = self.cfg.dirty_workers # Dynamic count for TTIN/TTOU |
| 97 | |
| 98 | self._server = None |
| 99 | self._loop = None |
| 100 | self._pending_requests = {} # request_id -> Future |
| 101 | |
| 102 | # Per-app worker allocation tracking |
| 103 | # Maps import_path -> {import_path, worker_count, original_spec} |
| 104 | self.app_specs = {} |
| 105 | # Maps import_path -> set of worker PIDs that have loaded the app |
| 106 | self.app_worker_map = {} |
| 107 | # Maps worker_pid -> list of import_paths loaded by this worker |
| 108 | self.worker_app_map = {} |
| 109 | # Per-app round-robin indices for routing |
| 110 | self._app_rr_indices = {} |
| 111 | # Queue of app lists from dead workers to respawn with same apps |
| 112 | self._pending_respawns = [] |
| 113 | |
| 114 | # Stash (shared state) - global tables stored in arbiter |
| 115 | # Maps table_name -> dict of data |
| 116 | self.stash_tables = {} |
| 117 | |
| 118 | # Parse app specs on init |
| 119 | self._parse_app_specs() |
| 120 | |
| 121 | def _parse_app_specs(self): |
| 122 | """ |
nothing calls this directly
no test coverage detected