0

我有一个“dynamodb_helper.js”,我使用local-dynamo在本地模拟 DynamoDB。我能够在测试之前启动服务器并成功运行它,但是 stopDB() 似乎不起作用。任何帮助/建议将不胜感激。因此,即使在测试结束后,服务器子进程仍会继续运行。

请在下面找到我的代码。

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

1 回答 1

1

在顶部添加:-

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”方法并没有真正杀死进程。

请注意,虽然该函数被称为 kill,但传递给子进程的信号实际上可能不会终止该进程。

子进程终止参考文档

于 2016-08-10T12:02:15.493 回答