I have a "dynamodb_helper.js" and I use the local-dynamo to emulate DynamoDB locally. I'm able to start the server and run it successfully before my test, but the stopDB() does not seem to work. Any help/suggestions would be much appreciated. So the server child-process still keeps running even after the test ends.
Please find my code below.
var AWS = require("aws-sdk");
var localDynamo = require('local-dynamo');
var dynamoServer = null;
var dynamodb = null;
var dynamo = null;
module.exports = {
startDB: function() {
dynamoServer = localDynamo.launch({
port: 4567,
detached: true,
heap: '512m',
stdio: 'pipe'
})
var awsConfig = require('../lib/util').ymlParser('aws');
AWS.config.update(awsConfig);
AWS.config.update({
accessKeyId: "TestData",
secretAccessKey: "TestData",
region: "localhost",
endpoint: "http://localhost:4567"
});
dynamodb = new AWS.DynamoDB();
dynamo = dynamodb.DocumentClient;
},
stopDB: function() {
setTimeout(function () {
dynamoServer.kill();
}, 1000)
},
createTable: function() {
var params = {
TableName : "Movies",
KeySchema: [
{ AttributeName: "year", KeyType: "HASH"}, //Partition key
{ AttributeName: "title", KeyType: "RANGE" } //Sort key
],
AttributeDefinitions: [
{ AttributeName: "year", AttributeType: "N" },
{ AttributeName: "title", AttributeType: "S" }
],
ProvisionedThroughput: {
ReadCapacityUnits: 10,
WriteCapacityUnits: 10
}
};
dynamodb.createTable(params, function(err, data) {
if (err) {
console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
}
});
}
};
Add at the top:-
var treeKill = require('tree-kill');
Change the stopDB as mentioned below:-
stopDB : function() {
console.log("Stop the dynamodb");
console.log("Dynamodb pid number :" + dynamoServer.pid);
treeKill(dynamoServer.pid, 'SIGTERM', function(err) {
if (err === null) {
console.log("Dynamodb process has been killed");
} else {
console.log("Dynamodb process hasn't been killed : " + JSON.stringify(err));
}
});
},
Use treeKill to kill the process as "kill" method doesn't really kill the process.
Note that while the function is called kill, the signal delivered to the child process may not actually terminate the process.