72 lines
2.7 KiB
Python
72 lines
2.7 KiB
Python
from __future__ import annotations
|
|
|
|
from celery import shared_task
|
|
|
|
|
|
@shared_task(name="govoplan_calendar.sync_due_caldav_sources")
|
|
def sync_due_caldav_sources_task(tenant_id: str | None = None, limit: int = 50) -> list[dict[str, object]]:
|
|
from govoplan_calendar.backend.service import sync_due_sources
|
|
from govoplan_core.core.module_entitlements import tenant_execution_scope
|
|
from govoplan_core.core.worker_runtime import build_worker_platform_registry
|
|
from govoplan_core.db.session import get_database
|
|
from govoplan_core.settings import settings
|
|
|
|
with get_database().SessionLocal() as session:
|
|
registry = build_worker_platform_registry(settings)
|
|
resolver = registry.tenant_entitlement_resolver()
|
|
admissions = (
|
|
(
|
|
resolver.admission(
|
|
session,
|
|
tenant_id=tenant_id,
|
|
module_id="calendar",
|
|
work_state="accepted",
|
|
),
|
|
)
|
|
if tenant_id is not None
|
|
else resolver.active_tenant_admissions(
|
|
session,
|
|
module_id="calendar",
|
|
work_state="accepted",
|
|
)
|
|
)
|
|
payload: list[dict[str, object]] = []
|
|
for admission in admissions:
|
|
if not admission.allowed:
|
|
payload.append(
|
|
{
|
|
"tenant_id": admission.tenant_id,
|
|
"status": "operator_action_required",
|
|
"operator_action": admission.payload(),
|
|
}
|
|
)
|
|
continue
|
|
with tenant_execution_scope(
|
|
resolver,
|
|
session,
|
|
tenant_id=admission.tenant_id,
|
|
work_state="accepted",
|
|
):
|
|
results = sync_due_sources(
|
|
session,
|
|
tenant_id=admission.tenant_id,
|
|
limit=limit,
|
|
)
|
|
payload.extend(
|
|
{
|
|
"tenant_id": admission.tenant_id,
|
|
"source_id": item.source_id,
|
|
"calendar_id": item.calendar_id,
|
|
"status": item.status,
|
|
"error": item.error,
|
|
"created": item.stats.created if item.stats else 0,
|
|
"updated": item.stats.updated if item.stats else 0,
|
|
"deleted": item.stats.deleted if item.stats else 0,
|
|
"unchanged": item.stats.unchanged if item.stats else 0,
|
|
"fetched": item.stats.fetched if item.stats else 0,
|
|
}
|
|
for item in results
|
|
)
|
|
session.commit()
|
|
return payload
|