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.
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 yourend_date, anext_execution_dateand a field that indicates theintervalbetween 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
Jobinstances and check whether theirnext_execution_dateis in the past. If so, launch a new huey task that actually executes the functionality you need to have periodically executed perJobinstance. On success, you calculate the newnext_execution_dateusing theinterval.So whenever you want a new
Jobwith a newend_date, you can just create this in the django admin (or make an interface for it) and you would set thenext_execution_dateas the first date where you want it to execute.Your final solution would thus have the
Jobmodel and two huey decorated functions. One for the periodic task that merely checks whetherJobinstances need to be executed and updates theirnext_execution_dateand another one that actually executes the periodic functionality perJobinstance. 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 noJobinstances 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.