Skip to content

Commit

Permalink
make sure waitForActive waits but doesn't hang
Browse files Browse the repository at this point in the history
  • Loading branch information
brandongoode committed Oct 24, 2016
1 parent 87ec990 commit b4c4dc5
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions lib/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ var compareIndexes = function compareIndexes(local, remote) {
var i;
var j;

var localIndexes = localTableReq.GlobalSecondaryIndexes;
var remoteIndexes = remoteTableReq.GlobalSecondaryIndexes;
var localIndexes = localTableReq.GlobalSecondaryIndexes || [];
var remoteIndexes = remoteTableReq.GlobalSecondaryIndexes || [];

debug('compareIndexes');
// let's see what remote indexes we need to sync or create
Expand Down Expand Up @@ -168,11 +168,13 @@ Table.prototype.init = function (next) {
deferred.reject('indexes are not synchronized and update flag is set to false');
}
}
table.waitForActive();
//table.active = data.Table.TableStatus === 'ACTIVE';
table.initialized = true;
return table.waitForActive()
.then(function () {
//table.active = data.Table.TableStatus === 'ACTIVE';

return deferred.resolve();
return deferred.resolve();
});
})
.catch(function (err) {
if (err && err.code === 'ResourceNotFoundException') {
Expand All @@ -182,15 +184,15 @@ Table.prototype.init = function (next) {
.then(function () {
table.initialized = true;
})
// .then(function() {
// if(table.options.waitForActive) {
// return table.waitForActive();
// }
// })
.then(function() {
if(table.options.waitForActive) {
return table.waitForActive();
}
})
);
}
if (err) {
debug('error initializing', err);
debug('error initializing', err.stack);
return deferred.reject(err);
}
});
Expand Down Expand Up @@ -219,6 +221,7 @@ Table.prototype.waitForActive = function (timeout, next) {
var timeoutAt = Date.now() + timeout;

function waitForActive() {
debug('Waiting...');
/*
if (table.active) {
debug('Table flag is set to Active - %s', table.name);
Expand All @@ -235,7 +238,8 @@ Table.prototype.waitForActive = function (timeout, next) {
table.describe()
.then(function (data) {
var active = (data.Table.TableStatus === 'ACTIVE');
data.Table.GlobalSecondaryIndexes.forEach(function (gsi) {
var indexes = data.Table.GlobalSecondaryIndexes || [];
indexes.forEach(function (gsi) {
//debug('waitForActive Index Check: %s', JSON.stringify(gsi, null, 2));
debug('index %s.IndexStatus is %s', gsi.IndexName, gsi.IndexStatus);
if (gsi.IndexStatus !== 'ACTIVE') {
Expand All @@ -251,6 +255,10 @@ Table.prototype.waitForActive = function (timeout, next) {
}
})
.catch(function (err) {
if (err && err.code === 'ResourceNotFoundException') {
return setTimeout(waitForActive, 10);
}
debug('Error waiting for active', err.stack);
return deferred.reject(err);
});
}
Expand Down

0 comments on commit b4c4dc5

Please sign in to comment.