UNPKG

kinesis-client-library

Version:

Process Kinesis streams and automatically scale up or down as shards split or merge.

101 lines (100 loc) 3.63 kB
"use strict"; var os_1 = require('os'); var async_1 = require('async'); var vogels_1 = require('vogels'); var factory_1 = require('../aws/factory'); var createModel = function (tableName, dynamodb) { var Cluster = vogels_1.define('Cluster', function (schema) { schema.String('type', { hashKey: true }); schema.String('id', { rangeKey: true }); schema.Number('activeConsumers').required(); schema.Number('expiresAt').required(); }); Cluster.config({ tableName: tableName, dynamodb: dynamodb, }); return Cluster; }; var Cluster = (function () { function Cluster(tableName, conf, dynamoEndpoint) { this.id = [os_1.hostname(), process.pid, Date.now()].join('@'); var dynamodb = factory_1.createDynamoClient(conf, dynamoEndpoint); this.Cluster = createModel(tableName, dynamodb); } Cluster.prototype.reportActiveConsumers = function (activeConsumers, callback) { this.Cluster.update({ type: Cluster.DB_TYPE, id: this.id, expiresAt: Date.now() + (1000 * 15), activeConsumers: activeConsumers, }, callback); }; Cluster.prototype.fetchAll = function (callback) { this.Cluster.query(Cluster.DB_TYPE) .filter('expiresAt').gt(Date.now()) .loadAll() .exec(callback); }; Cluster.prototype.garbageCollect = function (callback) { var _this = this; this.Cluster.query(Cluster.DB_TYPE) .filter('expiresAt').lt(Date.now()) .loadAll() .exec(function (err, clusters) { if (err) { return callback(err); } async_1.each(clusters.Items, function (cluster, done) { _this.Cluster.destroy('cluster', cluster.get('id'), done); }, function (err) { if (err) { return callback(err); } callback(null, clusters.Items); }); }); }; Cluster.createTable = function (name, conf, capacity, dynamoEndpoint, callback) { var dynamodb = factory_1.createDynamoClient(conf, dynamoEndpoint); var model = createModel(name, dynamodb); var tableStatus; model.createTable({ readCapacity: capacity.read || Cluster.DefaultCapacity.READ, writeCapacity: capacity.write || Cluster.DefaultCapacity.WRITE, }, function (err) { if (err) { return callback(err); } async_1.doUntil(function (done) { model.describeTable(function (err, data) { if (err) { return done(err); } tableStatus = data.Table.TableStatus; done(); }); }, function () { return tableStatus === 'ACTIVE'; }, callback); }); }; Cluster.tableExists = function (name, conf, dynamoEndpoint, callback) { var dynamodb = factory_1.createDynamoClient(conf, dynamoEndpoint); createModel(name, dynamodb).describeTable(function (err) { if (err && err.code === 'ResourceNotFoundException') { callback(null, false); } else if (err) { callback(err); } else { callback(null, true); } }); }; Cluster.DefaultCapacity = { READ: 10, WRITE: 10 }; Cluster.DB_TYPE = 'cluster'; return Cluster; }()); exports.Cluster = Cluster;