How to perform tasks in Celery using datetime from MySQL?

News is stored in the MySQL database from datetimepublication.

At any time, the user can add a new one to the table with the publication date of publication.

How to use Celery for a data list table and check if it's time to publish data?

To publish the data, another process responds (Python script). Therefore, Celery should call this script for each row of the MySQL table according to datetime.

How to do it with celery?

Another way, I think, is to create a queue with dateand idpublish, and then directly add data from a user form in that queue. Therefore, sholud will be a Celery process that monitors the queue for further tasks by date.

Celery provides documentation for periodic tasks and scheduled tasks, but there are no explanations or examples of how to use them.

Sorry if the question is trivial, I'm new to Piedon Celery.

+4
source share
1 answer

you can perform tasks using paramater etaon apply_async()(see http://docs.celeryproject.org/en/latest/userguide/calling.html#eta-and-countdown )

Parameter

eta must be in date and time format, so it will work with millisecond precision.

I recommend using ORM, so the datetime data type from your database will be automatically converted to ORM: D

let's say we have an Article model :

class Article(models.Model):
    title = models.CharField(max_length=100)
    published = models.BooleanField(default=False)
    created_date = models.DateTimeField()

and then our tasks :

@app.task(bind=True)
def set_published(*args, **kwargs):
    article = Article.objects.get(id=args[1])
    article.published = True
    article.save()

    print("article %d has been published" % args[1])

set_published ETA + 1minute

article = Article(title='This is a new article', published=False, created_date=datetime.utcnow())
article.save()

delta = timedelta(minutes=1)
set_published.apply_async(args=(article.id,), eta=article.created_date + delta)
+1

Source: https://habr.com/ru/post/1689826/


All Articles