browsertrix/backend/btrixcloud/main_op.py
Tessa Walsh 55a758f342
Consolidate ops class initialization (#2117)
Fixes #2111

The background job and operator entrypoints now use a shared function
that initalizes and returns the ops classes. This is not applied in the
main entrypoint as that also initializes the backend API, which we don't
want in the other entrypoints.
2024-10-30 15:33:22 -04:00

63 lines
1.4 KiB
Python

""" entrypoint module for operator """
import os
import sys
from fastapi import FastAPI
from .operator import init_operator_api
from .ops import init_ops
from .utils import register_exit_handler
app_root = FastAPI()
# ============================================================================
# pylint: disable=too-many-function-args, duplicate-code
def main():
"""init operator"""
# pylint: disable=import-outside-toplevel
if not os.environ.get("KUBERNETES_SERVICE_HOST"):
print(
"Sorry, the Browsertrix Backend must be run inside a Kubernetes environment.\
Kubernetes not detected (KUBERNETES_SERVICE_HOST is not set), Exiting"
)
sys.exit(1)
(
org_ops,
crawl_config_ops,
_,
crawl_ops,
page_ops,
coll_ops,
_,
storage_ops,
background_job_ops,
event_webhook_ops,
_,
) = init_ops()
return init_operator_api(
app_root,
crawl_config_ops,
crawl_ops,
org_ops,
coll_ops,
storage_ops,
event_webhook_ops,
background_job_ops,
page_ops,
)
# ============================================================================
@app_root.on_event("startup")
async def startup():
"""init on startup"""
register_exit_handler()
settings = main()
await settings.async_init()