Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Celery Starting a Task when Other Tasks have Completed

I have 3 tasks in Celery..

celery_app.send_task('tasks.read_cake_recipes')
celery_app.send_task('tasks.buy_ingredients')

celery_app.send_task('tasks.make_cake')

Both read_cake_recipes and buy_ingredients don't have any dependancies, however before the task make_cake can be run both read_cake_recipes and buy_ingredients need to have finished.

make_cake can be run at ANYTIME after the first two have started. But make_cake has no idea if the other tasks have completed. So if read_cake_recipes or buy_ingredients takes too long, then make_cake fails miserably.

Chaining tasks does not seem to work here because make_cake has more than one dependancies.

How can I still start the task make_cake, but have it then wait/pending etc until the other two tasks have completed first?

My saving grace is that read_cake_recipes and buy_ingredients save results to the DB, if make_cake somehow knew which ingredients or recipes to look for it could check this, maybe?

like image 225
Prometheus Avatar asked Jan 25 '26 04:01

Prometheus


1 Answers

Totally guessing at your underlying architecture, but here goes..

class Cake(models.Model):
    recipes_read = models.BooleanField(default=False)
    ingredients_purchased = models.BooleanField(default=False)
    batter_prepared = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        if self.recipes_read and self.ingredients_purchased:
            self.batter_prepared = True
        super(Cake, self).save(*args, **kwargs)


@task
read_cake_recipes():
    for cake in Cake.objects.all():
        # Read some shit!
        cake.recipes_read = True
        cake.save()

@task
buy_cake_ingredients():
    for cake in Cake.objects.all():
        # Buy some shit!
        cake.ingredients_purchased = True
        cake.save()

@task
make_cake():
    for cake in Cake.objects.filter(batter_prepared=True):
        # Make that shit!
like image 122
Casey Kinsey Avatar answered Jan 27 '26 18:01

Casey Kinsey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!