Skip to content

Commit

Permalink
Merge pull request #133 from bobdercole/get-job-type
Browse files Browse the repository at this point in the history
fix: modified QueueGetters.getJob and Job.fromId to also return null
  • Loading branch information
manast authored Feb 22, 2020
2 parents 36726bf + 102d7f7 commit 10f493e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
7 changes: 5 additions & 2 deletions src/classes/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,15 @@ export class Job<T = any, R = any> {
return job;
}

static async fromId(queue: QueueBase, jobId: string) {
static async fromId(
queue: QueueBase,
jobId: string,
): Promise<Job | undefined> {
// 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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/classes/queue-getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Job } from './job';
import { clientCommandMessageReg } from './worker';

export class QueueGetters extends QueueBase {
getJob(jobId: string) {
getJob(jobId: string): Promise<Job | undefined> {
return Job.fromId(this, jobId);
}

Expand Down
22 changes: 11 additions & 11 deletions src/test/test_getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,18 @@ 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 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 => {});
Expand Down
2 changes: 1 addition & 1 deletion src/test/test_job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down
24 changes: 12 additions & 12 deletions src/test/test_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
}),
Expand Down Expand Up @@ -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);
}
}),
);
Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}),
);
Expand Down Expand Up @@ -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);
}
}),
);
Expand Down

0 comments on commit 10f493e

Please sign in to comment.