PythonNest / PyNest

PyNest is a Python framework built on top of FastAPI that follows the modular architecture of NestJS
https://pythonnest.github.io/PyNest
MIT License
754 stars 52 forks source link

Proposal to implement decorators for task scheduling #77

Open maxsonferovante opened 2 months ago

maxsonferovante commented 2 months ago

Thinking about a way to contribute to the project, I found a very useful resource that I use in my daily life: task scheduling.

I tried to sketch out what my idea would be like, based on what Nest already has at Task Scheduling

I made a draft project

I ask for help to develop functionality along the lines of the pynest project.

geovalexis commented 2 months ago

That's a great idea @maxsonferovante! 🚀 That's a need many projects run into at some point. I implemented this feature in one my projects and I used a library called APScheduler, although there are many others out there. Let me know if you use that library, I can provide help.

maxsonferovante commented 2 months ago

Hello @geovalexis , I had an idea exactly because of a need that arose at work. I have an idea of ​​how to do it in pynest, but I'm worried about the orchestration part of the scheduled tasks and how apscheduler can help us with that. From what I saw and understood the managed API documentation. So it would be loading the beginning of the task event loop along with loading the modules, just like what is done in Nest. This is how I imagine it to be.

ItayTheDar commented 2 months ago

@maxsonferovante this is such a great idea! I would help as much as i can. How NestJS implement this feature?

geovalexis commented 2 months ago

Hello @geovalexis , I had an idea exactly because of a need that arose at work. I have an idea of ​​how to do it in pynest, but I'm worried about the orchestration part of the scheduled tasks and how apscheduler can help us with that. From what I saw and understood the managed API documentation. So it would be loading the beginning of the task event loop along with loading the modules, just like what is done in Nest. This is how I imagine it to be.

What do you mean by orchestration part? You mean like managing the state of the job (start, stop, etc...)? APScheduler provides a whole bunch of functions for this purpose in particular. What's best, it provides a decorator function for initialising a new cronjob. It's called schedule_job() (see https://apscheduler.readthedocs.io/en/3.x/userguide.html#adding-jobs).

We could just create a wrapper of that function that seamlessly integrates into PyNest.

maxsonferovante commented 2 months ago

Hey @ItayTheDar , from what I understand, NestJs, using the Cron and Interval decorator, registers the Job and its parameters in a list to be initialized when the module is loaded (https://docs.nestjs.com/techniques/task-scheduling )

Then, internally the decorator would register the work, as the apscheduler documentation demonstrates https://apscheduler.readthedocs.io/en/3.x/userguide.html#adding-jobs

When I express my concern about the orchestration, it is, in a way, an excess of concern. The decorator would have to be able to validate the parameters, register and start when the module was loaded, basically. @geovalexis

I'm finishing another project, so I'll start creating my idea with pynest. Thank you, including your availability. I'll bring you news soon. Hugs

ItayTheDar commented 1 month ago

Hi @maxsonferovante !

Do you get a chance to start working on that? Iv'e just faced on use case in my application that requires job scheduling and wander if you have started to implement this feature?

maxsonferovante commented 1 month ago

@ItayTheDar

Hey, sorry for disappearing, I've been having a rough few days at work.

I've started working on the implementation and opened a pull request so you can follow along and contribute, in case I've also started the implementation due to your demand.

pullrequest

ItayTheDar commented 1 month ago

@maxsonferovante saw the pr!! This is so great. I will integrate this into my project and make adjustments if needed. Thank you so much for this effort, this is crucial feature for PyNest