DynamoDBLocal:如何启动和停止数据库?

DynamoDBLocal : How can I start and stop DB?

我有一个 "dynamodb_helper.js" 并且我使用 local-dynamo 在本地模拟 DynamoDB。 我能够在测试前成功启动服务器并 运行 它,但 stopDB() 似乎不起作用。任何 help/suggestions 将不胜感激。所以即使在测试结束后,服务器子进程仍然保持 运行ning。

请在下面找到我的代码。

    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));
        }
    });
  }
};

在顶部添加:-

var treeKill = require('tree-kill');

如下所述更改 stopDB:-

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));
        }

    });
},

使用 treeKill 终止进程,因为 "kill" 方法并不能真正终止进程。

Note that while the function is called kill, the signal delivered to the child process may not actually terminate the process.

Child Process Kill Reference Document