belek
belek

Reputation: 957

How to add custom ID for Celery task in order to revoke task later?

I want to create delayed task and if there would be some conditions, I need to find that task and revoke it.

This is how I create task:

notify_before_departure.apply_async(
            args=(user.fcm_registration_token, booking.bus_stop_from.title,),
            eta=notification_time,
        )

So is there any attribute apply_async has where I can define my custom ID which can later be used to revoke this exact task? Something like this:

# create task
notify_before_departure.apply_async(
                args=(user.fcm_registration_token, booking.bus_stop_from.title,),
                eta=notification_time,
                custom_id=booking.id
            )
# revoke if needed
from celery.task.control import revoke 
revoke(booking.id, terminate=True)

Upvotes: 5

Views: 2684

Answers (1)

belek
belek

Reputation: 957

Well, answering my own question after reading FAQ :

notify_before_departure.apply_async(
            args=(user.fcm_registration_token, booking.bus_stop_from.title,),
            eta=notification_time,
            task_id=f"departure_push_for_booking_{booking.id}"
        )

And then:

revoke(f"departure_push_for_booking_{booking.id}", terminate=True)

Upvotes: 8

Related Questions