kinesis-client-library
Version:
Process Kinesis streams and automatically scale up or down as shards split or merge.
81 lines (80 loc) • 2.94 kB
JavaScript
;
var vogels_1 = require('vogels');
var underscore_1 = require('underscore');
var factory_1 = require('../aws/factory');
var createModel = function (tableName, dynamodb) {
var Lease = vogels_1.define('Lease', function (schema) {
schema.String('type', { hashKey: true });
schema.String('id', { rangeKey: true });
schema.Number('leaseCounter').required();
schema.Number('expiresAt').required();
schema.String('checkpointedSequence');
schema.Boolean('isFinished');
});
Lease.config({
tableName: tableName,
dynamodb: dynamodb,
});
return Lease;
};
var Lease = (function () {
function Lease(shardId, counter, table, conf, dynamoEndpoint) {
var dynamodb = factory_1.createDynamoClient(conf, dynamoEndpoint);
this.Lease = createModel(table, dynamodb);
this.shardId = shardId;
this.expectedLeaseCounter = counter;
}
Lease.prototype.getCheckpoint = function (callback) {
this.Lease.get(Lease.DB_TYPE, this.shardId, {
ConsistentRead: true,
AttributesToGet: ['checkpointedSequence'],
}, function (err, lease) {
if (err) {
return callback(err);
}
callback(null, lease.get('checkpointedSequence'));
});
};
Lease.prototype.update = function (properties, callback) {
var _this = this;
var atts = underscore_1.extend({
type: Lease.DB_TYPE,
id: this.shardId,
leaseCounter: { $add: 1 },
expiresAt: Date.now() + (1000 * 15),
}, properties);
var expected = {
expected: { leaseCounter: this.expectedLeaseCounter },
};
this.expectedLeaseCounter = (this.expectedLeaseCounter || 0) + 1;
this.Lease.update(atts, expected, function (err, record) {
if (!err) {
_this.checkpointedSequence = record.get('checkpointedSequence');
}
callback(err);
});
};
Lease.prototype.reserve = function (callback) {
this.update({}, callback);
};
Lease.prototype.checkpoint = function (checkpointedSequence, callback) {
// Skip redundant writes
if (checkpointedSequence === this.checkpointedSequence) {
return process.nextTick(callback);
}
this.update({ checkpointedSequence: checkpointedSequence }, callback);
};
Lease.prototype.markFinished = function (callback) {
this.update({ isFinished: true }, callback);
};
Lease.fetchAll = function (tableName, conf, dynamoEndpoint, callback) {
var dynamodb = factory_1.createDynamoClient(conf, dynamoEndpoint);
createModel(tableName, dynamodb)
.query(Lease.DB_TYPE)
.loadAll()
.exec(callback);
};
Lease.DB_TYPE = 'lease';
return Lease;
}());
exports.Lease = Lease;