skip to Main Content

I’ve put together this twitter bot and it is very simple. Gathers some data from an external API, works on it and tweets it out.

I tried several different ways to have the bot tweet out on the hour and now I have this:

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

            }
        }
    }()

}

There are improvements that can be made on it for sure, but it runs locally so I’m happy with it for now. The problem is that when i deploy it on Heroku it runs for like a minute and creshes. Here are the logs:

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

Any idea how to tackle this?

2

Answers


  1. Chosen as BEST ANSWER

    Thanks guys!

    I fixed it by adding:

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

    Only problem now is that the app goes to sleep after a while (maybe because I am using time.Sleep(). I will make sure to figure this one as well and post the answer here :)

    Fixed! Just used the Scheduler add-on that Heroku provides. Removed all timed functions. :)


  2. A bit more complex answer from the comment. Heroku has a health-check system which sends requests to your app to check if it answers. If you won’t open the port within 60 sec it will kill the application. You can read more about it here: https://devcenter.heroku.com/articles/dynos#web-dynos

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search