Pietro
Pietro

Reputation: 1835

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?

Upvotes: 3

Views: 1863

Answers (1)

Pietro
Pietro

Reputation: 1835

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.

Upvotes: 1

Related Questions