-
-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bull - Scoped Jobs #5
Comments
You should never use the global EM, always create a fork. If it's not possible to leverage Btw it should be possible to use the await RequestContext.createAsync(orm.em, async () => {
// here it should be safe to use EM or repositories from DI
// so you could just wrap your handlers in this?
}); |
Thanks for the tip, that sounds like something that would make a good method decorators. Bull workers are defined via decorators so I guess I could have some kind of Only thing is I'm not sure how I'd access the |
Nope, no global state out of box, but you could probably get around that yourself, defining some global export where you store the ORM instance once it's ready. |
Ok thanks. I'll look into it. Might come back here with some more questions tho. But really appreciate the help. |
Just sharing here what we ended up doing: export function IsolateOrm() {
return function(
target: any,
propertyKey: string,
descriptor: PropertyDescriptor,
) {
const originalMethod = descriptor.value;
descriptor.value = async function() {
const context: any = this;
const args = arguments;
if (!(context.orm instanceof MikroORM)) {
throw new Error('@IsolateOrm decorator can only be applied to methods of classes that carry `orm: MikroORM`');
}
await RequestContext.create(context.orm.em, async () => {
await originalMethod.apply(context, args);
});
};
return descriptor;
};
} So the only constraint that's added is to inject MikroORM to our job consumers before applying the decorator to the worker methods. Might be worth having something similar built in here, I don't know. |
Feel free to send a PR. I'd use a bit different naming, Also you might want to use |
Cool, I'll do that eventually. Thanks Like your better naming as well 👍 |
@B4nan the |
Latest rc is stable, i am already using that in production. Will ship the final later this month. |
Cool, thanks. |
Is your feature request related to a problem? Please describe.
We're experiencing issues with Bull where the Entity Manager is shared across all running jobs. That leads to errors memory leaks.
Describe the solution you'd like
There is this open issue on the nestjs/bull side which might be a dependency here, but I ideally would like pretty much the same mechanism as RequestContext middleware but applied to Bull Jobs (which obviously are not considered Requests by nest)
Describe alternatives you've considered
I don't have any ideas for alternative but I'm happy to hear some if you do.
The text was updated successfully, but these errors were encountered: