Search code examples
pythonceleryaiogram

What to use as a worker for deferred tasks?


I am creating a bot on aiogram. Admin of the bot can register events that will be executed at stated datetime. An event may be delayed by a week or even more. I decided to use celery as a worker and redis as a broker. However, in celery docs I read this:

Tasks with eta or countdown are immediately fetched by the worker and until the scheduled time passes, they reside in the worker’s memory. When using those options to schedule lots of tasks for a distant future, those tasks may accumulate in the worker and make a significant impact on the RAM usage.
Moreover, tasks are not acknowledged until the worker starts executing them. If using Redis as a broker, task will get redelivered when countdown exceeds visibility_timeout (see Caveats).
Therefore, using eta and countdown is not recommended for scheduling tasks for a distant future. Ideally, use values no longer than several minutes. For longer durations, consider using database-backed periodic tasks, e.g. with https://pypi.org/project/django-celery-beat/ if using Django (see Using custom scheduler classes).

Now I started to think about the speed of my future bot. According to this information it might well be so that it's going to be slow and use a lot of memory.
I am looking for any advice. What do I do in such a situation? Should I put away using celery and try another library? One more thing: despite the fact that tasks may be delayed by a long period of time, I don't plan creating a lot of tasks (more than six-seven).
Thank you!


Solution

  • We have implemented similar thing in one our project using celery. We had to send alert mails at a preset time (we have considered only hour and minute for the preset time, seconds were ignored) on a particular date or recursively at particular time on daily, weekly or monthly basis.

    Solution

    • Save event information along with its date, hour and minute to execute in DB when admin registers the event.
    • start two workers one beat worker and one normal worker
    • create two tasks an event checker task (a periodic task checkout periodic task documentation) and an event processor task.
    • Event checker task should be scheduled to run every minute using celery beat schedule. This task should implement the logic to check current date, hour and minute with the events execution time stored in DB. If any matching event has found, call the event processor task and pass the event information from DB to this task
    • Logic for executing the event should be handled in the event processor task.