Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 节点Twitter机器人在Heroku上不断崩溃_Node.js_Heroku - Fatal编程技术网

Node.js 节点Twitter机器人在Heroku上不断崩溃

Node.js 节点Twitter机器人在Heroku上不断崩溃,node.js,heroku,Node.js,Heroku,为了教会自己一些Node和如何使用Heroku,我决定创建一个简单的Twitter机器人,它应该每天发一次tweet 当手动运行我的机器人时,一切都很顺利,它会发出应有的tweet。在Heroku上,甚至在我添加一个调度器(每天运行一次我的机器人)之前,它就开始随机发送推特。我必须不断删除它们。我在日志中注意到,机器人不断崩溃并重新启动,这会触发tweet 我在谷歌上搜索了几个小时,弄不明白为什么会发生这种情况。我对Node和所有与服务器有关的东西都是全新的,所以我真的不知道如何从这一点上排除故

为了教会自己一些Node和如何使用Heroku,我决定创建一个简单的Twitter机器人,它应该每天发一次tweet

当手动运行我的机器人时,一切都很顺利,它会发出应有的tweet。在Heroku上,甚至在我添加一个调度器(每天运行一次我的机器人)之前,它就开始随机发送推特。我必须不断删除它们。我在日志中注意到,机器人不断崩溃并重新启动,这会触发tweet

我在谷歌上搜索了几个小时,弄不明白为什么会发生这种情况。我对Node和所有与服务器有关的东西都是全新的,所以我真的不知道如何从这一点上排除故障

如果能帮我找出是什么导致我的机器人崩溃,我将不胜感激

(我应该注意的是,我确实在Heroku上将其设置为
工作者
,这在我的
Procfile
中。我不确定这是否有区别。)

Heroku日志

    2018-07-08T16:21:10.427313+00:00 heroku[worker.1]: Starting process with command `node bot.js`
    2018-07-08T16:21:11.167238+00:00 heroku[worker.1]: State changed from starting to up
    2018-07-08T16:21:15.159855+00:00 heroku[worker.1]: State changed from up to crashed
    2018-07-08T16:21:15.056356+00:00 heroku[worker.1]: Process exited with status 0
    2018-07-08T16:56:32.097321+00:00 heroku[worker.1]: State changed from crashed to starting
    2018-07-08T16:56:36.064795+00:00 heroku[worker.1]: Starting process with command `node bot.js`
    2018-07-08T16:56:36.789264+00:00 heroku[worker.1]: State changed from starting to up
    2018-07-08T16:56:40.212853+00:00 heroku[worker.1]: State changed from up to crashed
    2018-07-08T16:56:40.170153+00:00 heroku[worker.1]: Process exited with status 0
    2018-07-08T17:08:08.000000+00:00 app[api]: Build started by user myemailaddress
    2018-07-08T17:08:23.727988+00:00 app[api]: Deploy 852c8541 by user myemailaddress
    2018-07-08T17:08:23.727988+00:00 app[api]: Release v17 created by user myemailaddress
    2018-07-08T17:08:24.186005+00:00 heroku[worker.1]: State changed from crashed to starting
    2018-07-08T17:08:24.000000+00:00 app[api]: Build succeeded
    2018-07-08T17:08:26.514939+00:00 heroku[worker.1]: Starting process with command `node bot.js`
    2018-07-08T17:08:27.156637+00:00 heroku[worker.1]: State changed from starting to up
    2018-07-08T17:08:28.631772+00:00 heroku[worker.1]: Process exited with status 0
    2018-07-08T17:08:28.723695+00:00 heroku[worker.1]: State changed from up to crashed
    2018-07-08T17:08:28.725787+00:00 heroku[worker.1]: State changed from crashed to starting
    2018-07-08T17:08:31.479079+00:00 heroku[worker.1]: Starting process with command `node bot.js`
    2018-07-08T17:08:32.358515+00:00 heroku[worker.1]: State changed from starting to up
    2018-07-08T17:08:33.922886+00:00 heroku[worker.1]: State changed from up to crashed
    2018-07-08T17:08:33.878013+00:00 heroku[worker.1]: Process exited with status 0
    2018-07-08T17:12:36.000000+00:00 app[api]: Build started by user myemailaddress
    2018-07-08T17:12:48.895075+00:00 app[api]: Release v18 created by user myemailaddress
    2018-07-08T17:12:48.895075+00:00 app[api]: Deploy 1fbb896c by user myemailaddress
    2018-07-08T17:12:49.246800+00:00 heroku[worker.1]: State changed from crashed to starting
    2018-07-08T17:12:49.000000+00:00 app[api]: Build succeeded
    2018-07-08T17:12:53.635812+00:00 heroku[worker.1]: Starting process with command `node bot.js`
    2018-07-08T17:12:54.327615+00:00 heroku[worker.1]: State changed from starting to up
    2018-07-08T17:12:57.874384+00:00 heroku[worker.1]: State changed from up to crashed
    2018-07-08T17:12:57.852555+00:00 heroku[worker.1]: Process exited with status 0
    2018-07-08T17:35:48.475654+00:00 heroku[worker.1]: State changed from crashed to starting
    2018-07-08T17:35:52.257156+00:00 heroku[worker.1]: Starting process with command `node bot.js`
    2018-07-08T17:35:52.995975+00:00 heroku[worker.1]: State changed from starting to up
    2018-07-08T17:35:56.888137+00:00 heroku[worker.1]: State changed from up to crashed
    2018-07-08T17:35:56.871199+00:00 heroku[worker.1]: Process exited with status 0
机器人程序源代码:


由Heroku支持解决!这让我发疯。我想我会分享支持答案,以防其他人遇到这个问题。我认为我读到的Twitter机器人教程没有这个问题的原因是,它们都让自己的机器人做其他事情,比如响应标签、关注者或发送给机器人的tweet。或者他们使用
setInterval
来推特,这样可以让应用程序持续运行

我听从了这个建议,它一直运行得很好

这两种崩溃的原因都是因为您没有在worker dynos上运行进程,您只是发出了tweet和exit命令,heroku web和worker dynos被设计用于处理长时间运行的任务,因此您的worker正在启动,发出tweet,然后突然退出并崩溃,让我们检测到撞车并启动一个新的工人,它只是在一个循环中做同样的事情

如果你只想运行预定的推文,你可以删除你的Procfile并缩小你的web和worker dyno,只要你安排好了,调度器就会启动一个单独的一次性dyno,它会为你运行推文脚本并退出

    const twit = require('twit');
    const getVideo = require('./youtube');
    const { getLyrics } = require('./lyricadder/lyrics');
    const { randomNumber } = require('./helpers');

    const config = {
      consumer_key: process.env.consumer_key,
      consumer_secret: process.env.consumer_secret,
      access_token: process.env.access_token,
      access_token_secret: process.env.access_token_secret
    };

    const Twitter = new twit(config);

    const lyrics = getLyrics();

    const tweetLyric = () => {
      const random = randomNumber(lyrics.length);
      const lyric = lyrics[random];

      getVideo(lyric.song).then(response => {
        const songHash = lyric.song.replace(/\s/g, '');
        const tweet = {
          status: `${lyric.lyric}\n${response} #ToriAmos #${songHash}`
        };

        Twitter.post('statuses/update', tweet, (err, data, response) => {
          if (err) {
            console.log(err);
          }
        });
      });
    };

    tweetLyric();