Twitter机器人与go在Heroku崩溃



我已经把这个推特机器人放在一起,它非常简单。从外部 API 收集一些数据,对其进行处理并将其发布。

我尝试了几种不同的方法来让机器人在整点发推文,现在我有了这个:

func main() {
fmt.Println("------ Starting bot ------")
func() {
for range time.Tick(time.Second) {
fmt.Println("checking for", time.Now().Format("15:04:05"))
if time.Now().Format("04") == "00" {
// call the function that calls the external API and does all the work
}
}
}()
}

肯定可以对其进行改进,但它在本地运行,所以我现在对它感到满意。问题是,当我在 Heroku 上部署它时,它会运行一分钟并吱吱作响。以下是日志:

2019-12-10T06:26:47.622145+00:00 app[web.1]: checking for 06:26:47
2019-12-10T06:26:48.622122+00:00 app[web.1]: checking for 06:26:48
2019-12-10T06:26:49.622554+00:00 app[web.1]: checking for 06:26:49
2019-12-10T06:26:50.622181+00:00 app[web.1]: checking for 06:26:50
2019-12-10T06:26:51.622207+00:00 app[web.1]: checking for 06:26:51
2019-12-10T06:26:52.622019+00:00 app[web.1]: checking for 06:26:52
2019-12-10T06:26:53.181083+00:00 heroku[web.1]: State changed from starting to crashed
2019-12-10T06:26:53.075003+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2019-12-10T06:26:53.075113+00:00 heroku[web.1]: Stopping process with SIGKILL
2019-12-10T06:26:53.162250+00:00 heroku[web.1]: Process exited with status 137

知道如何解决这个问题吗?

评论中更复杂的答案。Heroku有一个健康检查系统,可以向你的应用程序发送请求以检查它是否回答。如果您在 60 秒内不打开端口,它将杀死应用程序。您可以在此处阅读有关它的更多信息:https://devcenter.heroku.com/articles/dynos#web-dynos

谢谢大家!

我通过添加以下内容来修复它:

go http.ListenAndServe(":"+os.Getenv("PORT"), nil)

现在唯一的问题是应用程序在一段时间后进入睡眠状态(可能是因为我正在使用time.Sleep().我也会确保弄清楚这一点,并在此处发布答案:)

固定! 刚刚使用了 Heroku 提供的调度程序附加组件。删除了所有定时函数。:)