Files
coulobre/src/coulobre/hosting/tasks.py
T

43 lines
1.3 KiB
Python
Raw Normal View History

2026-11-19 21:18:37 +01:00
from datetime import timedelta
from celery import shared_task
from django.db.models import Q
from django.template.loader import render_to_string
from django.utils import timezone
from .models import WebProvisioningPlan
from .web_node import web_site_configuration
2026-11-19 21:18:37 +01:00
REDISPATCH_AFTER = timedelta(minutes=5)
@shared_task
def dispatch_web_plans():
stale_before = timezone.now() - REDISPATCH_AFTER
pending_ids = (
WebProvisioningPlan.objects.filter(completed_at__isnull=True)
.filter(
Q(last_dispatched_at__isnull=True) | Q(last_dispatched_at__lte=stale_before)
)
.order_by("requested_at", "pk")
.values_list("pk", flat=True)[:100]
)
for plan_id in list(pending_ids):
render_web_plan.apply_async(args=[plan_id], retry=False)
WebProvisioningPlan.objects.filter(
pk=plan_id, completed_at__isnull=True
).update(last_dispatched_at=timezone.now())
@shared_task(acks_late=True, reject_on_worker_lost=True)
def render_web_plan(plan_id):
pending = WebProvisioningPlan.objects.filter(pk=plan_id, completed_at__isnull=True)
plan = pending.first()
if plan is None:
return
apache_config = render_to_string(
"hosting/apache-vhost.conf", web_site_configuration(plan)
2026-11-19 21:18:37 +01:00
)
pending.update(apache_config=apache_config, completed_at=timezone.now())