Skip to content

Commit

Permalink
fix: not running jobs even though concurrency is not reached
Browse files Browse the repository at this point in the history
  • Loading branch information
simllll committed Oct 14, 2020
1 parent a70f500 commit 0e82025
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 7 deletions.
10 changes: 3 additions & 7 deletions src/JobProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ export class JobProcessor {
job.attrs.name,
job.attrs._id
);
this.runOrRetry();
this.runOrRetry(job);
} else {
const runIn = job.attrs.nextRunAt.getTime() - now.getTime();
log.extend('jobProcessing')(
Expand All @@ -380,7 +380,7 @@ export class JobProcessor {
* Internal method that tries to run a job and if it fails, retries again!
* @returns {undefined}
*/
private async runOrRetry() {
private async runOrRetry(job: Job) {
if (!this.isRunning) {
// const a = new Error();
// console.log('STACK', a.stack);
Expand All @@ -391,11 +391,7 @@ export class JobProcessor {
return;
}

const job = this.jobQueue.pop();
if (!job) {
console.info('empty queue');
return;
}
this.jobQueue.remove(job);

const jobDefinition = this.agenda.definitions[job.attrs.name];
const status = this.jobStatus[job.attrs.name];
Expand Down
82 changes: 82 additions & 0 deletions test/jobprocessor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import * as expect from 'expect.js';

import { Db } from 'mongodb';
import { Agenda } from '../src';
import { mockMongo } from './helpers/mock-mongodb';

// Create agenda instances
let agenda: Agenda;
// mongo db connection db instance
let mongoDb: Db;

const clearJobs = async () => {
if (mongoDb) {
await mongoDb.collection('agendaJobs').deleteMany({});
}
};

describe('Agenda', function () {
// this.timeout(1000000);

beforeEach(async () => {
if (!mongoDb) {
const mockedMongo = await mockMongo();
// mongoCfg = mockedMongo.uri;
mongoDb = mockedMongo.mongo.db();
}

return new Promise(resolve => {
agenda = new Agenda(
{
mongo: mongoDb,
maxConcurrency: 4,
defaultConcurrency: 1,
lockLimit: 15,
defaultLockLimit: 6,
processEvery: '1 second'
},
async () => {
await clearJobs();
return resolve();
}
);
});
});

afterEach(async () => {
await agenda.stop();
await clearJobs();
});

describe('configuration methods', () => {
it('ensure new jobs are always filling up running queue', async () => {
let shortOneFinished = false;

agenda.define('test long', async () => {
await new Promise(resolve => setTimeout(resolve, 1000));
});
agenda.define('test short', async () => {
shortOneFinished = true;
await new Promise(resolve => setTimeout(resolve, 5));
});

await agenda.start();

// queue up long ones
for (let i = 0; i < 100; i++) {
agenda.now('test long');
}

await new Promise(resolve => setTimeout(resolve, 1000));

// queue more short ones (they should complete first!)
for (let j = 0; j < 100; j++) {
agenda.now('test short');
}

await new Promise(resolve => setTimeout(resolve, 1000));

expect(shortOneFinished).to.be(true);
});
});
});

0 comments on commit 0e82025

Please sign in to comment.