From 65183fcf542d0227ec1d4d6637b46b5381331787 Mon Sep 17 00:00:00 2001 From: Robert D'Ercole Date: Mon, 10 Feb 2020 13:25:45 -0500 Subject: [PATCH 1/5] fix: modified QueueGetters.getJob and Job.fromId to also return null to maintain consistency with v3 API. --- src/classes/job.ts | 2 +- src/classes/queue-getters.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/classes/job.ts b/src/classes/job.ts index b8170dbacb..d3f24834cb 100644 --- a/src/classes/job.ts +++ b/src/classes/job.ts @@ -135,7 +135,7 @@ export class Job { return job; } - static async fromId(queue: QueueBase, jobId: string) { + static async fromId(queue: QueueBase, jobId: string): Promise { // jobId can be undefined if moveJob returns undefined if (jobId) { const client = await queue.client; diff --git a/src/classes/queue-getters.ts b/src/classes/queue-getters.ts index a31c4ab746..9aed285d21 100644 --- a/src/classes/queue-getters.ts +++ b/src/classes/queue-getters.ts @@ -6,7 +6,7 @@ import { Job } from './job'; import { clientCommandMessageReg } from './worker'; export class QueueGetters extends QueueBase { - getJob(jobId: string) { + getJob(jobId: string): Promise { return Job.fromId(this, jobId); } From ede352be75ffe05bf633516db9eda88467c562bf Mon Sep 17 00:00:00 2001 From: Robert D'Ercole Date: Tue, 11 Feb 2020 13:13:17 -0500 Subject: [PATCH 2/5] fix: modified QueueGetters.getJob and Job.fromId to return undefined instead of null --- src/classes/job.ts | 7 +++++-- src/classes/queue-getters.ts | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/classes/job.ts b/src/classes/job.ts index d3f24834cb..3ea4450a63 100644 --- a/src/classes/job.ts +++ b/src/classes/job.ts @@ -135,12 +135,15 @@ export class Job { return job; } - static async fromId(queue: QueueBase, jobId: string): Promise { + static async fromId( + queue: QueueBase, + jobId: string, + ): Promise { // jobId can be undefined if moveJob returns undefined if (jobId) { const client = await queue.client; const jobData = await client.hgetall(queue.toKey(jobId)); - return isEmpty(jobData) ? null : Job.fromJSON(queue, jobData, jobId); + return isEmpty(jobData) ? undefined : Job.fromJSON(queue, jobData, jobId); } } diff --git a/src/classes/queue-getters.ts b/src/classes/queue-getters.ts index 9aed285d21..b6c3833d9c 100644 --- a/src/classes/queue-getters.ts +++ b/src/classes/queue-getters.ts @@ -6,7 +6,7 @@ import { Job } from './job'; import { clientCommandMessageReg } from './worker'; export class QueueGetters extends QueueBase { - getJob(jobId: string): Promise { + getJob(jobId: string): Promise { return Job.fromId(this, jobId); } From 2de620b3eb9f292fc25f88bd1bff55805f832d03 Mon Sep 17 00:00:00 2001 From: Robert D'Ercole Date: Tue, 11 Feb 2020 13:15:54 -0500 Subject: [PATCH 3/5] test: updated tests for ede352b --- src/test/test_job.ts | 2 +- src/test/test_worker.ts | 24 ++++++++++++------------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/test/test_job.ts b/src/test/test_job.ts index 4a6d2928a4..38b9f5971d 100644 --- a/src/test/test_job.ts +++ b/src/test/test_job.ts @@ -101,7 +101,7 @@ describe('Job', function() { const job = await Job.create(queue, 'test', { foo: 'bar' }); await job.remove(); const storedJob = await Job.fromId(queue, job.id); - expect(storedJob).to.be.equal(null); + expect(storedJob).to.be.equal(undefined); }); }); diff --git a/src/test/test_worker.ts b/src/test/test_worker.ts index 11fdae5886..942e9c633d 100644 --- a/src/test/test_worker.ts +++ b/src/test/test_worker.ts @@ -57,7 +57,7 @@ describe('workers', function() { worker.on('completed', async (job: Job) => { try { const gotJob = await queue.getJob(job.id); - expect(gotJob).to.be.equal(null); + expect(gotJob).to.be.equal(undefined); const counts = await queue.getJobCounts('completed'); expect(counts.completed).to.be.equal(0); await worker.close(); @@ -89,7 +89,7 @@ describe('workers', function() { worker.on('completed', async job => { try { const gotJob = await newQueue.getJob(job.id); - expect(gotJob).to.be.equal(null); + expect(gotJob).to.be.equal(undefined); const counts = await newQueue.getJobCounts('completed'); expect(counts.completed).to.be.equal(0); await worker.close(); @@ -130,10 +130,10 @@ describe('workers', function() { const job = await queue.getJob(jobId); const logs = await queue.getJobLogs(jobId); if (index >= datas.length - keepJobs) { - expect(job).to.not.be.equal(null); + expect(job).to.not.be.equal(undefined); expect(logs.logs).to.not.be.empty; } else { - expect(job).to.be.equal(null); + expect(job).to.be.equal(undefined); expect(logs.logs).to.be.empty; } }), @@ -176,9 +176,9 @@ describe('workers', function() { jobIds.map(async (jobId, index) => { const job = await newQueue.getJob(jobId); if (index >= datas.length - keepJobs) { - expect(job).to.not.be.equal(null); + expect(job).to.not.be.equal(undefined); } else { - expect(job).to.be.equal(null); + expect(job).to.be.equal(undefined); } }), ); @@ -215,7 +215,7 @@ describe('workers', function() { await queue .getJob(jobId) .then(job => { - expect(job).to.be.equal(null); + expect(job).to.be.equal(undefined); return null; }) .then(() => { @@ -248,7 +248,7 @@ describe('workers', function() { return new Promise((resolve, reject) => { worker.on('failed', async jobId => { const job = await newQueue.getJob(jobId); - expect(job).to.be.equal(null); + expect(job).to.be.equal(undefined); const counts = await newQueue.getJobCounts('completed'); expect(counts.completed).to.be.equal(0); await worker.close(); @@ -285,9 +285,9 @@ describe('workers', function() { jobIds.map(async (jobId, index) => { const job = await queue.getJob(jobId); if (index >= datas.length - keepJobs) { - expect(job).to.not.be.equal(null); + expect(job).to.not.be.equal(undefined); } else { - expect(job).to.be.equal(null); + expect(job).to.be.equal(undefined); } }), ); @@ -330,9 +330,9 @@ describe('workers', function() { jobIds.map(async (jobId, index) => { const job = await newQueue.getJob(jobId); if (index >= datas.length - keepJobs) { - expect(job).to.not.be.equal(null); + expect(job).to.not.be.equal(undefined); } else { - expect(job).to.be.equal(null); + expect(job).to.be.equal(undefined); } }), ); From 57162796a089d76635edf017798190d38b7a9702 Mon Sep 17 00:00:00 2001 From: Robert D'Ercole Date: Tue, 11 Feb 2020 14:16:41 -0500 Subject: [PATCH 4/5] test: re-added getJob test --- src/test/test_getters.ts | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/test/test_getters.ts b/src/test/test_getters.ts index 5983acbad6..6a1d9ebc78 100644 --- a/src/test/test_getters.ts +++ b/src/test/test_getters.ts @@ -69,18 +69,13 @@ describe('Jobs getters', function() { await worker.close(); }); - /* - it('should get a specific job', function(done) { - var data = { foo: 'sup!' }; - queue.add(data).then(function(job) { - queue.getJob(job.id).then(function(returnedJob) { - expect(returnedJob.data).to.eql(data); - expect(returnedJob.id).to.be.eql(job.id); - done(); - }); - }); + it('should get a specific job', async () => { + const data = { foo: 'sup!' }; + const job = await queue.add('test', data); + const returnedJob = await queue.getJob(job.id); + expect(returnedJob.data).to.eql(data); + expect(returnedJob.id).to.be.eql(job.id); }); - */ it('should get completed jobs', async () => { const worker = new Worker(queueName, async job => {}); From 102d7f705dc28ce0dd467bed46d2e5c4e3523ee8 Mon Sep 17 00:00:00 2001 From: Robert D'Ercole Date: Tue, 11 Feb 2020 14:26:21 -0500 Subject: [PATCH 5/5] test: added test for undefined getJob --- src/test/test_getters.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/test/test_getters.ts b/src/test/test_getters.ts index 6a1d9ebc78..82703ace26 100644 --- a/src/test/test_getters.ts +++ b/src/test/test_getters.ts @@ -77,6 +77,11 @@ describe('Jobs getters', function() { expect(returnedJob.id).to.be.eql(job.id); }); + it('should get undefined for nonexistent specific job', async () => { + const returnedJob = await queue.getJob('test'); + expect(returnedJob).to.be.equal(undefined); + }); + it('should get completed jobs', async () => { const worker = new Worker(queueName, async job => {}); let counter = 2;