[ ] Regression
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Resuming my case of use: I have a simple controller that GET /redirects
, POST /redirects
and POST /redirects/update
.
This last one fetch an external URL and add to my database a list of redirects. Then, with my simple structure, I added a new decorator that I created.
@Post('/update')
@Cron('*/5 * * * * *')
update() {}
This Cron function is added to a list and after my app.listen
I init and invoke the method update
(or whatever I add the decorator).
The problem is, to call this update
, I can do: this.app.select(RedirectsModule).get(RedirectsController).update();
, but I can't run this by string name, only the component.
What I need is that select
and get
accepts string, not the Module and Controller.
A workaround I found is to create an array like:
[
{ name: 'FooModule', module: FooModule },
{ name: 'BarModule', module: BarModule }
]
and search the .module
based on .name
. But I don't think it's the best to use.
Anyway, if anybody has a batter solution to add my "cron jobs", I would appreciate.
Well, looking deeper into the code, I found out some solutions. But this will need changes in the core, that I think it's nice to have.
I created a method decorator:
export const Cron = (period) => {
return (target, paramName, descriptor) => {
// Here I can add the descriptor to an array that I can access later. You will understand :P
return descriptor;
}
};
Then, after start the server with await app.listen(3000);
, I have access to the app
that returns me a INestApplication
.
I looked into the code and saw that we have a property container
, that has a method getModules()
. So, I have to force the use of:
// Get the modules
const modules: ModulesContainer = app['container'].getModules();
That's not pretty, but the first thing I want to add is methods to access some part of this code, something like getContainer()
.
With this modules, I can loop in the list with modules.forEach(...)
and check if the module is in my cron list, and call the method with (considering that I have methodName
:
modules.routes.forEach(route => route.instance[methodName]());
So, I think we can add some things to make easier to access deeper in the code, so we can add more functionalities, like this Cron decorator that I'm looking for.
If I wasn't that clear, I try to explain in other way :P
Hi @eliasjnior
Did you see https://github.com/OptimalBits/bull ?
it's The fastest, most reliable, Redis-based queue for Nodejs. _as said_
it also supports Scheduled and repeat jobs according to a cron specification.
so I think it will be suited here.
in fact, @fwoelffel created a Dynamic Module around it.
nest-bull, so it can be used in nest-ish way, _thanks Fr茅d茅ric_ :smile: .
So back to the Monkey Patching
:joy: that you did with nest container.
imo, it's very bad idea to get direct access to the container, as it was my idea at first :smile: see here #282
see also this pull #283 .
and now we have Dynamic Module
so we can write our 3rd party module, did you checked out NestRouterModule
it's a very good, simple example of how you could make your own dynamic modules that could be configured and change behavior as this this configuration .
Regards.
@shekohex thank you. You gave me a good direction.
I did a rough read in what you sent and the topics, this will help me a lot :)
I just found that v5 has a doc to do that. Don't know if was after my ticket or before, but I liked! This made extremely easy to work with now!
I just need to app.get(TasksController);
and I have the access to instance of my controller :)
Glad to hear! 馃檪 v5.0.0
will hit final release very soon
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
Glad to hear! 馃檪
v5.0.0
will hit final release very soon