-
-
Notifications
You must be signed in to change notification settings - Fork 151
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
Using classes to define jobs? #270
Comments
I agree! I would recommend an approach that if the Do you need help writing a Pull Request? |
I am in fact incredibly rusty in coding - spend two years in hospital, I am just really getting back. Bit by bit…or byte.
I will try to see if I can do something there, but I wouldn’t be looking forward for that too much… So if you can, could you try to put something together there?
… Am 08.11.2018 um 19:53 schrieb Evan Tahler ***@***.***>:
I agree! I would recommend an approach that if the perform method is a function, it runs it, or if it is a Class, it would exec a new instance.
Do you need help writing a Pull Request?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub <#270 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/ACwnlETZFxT-WbjMmL1PWO6Dk8_whjwHks5utH2MgaJpZM4YVNQE>.
|
@IngwiePhoenix It is quite possible and I am in-fact doing that. As you wrote above, a simple base-class which simply confirms the contract, along with also providing simple details such as the name of the job and You can then utilise the |
Definitively! I have kind of lost track of what I had started on in this PR. I may just re-write the PR again and put my mind to it :) I am very sorry I went silent on this... |
This is how i do my jobs.
|
I did like this export class Job {
async perform(): Promise<number> {
console.log(this);
const cmd = this.createCommand();
return 0;
}
private createCommand() {
}
}
const jobs = {
job: new Job(),
};
worker = new Worker(
{ connection: connectionDetails, queues: ["test"] },
jobs
); this worked fine, until I needed to call |
the |
I ended up creating this wrapper: /**
*
* Wrapper to overcome issue that perform is called with wrong this context.
*
* @author Elan Ruusamäe <glen@pld-linux.org>
*
* https://github.com/actionhero/node-resque/issues/270
*/
class JobWrapper {
static wrap(proxy: any) {
return {
perform: async (...args: []) => {
return await proxy.perform.apply(proxy, args);
}
};
}
}
const jobs = {
test: JobWrapper.wrap(new Job()),
}; |
Nice! I would also be open to changing application of |
I think you can make it non-breaking change (or minimal impact affecting only class uses) if you test is each jobs key is a class instance ( I'm not sure is that distinguishing possible, but probably small StackOverflow search will give the answer. |
If you are looking for another example of a wrapping method, here's how Actionhero takes it's Task classes and structures them into the node-resque methods https://github.com/actionhero/actionhero/blob/3f9d936000d44afffd8798f1c3f5a3cb872390d2/src/initializers/tasks.ts#L62-L134 |
Never link to branches!
I've already encountered two of your broken links when getting acquainted with this project. |
@evantahler fix your post (edit it) and use permalink: |
I would like to know if it is possible to create a job as a class. For instance, would something like this be possible?
It would make organizing a lot of background jobs much easier to write everything in classes and just have them register at an initial script.
The text was updated successfully, but these errors were encountered: