55 lines
1.8 KiB
Python
55 lines
1.8 KiB
Python
from dynaconf import Dynaconf
|
|
from fastapi import FastAPI
|
|
from kn_utils.logging import logger
|
|
|
|
from pyinfra.config.loader import get_pyinfra_validators, validate_settings
|
|
from pyinfra.queue.callback import Callback
|
|
from pyinfra.queue.manager import QueueManager
|
|
from pyinfra.utils.opentelemetry import instrument_pika, setup_trace, instrument_app
|
|
from pyinfra.webserver.prometheus import (
|
|
add_prometheus_endpoint,
|
|
make_prometheus_processing_time_decorator_from_settings,
|
|
)
|
|
from pyinfra.webserver.utils import (
|
|
add_health_check_endpoint,
|
|
create_webserver_thread_from_settings,
|
|
)
|
|
|
|
|
|
def start_standard_queue_consumer(
|
|
callback: Callback,
|
|
settings: Dynaconf,
|
|
app: FastAPI = None,
|
|
):
|
|
"""Default serving logic for research services.
|
|
|
|
Supplies /health, /ready and /prometheus endpoints (if enabled). The callback is monitored for processing time per
|
|
message. Also traces the queue messages via openTelemetry (if enabled).
|
|
Workload is received via queue messages and processed by the callback function (see pyinfra.queue.callback for
|
|
callbacks).
|
|
"""
|
|
validate_settings(settings, get_pyinfra_validators())
|
|
|
|
logger.info("Starting webserver and queue consumer...")
|
|
|
|
app = app or FastAPI()
|
|
|
|
queue_manager = QueueManager(settings)
|
|
|
|
if settings.metrics.prometheus.enabled:
|
|
logger.info("Prometheus metrics enabled.")
|
|
app = add_prometheus_endpoint(app)
|
|
callback = make_prometheus_processing_time_decorator_from_settings(settings)(callback)
|
|
|
|
if settings.tracing.enabled:
|
|
setup_trace(settings)
|
|
|
|
instrument_pika()
|
|
instrument_app(app)
|
|
|
|
app = add_health_check_endpoint(app, queue_manager.is_ready)
|
|
|
|
webserver_thread = create_webserver_thread_from_settings(app, settings)
|
|
webserver_thread.start()
|
|
|
|
queue_manager.start_consuming(callback) |