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

97 lines
3.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.conf import settings
2026-11-19 21:18:37 +01:00
from django.db.models import Q
from django.utils import timezone
from coulobre.web_node import WebSite
from .models import WebActivation, WebProvisioningPlan
from .web_activation import activate_local_site
2026-11-19 21:18:37 +01:00
REDISPATCH_AFTER = timedelta(minutes=5)
WEB_ACTIVATION_QUEUE = "coulobre-web-activation"
2026-11-19 21:18:37 +01:00
@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 = WebSite(plan.account_id, plan.primary_domain).render("apache")
2026-11-19 21:18:37 +01:00
pending.update(apache_config=apache_config, completed_at=timezone.now())
@shared_task
def dispatch_web_activations():
if not settings.LOCAL_WEB_ACTIVATION:
return
stale_before = timezone.now() - REDISPATCH_AFTER
waiting = WebActivation.objects.filter(finished_at__isnull=True)
pending = (
waiting.filter(
Q(last_dispatched_at__isnull=True) | Q(last_dispatched_at__lte=stale_before)
)
.filter(Q(started_at__isnull=True) | Q(started_at__lte=stale_before))
.order_by("requested_at", "pk")
.values_list("pk", "requested_at")[:100]
)
for activation_id, requested_at in list(pending):
activate_web_plan.apply_async(
args=[activation_id], queue=WEB_ACTIVATION_QUEUE, retry=False
)
waiting.filter(pk=activation_id, requested_at=requested_at).update(
last_dispatched_at=timezone.now()
)
@shared_task(acks_late=True, reject_on_worker_lost=True)
def activate_web_plan(activation_id):
if not settings.LOCAL_WEB_ACTIVATION:
return
started_at = timezone.now()
pending = WebActivation.objects.filter(pk=activation_id, finished_at__isnull=True)
claim = pending.filter(
Q(started_at__isnull=True) | Q(started_at__lte=started_at - REDISPATCH_AFTER)
)
if not claim.update(started_at=started_at):
return
running = pending.filter(started_at=started_at)
activation = running.select_related("plan__account").first()
if activation is None:
return
plan = activation.plan
problem = ""
if not plan.can_export:
problem = "Request a completed Apache preview for the current account domain."
else:
try:
activate_local_site(WebSite(plan.account_id, plan.primary_domain))
except OSError:
problem = "Could not complete the request to the local web node. Check its socket and try again."
except ValueError as error:
problem = str(error)
running.update(finished_at=timezone.now(), problem=problem)