Command handlers with access to arbiter state. All handler methods return dictionaries that will be sent as the response data.
| 15 | |
| 16 | |
| 17 | class CommandHandlers: |
| 18 | """ |
| 19 | Command handlers with access to arbiter state. |
| 20 | |
| 21 | All handler methods return dictionaries that will be sent |
| 22 | as the response data. |
| 23 | """ |
| 24 | |
| 25 | def __init__(self, arbiter): |
| 26 | """ |
| 27 | Initialize handlers with arbiter reference. |
| 28 | |
| 29 | Args: |
| 30 | arbiter: The Gunicorn arbiter instance |
| 31 | """ |
| 32 | self.arbiter = arbiter |
| 33 | |
| 34 | def show_workers(self) -> dict: |
| 35 | """ |
| 36 | Return list of HTTP workers. |
| 37 | |
| 38 | Returns: |
| 39 | Dictionary with workers list containing: |
| 40 | - pid: Worker process ID |
| 41 | - age: Worker age (spawn order) |
| 42 | - requests: Number of requests handled (if available) |
| 43 | - booted: Whether worker has finished booting |
| 44 | - last_heartbeat: Seconds since last heartbeat |
| 45 | """ |
| 46 | workers = [] |
| 47 | now = time.monotonic() |
| 48 | |
| 49 | for pid, worker in self.arbiter.WORKERS.items(): |
| 50 | try: |
| 51 | last_update = worker.tmp.last_update() |
| 52 | last_heartbeat = round(now - last_update, 2) |
| 53 | except (OSError, ValueError): |
| 54 | last_heartbeat = None |
| 55 | |
| 56 | workers.append({ |
| 57 | "pid": pid, |
| 58 | "age": worker.age, |
| 59 | "booted": worker.booted, |
| 60 | "aborted": worker.aborted, |
| 61 | "last_heartbeat": last_heartbeat, |
| 62 | }) |
| 63 | |
| 64 | # Sort by age (oldest first) |
| 65 | workers.sort(key=lambda w: w["age"]) |
| 66 | |
| 67 | return {"workers": workers, "count": len(workers)} |
| 68 | |
| 69 | def show_dirty(self) -> dict: |
| 70 | """ |
| 71 | Return dirty workers and apps information. |
| 72 | |
| 73 | Returns: |
| 74 | Dictionary with: |
no outgoing calls