3

我有一个 node.js 脚本,它创建一个到加密交易网站 (bitmex) 的 websocket 连接。它流式传输价格数据。

出于某种原因,在一两个小时后,流变坏了,价格(如果有流的话)不准确。

现在我每小时手动重新启动一次,但我需要自动完成。我怎样才能做到这一点?

这是脚本的代码:

var WebSocket = require('ws');
var ws = new WebSocket("wss://www.bitmex.com/realtime");

var couchbase = require('couchbase')
var cluster = new couchbase.Cluster('couchbase://localhost/');
cluster.authenticate('xxxxxx', 'xxxxxxx');
var bucket = cluster.openBucket('test');
var N1qlQuery = couchbase.N1qlQuery;

let num_msg = 0;        
ws.onopen = function(){
    ws.send(JSON.stringify({"op": "subscribe", "args": [    
    "trade:XBTUSD",
    "trade:LTCZ18"]
    }))
};
ws.onmessage = function(msg){
    var response = JSON.parse(msg.data);
    num_msg++
    if(num_msg > 50) {
        var coin = response['data'][0]['symbol'];
        var price = response['data'][0]['price'];
        //console.log(coin + ":" + price + "\n");
        bucket.manager().createPrimaryIndex(
            function(){
                bucket.upsert(  coin, 
                                {
                                'price': price
                                },
                                function (err, result){
            });
        });

    }

};

编辑:我没有提到目前我使用的是 Windows 7 系统(尽管我确实需要迁移到 Ubuntu 或类似系统)。

编辑2:我的代码的最终版本:)

const cron = require("node-cron");
var WebSocket = require('ws');
var couchbase = require('couchbase');
var dateTime = require('node-datetime');

let now = new Date();
minutes = now.getMinutes() + 1;
if(minutes + 30 > 59) {
    minutes1 = minutes - 30;
} else {
    minutes1 = minutes - 30;
}
if(minutes > minutes1) {
    s_cron = minutes1 + "," + minutes +  " * * * *";
} else {
    s_cron = minutes + "," + minutes1 +  " * * * *";
}
cron.schedule(s_cron, () => {
    console.log("---------------------");
    console.log("Running Cron Job");
    var dt = dateTime.create();
    var formatted = dt.format('Y-m-d H:M:S');
    console.log(formatted);    
    // create bitmex ws
        var ws = new WebSocket("wss://www.bitmex.com/realtime");
        // connect to couchbase
        var cluster = new couchbase.Cluster('couchbase://localhost/');
        cluster.authenticate('xxxxxxxx', 'xxxxxxxxxx');
        var bucket = cluster.openBucket('test');
        var N1qlQuery = couchbase.N1qlQuery;

        let num_msg = 0;        
        ws.onopen = function(){
            ws.send(JSON.stringify({"op": "subscribe", "args": [    
                "trade:XBTUSD",
                "trade:LTCZ18"]
            }))
        };
        ws.onmessage = function(msg){
            var response = JSON.parse(msg.data);
            num_msg++
            if(num_msg > 50) {
                var coin = response['data'][0]['symbol'];
                var price = response['data'][0]['price'];
                //console.log(coin + ":" + price + "\n");
                bucket.manager().createPrimaryIndex(
                    function(){
                        bucket.upsert(  coin, 
                                        {
                                        'price': price
                                        },
                                        function (err, result){
                                            //bucket.disconnect()
                    });
                });

            }

        };
  });
4

2 回答 2

2

尝试 'node-cron':https ://www.npmjs.com/package/node-cron 上的更多内容希望可行。

于 2018-10-08T18:02:44.750 回答
1

考虑使用cron每小时重新启动。您的crontab条目将如下所示:

0 * * * * <command to restart your app>

如果您不能或不想使用您的系统crontab或等效系统(不确定在 Windows 上会是什么),您可以使用pm2.

pm2https ://www.npmjs.com/package/pm2

有关如何pm2每小时重启一次,请参阅https://github.com/Unitech/pm2/issues/1076https://stackoverflow.com/a/38062307/436641

另一种选择是node-cronhttps ://www.npmjs.com/package/node-cron

于 2018-10-08T17:53:43.103 回答