0

Is there a way to add expiry date to a Huey Dynamic periodic task ? Just like there is an option in celery task - "some_celery_task.apply_async(args=('foo',), expires=expiry_date)" to add expiry date while creating the task.

I want to add the expiry date while creating the Huey Dynamic periodic task. I used "revoke" , it worked as it supposed to , but I want to stop the task completely after the expiry date not revoking it . When the Huey dynamic periodic task is revoked - message is displayed on the Huey terminal that the huey function is revoked (whenever crontab condition becomes true). (I am using Huey in django)

(Extra) What I did to meet the need of this expiry date - I created the function which return Days - Months pairs for crontab : For eg. start date = 2021-1-20 , end date = 2021-6-14 then the function will return - Days_Month :[['20-31',1], ['*','2-5'], ['1-14','6']] Then I call the Huey Dynamic periodic task (three times in this case). (the Days_Month function will return Day-Months as per requirement - Daily, Weekly, Monthly or repeating after n days)

Is there a better way to do this? Thank you for the help.

1 Answers1

0

The best solution will depend on how often you need this functionality of having periodic tasks with a specific end date but the ideal approach is probably involving your database.

I would create a database model (let's call it Job) with fields for your end_date, a next_execution_date and a field that indicates the interval between repetitions (like x days).

You would then create a periodic task with huey that runs every day (or even every hour/minute if you need finer grain of control). Every time this periodic task runs you would then go over all your Job instances and check whether their next_execution_date is in the past. If so, launch a new huey task that actually executes the functionality you need to have periodically executed per Job instance. On success, you calculate the new next_execution_date using the interval.

So whenever you want a new Job with a new end_date, you can just create this in the django admin (or make an interface for it) and you would set the next_execution_date as the first date where you want it to execute.

Your final solution would thus have the Job model and two huey decorated functions. One for the periodic task that merely checks whether Job instances need to be executed and updates their next_execution_date and another one that actually executes the periodic functionality per Job instance. This way you don't have to do any manual cancelling and you only need 1 periodic task that just runs indefinitely but doesn't actually execute anything if there are no Job instances that need to be run.

Note: this will only be a reasonable approach if you have multiple of these tasks and you potentially want to control the end_dates in your interface.

Glenn D.J.
  • 1,874
  • 1
  • 8
  • 19