Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate restore script to promises #32

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
language: nodejs
language: node_js

cache:
directories:
- "node_modules"

branches:
only:
- master

install:
- npm install

script:
- npm test

node_js:
- "6"
- "7"
100 changes: 65 additions & 35 deletions bin/dynamo-restore.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,78 @@ var sleep = require('sleep');

var argv = utils.config({
demand: ['table'],
optional: ['rate', 'key', 'secret', 'region'],
optional: ['rate', 'key', 'secret', 'region', 'v'],
usage: 'Restores Dynamo DB table from JSON file\n' +
'Usage: dynamo-archive --table my-table [--rate 100] [--region us-east-1] [--key AK...AA] [--secret 7a...IG]'
'Usage: dynamo-archive --table my-table [-v] [--rate 100] [--region us-east-1] [--key AK...AA] [--secret 7a...IG]'
});

delay = t => new Promise(resolve => { setTimeout(resolve, t) })

var dynamo = utils.dynamo(argv);
dynamo.describeTable(
{
TableName: argv.table
},
function (err, data) {
if (err != null) {
throw err;
}
if (data == null) {
throw 'Table ' + argv.table + ' not found in DynamoDB';
}

dynamo.describeTable({ TableName: argv.table }, (err, data) => {
if (err != null) throw err
if (data == null) throw 'Table ' + argv.table + ' not found in DynamoDB'

var quota = data.Table.ProvisionedThroughput.WriteCapacityUnits;
var start = Date.now();
var msecPerItem = Math.round(1000 / quota / ((argv.rate || 100) / 100));
var done = 0;
readline.createInterface(process.stdin, process.stdout).on(
var promises = []
var counter = 0

readline.createInterface({input: process.stdin, terminal: false, output: process.stdout}).on(
'line',
function(line) {
dynamo.putItem(
{
TableName: argv.table,
Item: JSON.parse(line)
},
function (err, data) {
if (err) {
console.log(err, err.stack);
throw err;
line => {
promises.push(() => new Promise((resolve, reject) => {
dynamo.putItem({
TableName: argv.table,
Item: JSON.parse(line)
}, (err, data) => {
if (err) {
console.log(err, err.stack);
return reject(err)
}

resolve()
}
}
);
++done;
var expected = start + msecPerItem * done;
if (expected > Date.now()) {
sleep.usleep((expected - Date.now()) * 1000);
}
);
}))
}
);
).on('close', function() {
argv.v && console.log(`Total promises: ${promises.length}`)

var batches = promises.reduce((set, promise) => {
var current = set.length - 1
set[current].push(promise)
if (set[current].length == quota) set.push([])

return set
}, [[]])

var promiseBatches = batches.map(batch => () => {
if (batch.length == 0) return Promise.resolve()

return delay(1000)
.then(() => Promise.all(batch.map(promise => promise())) )
.then(() => { argv.v && console.log(`Processed a batch with ${batch.length} items`) })
})

argv.v && console.log(`Processing ${promiseBatches.length} batches of promises, up to ${quota} promises each`)

Promise.series(promiseBatches, {}).then(() => {
argv.v && console.log('All done, hooray!')
}).catch(err => {
console.log(err)
})
});
}
);

Promise.series = function(promises, initValue) {
return promises.reduce(function(chain, promise) {
if (typeof promise !== "function") {
return Promise.reject(
new Error("Error: Invalid promise item: " + promise)
)
}
return chain.then(promise)
}, Promise.resolve(initValue))
}