Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django schedule a "one time" task based on datetime model attribute

What the best solution in Django to solve this kind of problem:

I need to set a schedule time, based on an object attribute value, to run a "one time" task, when schedule time is reached.

For each attribute updates, the schedule time has to be also updated.

Example (pseudo code)

class Runner(models.Model):
    execute_time = models.DateTimeField()

post_save( update_scheduler, sender=Runner)


def update_scheduler(sender, instance, created, **kwargs):
    if created:
        # set schedule time = instance.execute_time
        create_or_update_schedule(instance.datetime)

Is it possibile to do something like this using Celery? update schedule time on object update?

like image 455
Pietro Avatar asked Mar 22 '26 14:03

Pietro


1 Answers

As Banana suggested, I solved this issue using eta. Here a simple sample code:

task.apply_async([ev_objects], eta=my_eta, task_id=my_task_id)

Also "revoke" can come in handy to terminate a task that hasn't start yet. It's a remote control command so it wouldn't work with django database solution but only with Redis or RabbitMQ.

I'm still searching for a solution to revoke a task using Django database.

like image 98
Pietro Avatar answered Mar 25 '26 06:03

Pietro



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!