graphile / worker

High performance Node.js/PostgreSQL job queue (also suitable for getting jobs generated by PostgreSQL triggers/functions out into a different work queue)
http://worker.graphile.org/
MIT License
1.78k stars 97 forks source link

Feature: Add an API for giving workers more helpers #113

Open hcharley opened 4 years ago

hcharley commented 4 years ago

Usecase

Give users of graphile-worker the ability to pass in custom helpers. This could include custom database connection objects, metadata, application-specific helpers, etc...

For example:

My specific usecase is to pass a Slonik database connection object.

Implementation

https://github.com/graphile/worker/blob/1d4fa8c08f10f49eaca2343365a5db2b477206bf/src/helpers.ts#L71

It looks like if this TODO was done, this usecase could be satisified.

I'm not sure what the ultimate implementation would look like. Would there be a specific file like helpers.ts that would be loaded and named exports would automatically be included?

Pitfalls/Things to watch out for

benjie commented 4 years ago

Is there a reason to not simply const helpers = require('../helpers') inside the worker?

hcharley commented 4 years ago

No, that seems like a good solution. I can try that out. Thanks!

benjie commented 4 years ago

[semi-automated message] Thanks for your question; hopefully we're well on the way to helping you solve your issue. This doesn't currently seem to be a bug in the library so I'm going to close the issue, but please feel free to keep requesting help below and if it does turn out to be a bug we can definitely re-open it 👍

You can also ask for help in the #help-and-support channel in our Discord chat.

StepanMynarik commented 5 months ago

@benjie Just encountered this.

The TODO is still there and I believe that in certain situations this would allow for much more pleasurable DX.

For example in our case we have set up nodemailer instance for fastify route plugin consumption. All setup and "service" init is defined in server.ts. All works beautifully. But then I integrated Graphile Worker and now I need the nodemailer instance in my tasks to send e-mails. Would be elegant as heck with helpers object augmentation. Without it, I have to refactor.

Not the end of the world, don't get me wrong, Graphile Worker is an amazing alternative to pg-boss! But this little thing would definitely make it even better.