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

@ -1,4 +0,0 @@
# myproject/__init__.py
from .celery import app as celery_app
__all__ = ("celery_app",)

View File

@ -1,32 +0,0 @@
import os
from celery import Celery
from celery.schedules import crontab
# Set the default Django settings module for the 'celery' program.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "apibase.settings")
app = Celery("apibase")
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object("django.conf:settings", namespace="CELERY")
# Load task modules from all registered Django apps.
app.autodiscover_tasks(["api", "devices"])
# Add periodic task scheduler
app.conf.beat_schedule = {
"deactivate-expired-devices-every-day": {
"task": "api.tasks.deactivate_expired_devices",
"schedule": crontab(hour=0, minute=0), # Runs daily at midnight
},
}
@app.task(bind=True, ignore_result=True)
def debug_task(self):
print(f"Request: {self.request!r}")

View File

@ -68,7 +68,7 @@ INSTALLED_APPS = [
# third party
"django_filters",
"corsheaders",
"django_celery_beat",
"procrastinate.contrib.django",
]
if DEBUG:
@ -134,24 +134,16 @@ if not DEBUG:
SECURE_CONTENT_TYPE_NOSNIFF = True
# DATABASES
if DEBUG:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}
else:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": env("POSTGRES_DATABASE"),
"USER": env("POSTGRES_USER"),
"PASSWORD": env("POSTGRES_PASSWORD"),
"HOST": env("POSTGRES_HOST"),
"PORT": env("POSTGRES_PORT"),
},
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": env("POSTGRES_DATABASE"),
"USER": env("POSTGRES_USER"),
"PASSWORD": env("POSTGRES_PASSWORD"),
"HOST": env("POSTGRES_HOST"),
"PORT": env("POSTGRES_PORT"),
}
}
# More robust caching configuration
@ -307,6 +299,9 @@ logging.config.dictConfig(
"format": "%(asctime)s %(levelname)s %(message)s",
},
"django.server": DEFAULT_LOGGING["formatters"]["django.server"],
"procrastinate": {
"format": "%(asctime)s %(levelname)-7s %(name)s %(message)s"
},
},
"handlers": {
"console": {
@ -318,6 +313,11 @@ logging.config.dictConfig(
"formatter": "request",
},
"django.server": DEFAULT_LOGGING["handlers"]["django.server"],
"procrastinate": {
"level": "DEBUG",
"class": "logging.StreamHandler",
"formatter": "procrastinate",
},
},
"loggers": {
"": {
@ -340,6 +340,11 @@ logging.config.dictConfig(
"handlers": ["console"],
"propagate": False,
},
"procrastinate": {
"handlers": ["procrastinate"],
"level": "DEBUG",
"propagate": False,
},
},
}
)