gjcarneiro / yacron

A modern Cron replacement that is Docker-friendly
MIT License
449 stars 38 forks source link

Exposing simple REST endpoint to report what will run #23

Closed eric239 closed 4 years ago

eric239 commented 5 years ago

@gjcarneiro raising a question here since I don't know if there's a better place

In our environment, we find it highly desirable to be able to answer questions like "what is scheduled to run in X [unit of time]"? For a simple use case, consider yacron triggering a (presumably long-ish) action within a running process; so if we intend to redeploy the said process, we want to make sure no relevant actions are about to take place within 10 minutes or so.

Of course we can read and mentally parse the yaml config, but it's not very human-friendly activity and requires mental date/time arithmetics. Hence we've been contemplating exposing a very simple HTTP API that can be interrogated to get ordered list of (job, timestamp) tuples for all jobs scheduled to run X units of time from now.

Have you ever considered this kind of feature? Will you accept a PR for such feature?

Thanks!

gjcarneiro commented 5 years ago

I wouldn't mind such a feature. However, it's not so easy to implement, due to the way yacron is built on top of the crontab python module.

crontab has a way to test a certain timestamp to see if it matches a certain cron schedule. It has no provision for checking when is the next timestamp that matches the schedule. So the code would have to do a number of tests, one per minute. This doesn't scale well, for example with 10 jobs and an interval of 30 minutes, you'd need to do 300 tests.

It's not terrible for low number of jobs and small interval, but it scales badly. If you accept that it might be slow, I guess we can have it. As long as the aiohttp handler does an await asyncio.sleep(0) every few iterations, it shouldn't slow down the rest of yacron operation.

eric239 commented 5 years ago

Correct me if I'm missing something obvious, but as per the docs wouldn't methods under "Schedule Functionality" be exactly what we'd need? E.g. datetime = schedule.get_next()

gjcarneiro commented 5 years ago

I think you linked to the wrong module, yacron is using the python module crontab, python-crontab is a different module, with a different API.

But that point is moot, as the crontab module has that get_next() API. Yes, you are right, I didn't remember that API.

So, yes, should be pretty simple to add a REST API to get what you want. If you want to work on this:

  1. I prefer to use aiohttp (it is an outstanding http framework for this sort of thing and I use it a lot);
  2. There should be an option to opt in to enabling the REST interface, otherwise security people will bite my head off. I personally don't care (I always run yacron in docker container), but some people might care. There should be options to configure the bind host and bind port.
eric239 commented 5 years ago

Thanks, both points make complete sense.

On Wed, Nov 7, 2018 at 3:40 AM Gustavo J. A. M. Carneiro < notifications@github.com> wrote:

I think you linked to the wrong module, yacron is using the python module crontab, python-crontab is a different module, with a different API.

But that point is moot, as the crontab module has that get_next() API. Yes, you are right, I didn't remember that API.

So, yes, should be pretty simple to add a REST API to get what you want. If you want to work on this:

  1. I prefer to use aiohttp (it is an outstanding http framework for this sort of thing and I use it a lot);
  2. There should be an option to opt in to enabling the REST interface, otherwise security people will bite my head off. I personally don't care (I always run yacron in docker container), but some people might care. There should be options to configure the bind host and bind port.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/gjcarneiro/yacron/issues/23#issuecomment-436596334, or mute the thread https://github.com/notifications/unsubscribe-auth/AIEbRXF7k1gNjXs0u53uP-3h_JdAfeuwks5ussafgaJpZM4YRhPW .