Coverage for /home/airflow/.local/lib/python3.12/site-packages/airflow/api_fastapi/gunicorn_config.py: 0%
17 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 14:22 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 14:22 +0000
1# Licensed to the Apache Software Foundation (ASF) under one
2# or more contributor license agreements. See the NOTICE file
3# distributed with this work for additional information
4# regarding copyright ownership. The ASF licenses this file
5# to you under the Apache License, Version 2.0 (the
6# "License"); you may not use this file except in compliance
7# with the License. You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing,
12# software distributed under the License is distributed on an
13# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14# KIND, either express or implied. See the License for the
15# specific language governing permissions and limitations
16# under the License.
17"""
18Gunicorn configuration hooks for the Airflow API server.
20This module provides Gunicorn server hooks that are loaded via the -c config option.
21These hooks handle:
22- Setting process titles to indicate worker readiness (useful for debugging)
23- Cleaning up ORM connections on worker exit
25Usage:
26 gunicorn -c python:airflow.api_fastapi.gunicorn_config airflow.api_fastapi.main:app
27"""
29from __future__ import annotations
31import logging
32import sys
34from airflow import settings
36log = logging.getLogger(__name__)
38# On macOS, setproctitle has issues, so we skip it
39if sys.platform == "darwin":
41 def setproctitle(title):
42 log.debug("macOS detected, skipping setproctitle")
43else:
44 from setproctitle import setproctitle
47def post_worker_init(worker):
48 """
49 Execute after a worker has been initialized.
51 This hook runs in each worker process after the ASGI app has been loaded.
52 We set the process title with a ready prefix for debugging visibility.
53 """
54 ready_prefix = settings.GUNICORN_WORKER_READY_PREFIX
55 proc_title = f"{ready_prefix}airflow api-server worker"
56 setproctitle(proc_title)
57 log.info("Worker initialized and ready, set process title: %s", proc_title)
60def worker_exit(server, worker):
61 """
62 Execute when a worker is about to exit.
64 This hook runs in the worker process itself (not the arbiter) just before exit.
65 We clean up ORM connections to ensure proper resource cleanup.
66 """
67 log.debug("Worker %s exited, disposing ORM connections", worker.pid)
68 settings.dispose_orm(do_log=False)