Python-telegram-bot: Implement CRON style job scheduling

Created on 3 Feb 2019  路  5Comments  路  Source: python-telegram-bot/python-telegram-bot

Hi all! I'd like to contribute to this project. I think it would be much easier to work with CRON style job scheduling. Below I describe the idea and list few examples. If this suggestion can be approved and merged I can come up with some integration plan.

Implementation details

def run_cron(self, callback, tab)

Where tab is a string object representing a standard CRON tab entry.

Function run_cron would throw appropriate exception if tab object is not a string matching CRON tab entry (use regex validation)

In order to easily integrate it into current scheduler we need to know how much time is required to pass until next execution, then we just add it into the queue with appropriate delay, which itself can be computed using croniter.

Few examples below.

Examples

Run callback_function everyday at 2:00 AM.

job = j.run_cron(callback_function, '0 2 * * *')

Run callback_function twice per day, at 5:00 AM and at 5:00 PM

job = j.run_cron(callback_function, '0 5,17 * * *')

Run callback_function every minute

job = j.run_cron(callback_function, '* * * * *')

Run callback_function every Sunday at 5:00 PM ( note that Sunday is both 0 and 7 )

job = j.run_cron(callback_function, '0 17 * * 0')

Run callback_function every 10 minutes

job = j.run_cron(callback_function, '*/10 * * * *')

Run callback_function every minute but only in January, May and August

job = j.run_cron(callback_function, '* * * 1,5,8 *')

Run callback_function every Friday and Sunday at 5:00 PM

job = j.run_cron(callback_function, '0 17 * * 5,7')
enhancement

Most helpful comment

There's not much feedback from @python-telegram-bot/maintainers, I guess I'll just prototype and PR. I'll keep you guys updated!

All 5 comments

I guess it's a decent addition, but maybe this would be better placed as a helper method (in telegram.utils.helpers)?
@python-telegram-bot/maintainers ?

There's not much feedback from @python-telegram-bot/maintainers, I guess I'll just prototype and PR. I'll keep you guys updated!

What a problem to use APScheduler or Celery?

@llybin there is absolutely nothing wrong with APScheduler nor Celery. Those are very useful tools and could be used to solve the problems I mentioned while opening this issue. Let me explain my line of reasoning

  1. I think most developers (if not nearly all of them) are familiar with CRON syntax, but not everyone used tools such as APScheduler
  2. Croniter integrates nicely into current implementation - it is a library that given a crontab and current timestamp provides next timestamp job has to be executed and it works well with threading.Event.wait(tmout) which is the way jobqueue works at the moment

After internal discussion, we came to the conclusion that we don't want to continue implementing scheduling logic on our own and instead rely on a 3rd party library for that, in fact APScheduler. Hence, I'll close this in favor of #1936.

Was this page helpful?
0 / 5 - 0 ratings