【问题标题】:Node forever says program is running, but evidence suggests notNode 永远说程序正在运行,但证据表明不是
【发布时间】:2017-04-18 16:16:09
【问题描述】:

我正在尝试使用 twit 库和 forever 运行一个简单的 twitterbot。该机器人本质上会监视另一个 Twitter 帐户,并复制其推文并进行一些修改。

我一直在记录从流式 API 接收到的推文,并记录任何错误。

但是,机器人通常会停止工作,但不会记录任何错误,而 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

起初,我认为这可能是 twitter 流 API 的问题 - 即,该信息已停止“通过管道”发送,因为日志文件已停止记录来自 twitter 流 API 的新消息。

为了验证机器人确实仍在运行,我创建了一个heartbeat,每分钟记录一次。

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

当机器人停止运行时,心跳也会停止记录。然而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(" ");
}

【问题讨论】:

    标签: javascript node.js twitter forever


    【解决方案1】:

    如果一条推文的所有单词都以@ 开头,那么你有一个无限循环,从而阻塞了主线程。

    【讨论】:

      猜你喜欢
      • 2017-11-19
      • 2020-08-20
      • 1970-01-01
      • 2018-10-03
      • 2019-06-15
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多