0

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?

tdranv
  • 1,140
  • 11
  • 38
  • is says what's the problem `Web process failed to bind to $PORT within 60 seconds of launch`. You have to open the port and the run your bot. – kabanek Dec 10 '19 at 07:53
  • 1
    You are using web dynos, so you need to bind $PORT env variable by http server or something. To fix it just create a simple web server, and make it bind to the os.Getenv("PORT") – Marat Mkhitaryan Dec 10 '19 at 08:15

2 Answers2

0

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

kabanek
  • 303
  • 3
  • 18
0

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. :)

tdranv
  • 1,140
  • 11
  • 38
  • it goes to sleep after a while because that's how heroku works - if there's no traffic it will send your app to sleep for resource optimization – kabanek Dec 10 '19 at 09:48
  • Okay, any idea how to have it wake up every hour? Maybe Heroku Scheduler :? I tried using `gocron` and Every().Hour() syntax, but it still went to sleep. Your help is much appreciated :) – tdranv Dec 10 '19 at 12:03