forked from fortunejs/fortune
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #202 from agco/add-checkpoint-writer
Add checkpoint writer
- Loading branch information
Showing
3 changed files
with
179 additions
and
42 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict' | ||
const EventEmitter = require('events'); | ||
const debug = require('debug')('events-reader'); | ||
let writerLoopStopped = true; | ||
let lastDoc; | ||
let lastCheckpointId; | ||
let harvesterApp; | ||
|
||
const checkpointEventEmitter = new EventEmitter(); | ||
|
||
checkpointEventEmitter.on('newCheckpoint', (checkpointId, doc) => { | ||
lastCheckpointId = checkpointId; | ||
lastDoc = doc; | ||
}); | ||
|
||
const persistLastCheckpoint = () => { | ||
if(lastDoc && lastCheckpointId) { | ||
harvesterApp.adapter.update('checkpoint', lastCheckpointId, {ts: lastDoc.ts}) | ||
.then(checkpoint => { | ||
debug('last written checking point ' + checkpoint.ts); | ||
}) | ||
.catch(error => { | ||
console.log(error); | ||
process.exit(1); | ||
}); | ||
}; | ||
lastCheckpointId = undefined; | ||
lastDoc = undefined; | ||
}; | ||
|
||
const persistInInterval = (ms) => { | ||
setInterval(() => { | ||
persistLastCheckpoint(); | ||
}, ms); | ||
}; | ||
|
||
const startWriterLoop = app => { | ||
harvesterApp = app; | ||
const defaultWriteInterval = 1; | ||
const writeInterval = parseInt((harvesterApp.options && harvesterApp.options.eventsReaderDebounceWait) || defaultWriteInterval); | ||
if(writerLoopStopped) { | ||
persistInInterval(writeInterval); | ||
writerLoopStopped = false; | ||
}; | ||
}; | ||
|
||
const getLastCheckpointId = () => lastCheckpointId; | ||
|
||
const getLastDoc = () => lastDoc; | ||
|
||
const setWriterLoopStopped = shouldStop => writerLoopStopped = shouldStop; | ||
|
||
module.exports = { | ||
startWriterLoop: startWriterLoop, | ||
checkpointEventEmitter: checkpointEventEmitter, | ||
getLastCheckpointId: getLastCheckpointId, | ||
getLastDoc: getLastDoc, | ||
setWriterLoopStopped: setWriterLoopStopped | ||
}; |
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,113 @@ | ||
'use strict' | ||
const expect = require('chai').expect; | ||
const Joi = require('joi'); | ||
const sinon = require('sinon'); | ||
const checkpointWriter = require('../lib/events-reader-checkpoint-writer'); | ||
const Promise = require('bluebird'); | ||
|
||
describe('checkpoint writer', function () { | ||
|
||
describe('timeout', function () { | ||
context('when using the default config options', () => { | ||
const harvestApp = { | ||
adapter: { | ||
update: () => {} | ||
} | ||
}; | ||
const fakeDoc = {ts: 1}; | ||
const checkpointEventEmitter = checkpointWriter.checkpointEventEmitter; | ||
|
||
let clock; | ||
|
||
beforeEach(() => { | ||
clock = sinon.useFakeTimers(); | ||
sinon.stub(harvestApp.adapter, 'update'); | ||
harvestApp.adapter.update.returns(new Promise.resolve(fakeDoc)); | ||
checkpointWriter.startWriterLoop(harvestApp); | ||
checkpointWriter.setWriterLoopStopped(true); | ||
checkpointEventEmitter.emit('newCheckpoint', 1, fakeDoc); | ||
}); | ||
|
||
afterEach(() => { | ||
harvestApp.adapter.update.restore(); | ||
clock.restore(); | ||
}); | ||
|
||
it('should clean last doc and checkpoint after handled', done => { | ||
clock.tick(1); | ||
expect(harvestApp.adapter.update.callCount).to.be.eql(1); | ||
clock.tick(1); | ||
expect(checkpointWriter.getLastDoc()).to.be.undefined; | ||
expect(checkpointWriter.getLastCheckpointId()).to.be.undefined; | ||
expect(harvestApp.adapter.update.calledOnce).to.be.true; | ||
|
||
done(); | ||
}); | ||
|
||
it('should write a checkpoint in a given interval', done => { | ||
clock.tick(1); | ||
expect(harvestApp.adapter.update.callCount).to.be.eql(1); | ||
|
||
checkpointEventEmitter.emit('newCheckpoint', 1, fakeDoc); | ||
clock.tick(1); | ||
expect(harvestApp.adapter.update.callCount).to.be.eql(2); | ||
|
||
done(); | ||
}); | ||
|
||
it('should debounce excessive checkpoint update function calls', done => { | ||
checkpointEventEmitter.emit('newCheckpoint', 1, fakeDoc); | ||
checkpointEventEmitter.emit('newCheckpoint', 1, fakeDoc); | ||
checkpointEventEmitter.emit('newCheckpoint', 1, fakeDoc); | ||
checkpointEventEmitter.emit('newCheckpoint', 1, fakeDoc); | ||
clock.tick(1); | ||
expect(harvestApp.adapter.update.callCount).to.be.eql(1); | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
context('when passing the option eventsReaderDebounceWait', () => { | ||
const eventsReaderDebounceDelay = 1000; | ||
const harvestApp = { | ||
adapter: { | ||
update: () => {} | ||
}, | ||
options: { | ||
eventsReaderDebounceWait: eventsReaderDebounceDelay | ||
} | ||
}; | ||
const fakeDoc = {ts: 1}; | ||
const checkpointEventEmitter = checkpointWriter.checkpointEventEmitter; | ||
|
||
let clock; | ||
|
||
beforeEach(() => { | ||
clock = sinon.useFakeTimers(); | ||
sinon.stub(harvestApp.adapter, 'update'); | ||
harvestApp.adapter.update.returns(new Promise.resolve(fakeDoc)); | ||
checkpointWriter.startWriterLoop(harvestApp); | ||
checkpointWriter.setWriterLoopStopped(true); | ||
checkpointEventEmitter.emit('newCheckpoint', 1, fakeDoc); | ||
}); | ||
|
||
afterEach(() => { | ||
harvestApp.adapter.update.restore(); | ||
clock.restore(); | ||
}); | ||
|
||
it('should write a checkpoint in a given interval', done => { | ||
clock.tick(eventsReaderDebounceDelay); | ||
expect(harvestApp.adapter.update.callCount).to.be.eql(1); | ||
|
||
checkpointEventEmitter.emit('newCheckpoint', 1, fakeDoc); | ||
clock.tick(eventsReaderDebounceDelay); | ||
expect(harvestApp.adapter.update.callCount).to.be.eql(2); | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
}); |