Search code examples
pythondjangodatabasecelery

Edit an Object of the PostgresSQL Database in Django from Celery Worker


I'm trying to edit an object which is saved into my Postgress Database used by Django from a Celery Worker.

The Worker is called when the signal post_save is called with

@receiver(post_save, sender=Task)
def send_task_creation(sender, instance, created, raw, using, **kwargs):
    print(str(instance.pk))
    rephrase_task.delay(instance.pk)

My task.py contains the function for the Worker

from celery import shared_task
from celery import Celery
from celery_progress.backend import ProgressRecorder
from .models import Task
from time import sleep
from celery.utils.log import get_task_logger

celery = Celery(__name__)
celery.config_from_object(__name__)

@shared_task(bind=True)
def rephrase_task(self, primary_key):
    print('ID Worker ' + self.request.id)
    Task.objects.filter(pk=primary_key).update(TaskId=str(self.request.id))
    progress_recorder = ProgressRecorder(self)
    for i in range(0,100):
        sleep(1)
        i += 1
        progress_recorder.set_progress(i, 100)
    return 1

Now, everything seems to work expect the fact that Task.objects.filter(pk=primary_key).update(TaskId=str(self.request.id)) does really nothing

I'm seeing the Celery task processing correctly but it seems it can't access the database

My Celery settings in settings.py

REDIS_HOST = 'localhost' 
REDIS_PORT = '6379' 
BROKER_URL = 'redis://' + REDIS_HOST + ':' + REDIS_PORT + '/0' 
BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 3600} 
CELERY_RESULT_BACKEND = 'redis://' + REDIS_HOST + ':' + REDIS_PORT + '/0'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'

How can I access the database from the Celery Worker


Solution

  • The issue was because I needed to re-start the Celery worker as it seems it won't update settings while running