From 4ae70ef1f13a7147678b4b540564cb6b70e12254 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Thu, 16 Feb 2023 02:38:30 -0500 Subject: [PATCH] Fix celery task running --- vrobbler/__init__.py | 5 +++++ vrobbler/apps/scrobbles/views.py | 4 +++- vrobbler/celery.py | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/vrobbler/__init__.py b/vrobbler/__init__.py index e69de29..15d7c50 100644 --- a/vrobbler/__init__.py +++ b/vrobbler/__init__.py @@ -0,0 +1,5 @@ +# This will make sure the app is always imported when +# Django starts so that shared_task will use this app. +from .celery import app as celery_app + +__all__ = ('celery_app',) diff --git a/vrobbler/apps/scrobbles/views.py b/vrobbler/apps/scrobbles/views.py index 1b323f4..3ed3468 100644 --- a/vrobbler/apps/scrobbles/views.py +++ b/vrobbler/apps/scrobbles/views.py @@ -179,7 +179,9 @@ class AudioScrobblerImportCreateView( @permission_classes([IsAuthenticated]) @api_view(['GET']) def lastfm_import(request): - lfm_import = LastFmImport.objects.create(user=request.user) + lfm_import, created = LastFmImport.objects.get_or_create( + user=request.user, processed_on__isnull=True + ) process_lastfm_import.delay(lfm_import.id) diff --git a/vrobbler/celery.py b/vrobbler/celery.py index 7636f64..9ae34c0 100644 --- a/vrobbler/celery.py +++ b/vrobbler/celery.py @@ -3,7 +3,7 @@ import os from celery import Celery os.environ.setdefault("DJANGO_SETTINGS_MODULE", "vrobbler.settings") -app = Celery() +app = Celery("vrobbler") app.config_from_object("django.conf:settings", namespace="CELERY") app.autodiscover_tasks()