gendesign/backend/app/api/v1/admin_jobs.py
lekss361 b233bf91cc
All checks were successful
Deploy / changes (push) Successful in 5s
Deploy / build-frontend (push) Has been skipped
Deploy / build-backend (push) Successful in 1m20s
Deploy / build-worker (push) Successful in 2m21s
Deploy / deploy (push) Successful in 1m7s
refactor(security): убрать X-Admin-Token (Caddy basic_auth достаточен) (#437)
2026-05-23 10:41:22 +00:00

73 lines
2.4 KiB
Python

"""Admin API для централизованных настроек Celery-job'ов.
GET /api/v1/admin/jobs/settings — список всех job_settings
GET /api/v1/admin/jobs/settings/{type} — одна строка по job_type
PUT /api/v1/admin/jobs/settings/{type} — PATCH-style update
Auth: gendsgn.ru-wide Caddy basic_auth gate (PR #426). App-level X-Admin-Token
header removed 2026-05-23 — двойная auth избыточна для pilot.
"""
from __future__ import annotations
from typing import Annotated, Any
from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
from app.core.db import get_db
from app.schemas.job_settings import JobSettingRead, JobSettingUpdate
router = APIRouter()
@router.get("/settings", response_model=list[JobSettingRead])
def list_job_settings(
db: Annotated[Session, Depends(get_db)],
) -> Any:
"""Список всех job_settings (все job_type)."""
from app.services.job_settings import get_all
return get_all(db)
@router.get("/settings/{job_type}", response_model=JobSettingRead)
def get_job_setting(
job_type: str,
db: Annotated[Session, Depends(get_db)],
) -> Any:
"""Настройки одного job_type. Возвращает fallback defaults если строки нет в БД."""
from app.services.job_settings import get_one
return get_one(job_type, db)
@router.put("/settings/{job_type}", response_model=JobSettingRead)
def update_job_setting(
job_type: str,
payload: JobSettingUpdate,
db: Annotated[Session, Depends(get_db)],
) -> Any:
"""PATCH-style update: передавать только изменяемые поля.
После изменения cron_schedule нужен restart beat-контейнера.
Остальные поля (rate_ms, extra_config, enabled) применяются динамически.
cron_schedule='' устанавливает NULL (manual-only режим).
rate_ms=0 устанавливает NULL (no throttle).
"""
from app.services.job_settings import update
result = update(
job_type,
db,
enabled=payload.enabled,
queue_name=payload.queue_name,
cron_schedule=payload.cron_schedule,
rate_ms=payload.rate_ms,
max_retries=payload.max_retries,
max_concurrency=payload.max_concurrency,
extra_config=payload.extra_config,
updated_by="admin-ui",
)
return result