Last active
September 30, 2016 20:40
-
-
Save virgilioneto/de2720b49cb298c13ddd3e7ee2820b90 to your computer and use it in GitHub Desktop.
NodeJS Agenda module concurrency test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Agenda = require(`agenda`); | |
const agenda = new Agenda({db: {address: "MONGODB_CONNECTION_STRING_HERE"}}); | |
/** | |
* Concurrency set to 100 | |
*/ | |
agenda.maxConcurrency(100); | |
/** | |
* Lock life time set to 2 seconds | |
*/ | |
agenda.defaultLockLifetime(2000); | |
/** | |
* Lock limit set to 100 | |
*/ | |
agenda.defaultLockLimit(100); | |
/** | |
* Object to control how many job are running | |
* @type {{test: number, test2: number, test3: number}} | |
*/ | |
var o = { | |
test: 0, | |
test2: 0, | |
test3: 0 | |
}; | |
/** | |
* Job 'test' | |
* Concurrency set to 5 | |
* LockLimit set to 5 | |
* | |
* Internal job code validate if running count (o.test) is equal 4 to skip the 5th execution | |
* | |
* The job execution will end 10 seconds before it starts to simulate concurrent jobs | |
*/ | |
agenda.define(`test`, {concurrency: 5, lockLimit: 5}, (job, done) => { | |
if (o.test == 4) { | |
console.log(`TEST SKIPPED`); | |
return done(); | |
} | |
o.test += 1; | |
console.log(`-> test (${o.test})`, new Date()); | |
setTimeout(() => { | |
o.test -= 1; | |
console.warn(`-> test (${o.test}) DONE!`); | |
done(); | |
}, 10000) | |
}); | |
/** | |
* Job 'test2' | |
* Concurrency set to 5 | |
* LockLimit set to 5 | |
* | |
* Internal job code validate if running count (o.test2) is equal 4 to skip the 5th execution | |
* | |
* The job execution will end 10 seconds before it starts to simulate concurrent jobs | |
*/ | |
agenda.define(`test2`, {concurrency: 5, lockLimit: 5}, (job, done) => { | |
if (o.test2 == 4) { | |
console.log(`TEST2 SKIPPED`); | |
return done(); | |
} | |
o.test2 += 1; | |
console.log(`--> test2 (${o.test2})`, new Date()); | |
setTimeout(() => { | |
o.test2 -= 1; | |
console.warn(`-> test2 (${o.test2}) DONE!`); | |
done(); | |
}, 10000) | |
}); | |
/** | |
* Job 'test3' | |
* Concurrency set to 0 | |
* LockLimit set to 1 | |
* | |
* Internal job code validate if running count (o.test3) is equal 1 to skip the 2nd execution | |
* | |
* The job execution will end 10 seconds before it starts to simulate concurrent jobs | |
*/ | |
agenda.define(`test3`, {concurrency: 0, lockLimit: 1}, (job, done) => { | |
if (o.test3 == 1) { | |
console.log(`TEST3 SKIPPED`); | |
return done(); | |
} | |
o.test3 += 1; | |
console.log(`---> test3 (${o.test3})`, new Date()); | |
setTimeout(() => { | |
o.test3 -= 1; | |
console.warn(`-> test3 (${o.test3}) DONE!`); | |
done(); | |
}, 10000) | |
}); | |
/** | |
* Interval of 1 second to watch job execution count | |
* If any execution break these rules an exception will be thrown | |
*/ | |
setInterval(() => { | |
if (o.test > 4 || o.test2 > 4 || o.test3 > 1) { | |
throw new Error('Woops!'); | |
} | |
}, 1000); | |
/** | |
* Set all jobs to run every 1 second | |
*/ | |
agenda.on(`ready`, () => { | |
agenda.every(`1 second`, `test`); | |
agenda.every(`1 second`, `test2`); | |
agenda.every(`1 second`, `test3`); | |
agenda.start(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment