节点永远说节目正在运行,但证据显示不是

我试图运行一个简单的twitterbot使用twit库,并永远 。 机器人本质上看另一个Twitter帐户,并复制它的鸣叫一些修改。

我一直在logging从stream媒体API接收到的tweets,并logging任何错误。

然而,机器人往往停止工作,而没有logging错误,而forever list仍然显示程序正在运行。

 info: Forever processes running data: uid command script forever pid id logfile uptime data: [0] QOmt /usr/bin/nodejs bot.js 13642 13649 /home/ubuntu/.forever/QOmt.log 0:5:25:34.733 

起初,我认为这可能是一个叽叽喳喳stream媒体API的问题 – 即,信息已经停止发送“pipe道”,因为日志文件已经停止logging从TwitterstreamAPI的新消息。

为了validation机器人确实还在运行,我创build了一个每分钟logging一次的heartbeat

 // Heartbeat to make sure the process is still running setInterval(function () { console.logWithDate('Program heartbeat'); }, 60*1000); 

当机器人停止运作时,心跳也停止logging。 然而, forever仍然说机器人正在运行。

任何帮助将非常感激。 我发布了我的机器人的完整代码,以防万一。

 #!/usr/bin/env node "use strict" const fs = require('fs'); const util = require('util'); const Twit = require('twit'); // Logging const error_log_file = fs.createWriteStream(__dirname + '/debug.log', {flags : 'a'}); const log_stdout = process.stdout; console.error = function(d) { error_log_file.write(util.format("\n%s> %s", new Date(), d) + '\n'); log_stdout.write(util.format("\n%s> %s", new Date(), d) + '\n'); }; console.logWithDate = function(d) { log_stdout.write(util.format("\n%s> %s", new Date(), d) + '\n'); } // Heartbeat to make sure the process is still running setInterval(function () { console.logWithDate('Program heartbeat'); }, 60*1000); // Read in twitter secrets file const twitter_secrets = JSON.parse(fs.readFileSync("twitter_secrets.json")); // Connect to twitter const client = new Twit({ consumer_key: twitter_secrets.TWITTER_CONSUMER_KEY, consumer_secret: twitter_secrets.TWITTER_CONSUMER_SECRET, access_token: twitter_secrets.TWITTER_ACCESS_TOKEN_KEY, access_token_secret: twitter_secrets.TWITTER_ACCESS_TOKEN_SECRET, timeouts_ms: 60*1000 }); // Main const stream = client.stream('statuses/filter', {follow: 87818409}); stream.on('tweet', function(event) { if (event.user.id === 87818409) { console.logWithDate("Guardian tweet: " + event.text) client.post( 'statuses/update', {status: misspellRandomWords(event.text)}, function(error, tweet, response) { if (error) { console.error(error); } else { console.logWithDate("Bot tweet: " + tweet.text); // Tweet body. //console.log(response); // Raw response object. } } ); } else { console.logWithDate("Guardian-related tweet: " + event.text) } }); // Log various types of messages for debugging stream.on('limit', function(error) { console.error(error); }); stream.on('disconnect', function(error) { console.error(error); }); stream.on('error', function(error) { console.error(error); }); stream.on('connect', function (conn) { console.logWithDate('connecting') }) stream.on('reconnect', function (reconn, res, interval) { console.logWithDate('reconnecting. statusCode:', res.statusCode) }) /* Helper functions */ function swapRandomLetters(word) { const limit = word.length; const iFirstLetter = Math.floor(Math.random() * limit); var iSecondLetter = Math.floor(Math.random() * limit); while (iFirstLetter === iSecondLetter) { iSecondLetter = Math.floor(Math.random() * limit); } let letters = word.split(""); letters[iFirstLetter] = word[iSecondLetter]; letters[iSecondLetter] = word[iFirstLetter]; return letters.join(""); } function isLink(word) { // Very crude URL check return word.substring(0,4) === "http"; } function misspellRandomWords(sentence) { let words = sentence.split(" "); const limit = words.length; // Choose a first word, filtering out urls var iFirstWord = Math.floor(Math.random() * limit); while (isLink(words[iFirstWord]) || words[iFirstWord][0] === "@" ) { iFirstWord = Math.floor(Math.random() * limit); } // Choose second misspelled word, and make sure it isn't the first or an URL var iSecondWord = Math.floor(Math.random() * limit); while (isLink(words[iSecondWord]) || iSecondWord === iFirstWord || words[iSecondWord][0] === "@") { iSecondWord = Math.floor(Math.random() * limit); } words[iFirstWord] = swapRandomLetters(words[iFirstWord]); words[iSecondWord] = swapRandomLetters(words[iSecondWord]); return words.join(" "); } 

如果一个twit的所有单词都以@开始,那么就会有一个无限循环,从而阻塞了主线程。