Reputation: 969
I want to create database records with celery task. But for some reason object.save()
method is not working with task.apply_async()
(Apply tasks asynchronousy).
Same record (Ticker) is saved in the database with a celery task while running it locally:
get_all_tickers.apply()
But is not saved with asynchronous mode:
get_all_tickers.apply_async()
In both cases INSERT statement is visible in the server log.
models.py
class Ticker(TimeStampedModel):
ask = models.DecimalField(max_digits=18, decimal_places=8)
bid = models.DecimalField(max_digits=18, decimal_places=8)
pair = models.ForeignKey(Pair)
tasks.py
from celery import shared_task
...
@shared_task()
def get_all_tickers():
pair = Pair.objects.last()
ticker = Ticker(ask=Decimal(1.0), bid=Decimal(1.0), pair=pair)
ticker.save()
Upvotes: 1
Views: 1198
Reputation: 969
Django server(root) and celery(celery_user) tasks were run by different users, therefore, celery_user has not got write access to the database. So then task.apply()
is run by root and can save()
records and task.apply_async()
- by celery_user and cannot.
The short-term solution for this problem was to make celery_user the owner of the database (development env using sqlite3):
chown celery_user:celery_user db.sqlite3
Although, Adding a group to the celery_user would be more appropriate (for short-term).
Or long term - run everything with unprivileged users https://www.syncano.io/blog/configuring-running-django-celery-docker-containers-pt-1/
p.s.: I was having some problems with http://docs.celeryproject.org/en/latest/reference/celery.contrib.rdb.html (debugging tool for celery). Telnet was disconnected every time the task was received (i.e. every 30 seconds). Make sure that You are using task which is dedicated for debugging.
Upvotes: 2