Refactor task management: replace Celery with Procrastinate for background tasks and update related configurations

This commit is contained in:
2025-06-28 10:25:33 +05:00
parent 28315c59cf
commit e3b39478eb
8 changed files with 37 additions and 66 deletions

View File

@ -4,11 +4,12 @@ from devices.models import Device
from api.notifications import send_sms
import os
import logging
from celery import shared_task
from django.utils import timezone
from api.notifications import send_clean_telegram_markdown
from api.omada import Omada
from apibase.env import env, BASE_DIR
from procrastinate.contrib.django import app
logger = logging.getLogger(__name__)
@ -17,13 +18,14 @@ env.read_env(os.path.join(BASE_DIR, ".env"))
omada_client = Omada()
@shared_task
@app.task
def add(x, y):
print(f"Adding {x} and {y}")
return x + y
@shared_task
@app.periodic(cron="0 0 */28 * *") # type: ignore
@app.task
def deactivate_expired_devices():
expired_devices = Device.objects.filter(
expiry_date__lte=timezone.localtime(timezone.now()), is_active=True
@ -55,12 +57,13 @@ def deactivate_expired_devices():
}
@shared_task
@app.task
def add_new_devices_to_omada(new_devices: list[dict]):
"""
Add new devices to Omada via Omada class.
:param new_devices: List of new device names to add.
"""
logger.info("Running add new devices to Omada task...")
omada_client.add_new_devices_to_omada(new_devices)

View File

@ -60,7 +60,7 @@ class ErrorMessages:
@api_view(["GET"])
def healthcheck(request):
add.delay(1, 2)
add.defer(1, 2)
return Response({"status": "Good"}, status=status.HTTP_200_OK)