Skip to content
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

JobLock option reEnqueue to skip trying job again #449

Merged
merged 1 commit into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async function boot() {
add: {
plugins: ["JobLock"],
pluginOptions: {
JobLock: {},
JobLock: { reEnqueue: true },
},
perform: async (a, b) => {
await new Promise((resolve) => {
Expand Down
58 changes: 58 additions & 0 deletions __tests__/plugins/jobLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ const jobs = {
return answer;
},
},
withoutReEnqueue: {
plugins: ["JobLock"],
pluginOptions: { JobLock: { reEnqueue: false } },
perform: async () => {
return "hi";
},
},
};

describe("plugins", () => {
Expand Down Expand Up @@ -204,6 +211,57 @@ describe("plugins", () => {
worker2.start();
});

test("can be configured not to re-enqueue a duplicate task", async (done) => {
let count = 0;
worker1 = new Worker(
{
connection: specHelper.cleanConnectionDetails(),
timeout: specHelper.timeout,
queues: [specHelper.queue],
},
jobs
);
worker2 = new Worker(
{
connection: specHelper.cleanConnectionDetails(),
timeout: specHelper.timeout,
queues: [specHelper.queue],
},
jobs
);

worker1.on("error", (error) => {
throw error;
});
worker2.on("error", (error) => {
throw error;
});

await worker1.connect();
await worker2.connect();

const onComplete = async () => {
count++;
expect(count).toBe(1);
await worker1.end();
await worker2.end();

const timestamps = await queue.timestamps();
expect(timestamps).toEqual([]);

done();
};

worker1.on("success", onComplete);
worker2.on("success", onComplete);

await queue.enqueue(specHelper.queue, "withoutReEnqueue");
await queue.enqueue(specHelper.queue, "withoutReEnqueue");

worker1.start();
worker2.start();
});

test("will run 2 jobs with the different args at the same time", async (done) => {
worker1 = new Worker(
{
Expand Down
4 changes: 1 addition & 3 deletions __tests__/utils/specHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ const SpecHelper = {

cleanup: async function () {
const keys = await this.redis.keys(this.namespace + "*");
if (keys.length > 0) {
await this.redis.del(keys);
}
if (keys.length > 0) await this.redis.del(keys);
},

disconnect: async function () {
Expand Down
10 changes: 9 additions & 1 deletion src/plugins/JobLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ export class JobLock extends Plugin {
if (lockedByMe && lockedByMe.toString().toLowerCase() === "ok") {
return true;
} else {
await this.reEnqueue();
const options = this.job.pluginOptions;
const toReEnqueue = options.JobLock
? options.JobLock.reEnqueue !== null &&
options.JobLock.reEnqueue !== undefined
? options.JobLock.reEnqueue
: true
: true;

if (toReEnqueue) await this.reEnqueue();
return false;
}
}
Expand Down