-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Mickael Alibert <mickael.alibert@pix.fr> Co-authored-by: Emeric Martineau <eric.martineau@pix.fr>
- Loading branch information
1 parent
a5964e1
commit 972f884
Showing
5 changed files
with
208 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const parisTimezone = 'Europe/Paris'; | ||
const CronJob = require('cron').CronJob; | ||
const logger = require('../../common/services/logger'); | ||
|
||
const taskScheluder = function (tasksList) { | ||
const scheduleTask = ({ schedule, handler }) => { | ||
new CronJob({ | ||
cronTime: schedule, | ||
onTick: handler, | ||
onComplete: null, | ||
start: true, | ||
timezone: parisTimezone, | ||
}); | ||
}; | ||
|
||
tasksList.forEach(({ name, schedule, enabled, handler }) => { | ||
if (enabled) { | ||
logger.info({ event: 'task-scheduler', message: `task ${name} scheduled ${schedule}` }); | ||
scheduleTask({ | ||
schedule, | ||
handler, | ||
}); | ||
} else { | ||
logger.info({ event: 'task-scheduler', message: `task ${name} not scheduled` }); | ||
} | ||
}); | ||
}; | ||
|
||
module.exports = taskScheluder; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
const { expect, sinon } = require('../../../test-helper'); | ||
const logger = require('../../../../common/services/logger'); | ||
const taskScheluder = require('../../../../run/services/task-scheduler'); | ||
|
||
describe('Integration | Run | Services | schedule-task', function () { | ||
let clock; | ||
const ONE_SECOND = 1 * 10 ** 3; | ||
|
||
beforeEach(function () { | ||
clock = sinon.useFakeTimers(); | ||
}); | ||
|
||
afterEach(function () { | ||
clock.restore(); | ||
sinon.restore(); | ||
}); | ||
|
||
describe('#ScheduleTask', function () { | ||
it('should schedule enabled tasks', function () { | ||
// given | ||
const loggerInfoStub = sinon.stub(logger, 'info'); | ||
|
||
const runStub = sinon.stub(); | ||
const testTasks = [ | ||
{ | ||
name: 'task1', | ||
enabled: true, | ||
schedule: '* * * * * *', | ||
handler: runStub, | ||
}, | ||
]; | ||
|
||
// when | ||
taskScheluder(testTasks); | ||
|
||
// then | ||
expect(runStub).to.not.have.been.called; | ||
|
||
// when | ||
clock.tick(ONE_SECOND - 1); | ||
|
||
// then | ||
expect(runStub).to.not.have.been.called; | ||
|
||
// when | ||
clock.tick(1); | ||
|
||
// then | ||
expect(runStub).to.have.been.calledOnce; | ||
|
||
expect(loggerInfoStub.calledOnce).to.be.true; | ||
expect(loggerInfoStub.firstCall.args[0]).to.deep.equal({ | ||
event: 'task-scheduler', | ||
message: 'task task1 scheduled * * * * * *', | ||
}); | ||
}); | ||
|
||
it('should not schedule tasks', function () { | ||
// given | ||
const loggerInfoStub = sinon.stub(logger, 'info'); | ||
|
||
const runStub = sinon.stub(); | ||
const testTasks = [ | ||
{ | ||
name: 'task1', | ||
enabled: false, | ||
schedule: '* * * * * *', | ||
handler: runStub, | ||
}, | ||
]; | ||
|
||
// when | ||
taskScheluder(testTasks); | ||
// then | ||
expect(runStub).to.not.have.been.called; | ||
|
||
// when | ||
clock.tick(ONE_SECOND - 1); | ||
|
||
// then | ||
expect(runStub).to.not.have.been.called; | ||
|
||
// when | ||
clock.tick(1); | ||
|
||
// then | ||
expect(runStub).to.not.have.been.called; | ||
|
||
expect(loggerInfoStub.calledOnce).to.be.true; | ||
|
||
expect(loggerInfoStub.firstCall.args[0]).to.deep.equal({ | ||
event: 'task-scheduler', | ||
message: 'task task1 not scheduled', | ||
}); | ||
}); | ||
}); | ||
}); |