[Python] How to trigger Django task at specific date in model field

Discussão em 'Python' iniciado por Stack, Dezembro 7, 2024.

  1. Stack

    Stack Membro Participativo

    I have a model that contains the following field

    class EventConfig(models.Model):
    end_date = models.DateTimeField(blank=True, null=True)


    I have celery installed that's sending periodic emails and triggering other tasks at set time intervals e.g every hour or at 12pm. However, if I want to trigger an email as exactly when 'end_date' is reached, what is the best way to do this? I realise I could set a Celery schedule to run every minute to check the date, but is running a batch job every 60 seconds perpetually, the best approach or would this have drawbacks?

    Is there a way for example to set the crontab schedule to read from a list of values so that I could so something like schedule_list = EventConfig.objects.all().values_list(end_date, flat=True) or schedule_list.append(self.end_date) whenever a model instance is saved, and have Celery read from 'schedule_list' so the batch jobs then only fire at the correct times?

    Or is there a better way to do this either with or without celery?

    Continue reading...

Compartilhe esta Página