My golang app runs on port 9000 at my localhost. After deploying it at heroku using godep support, i was able to push and deploy at heroku. However when i try to access the endpoint e.g '/', it shows Application Error. You can see below my code and log while deploying at heroku
package main
import (
"log"
"fmt"
"net/http"
"os"
"github.com/gorilla/mux"
"github.com/gorilla/context"
"gopkg.in/paytm/grace.v1"
// utilhttp "bitbucket.org/michaelchandrag/chit/pkg/util/http"
// util "bitbucket.org/michalechandrag/chit/pkg/util"
)
func main() {
log.Println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ CHIT STARTED $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
log.Println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
muxRouter := mux.NewRouter()
muxRouter.HandleFunc("/", Articles)
http.Handle("/", muxRouter)
// err = grace.Serve(":"+cfg.Server.Port, context.ClearHandler(http.DefaultServeMux))
err := grace.Serve(":9000", context.ClearHandler(http.DefaultServeMux))
if err != nil {
log.Println("[ERROR GRACEFUL]", err)
os.Exit(1)
}
os.Exit(0)
}
func Articles(w http.ResponseWriter, r *http.Request) {
// vars := mux.Vars(r)
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Hello")
// r.Close = true
// w.Header().Set("Content-Type", "application/json")
// w.Header().Set("Access-Control-Allow-Origin", "*")
/*if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
if err := fn(w, r); err != nil {
log.Println(err)
apiObject := ConstructAPIError(http.StatusInternalServerError, ErrGeneral, SysMsgErrGeneral, MsgErrGeneral)
SendAPIObject(w, apiObject)
return
}*/
}
heroku logs while deploying
-----> Go app detected
-----> Fetching jq... done
-----> Fetching stdlib.sh.v8... done
-----> Checking Godeps/Godeps.json file.
-----> New Go Version, clearing old cache
-----> Installing go1.12.6
-----> Fetching go1.12.6.linux-amd64.tar.gz... done
-----> Running: go install -v -tags heroku ./...
bitbucket.org/michaelchandrag/chit/pkg
bitbucket.org/michaelchandrag/chit/vendor/github.com/gorilla/context
bitbucket.org/michaelchandrag/chit/vendor/github.com/gorilla/mux
bitbucket.org/michaelchandrag/chit/vendor/gopkg.in/tylerb/graceful.v1
bitbucket.org/michaelchandrag/chit/vendor/gopkg.in/paytm/grace.v1
bitbucket.org/michaelchandrag/chit/pkg/util
bitbucket.org/michaelchandrag/chit
Installed the following binaries:
./bin/chit
-----> Discovering process types
Procfile declares types -> web
-----> Compressing...
Done: 7.5M
-----> Launching...
Released v3
https://michaelchandrag-project.herokuapp.com/ deployed to Heroku
app logs before and after access endpoint
2:47.954106+00:00 heroku[web.1]: Starting process with command `chit`
2019-07-08T05:02:49.413453+00:00 app[web.1]: 2019/07/08 05:02:49 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ CHIT STARTED $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
2019-07-08T05:02:49.413476+00:00 app[web.1]: 2019/07/08 05:02:49 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
2019-07-08T05:02:49.413647+00:00 app[web.1]: 2019/07/08 05:02:49 starting serve on :9000
2019-07-08T05:03:48.131507+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2019-07-08T05:03:48.131595+00:00 heroku[web.1]: Stopping process with SIGKILL
2019-07-08T05:03:48.214979+00:00 heroku[web.1]: State changed from starting to crashed
2019-07-08T05:03:48.193205+00:00 heroku[web.1]: Process exited with status 137
2019-07-08T10:38:59.721224+00:00 heroku[web.1]: State changed from crashed to starting
2019-07-08T10:39:00.359017+00:00 heroku[web.1]: Starting process with command `chit`
2019-07-08T10:39:02.232435+00:00 app[web.1]: 2019/07/08 10:39:02 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ CHIT STARTED $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
2019-07-08T10:39:02.232458+00:00 app[web.1]: 2019/07/08 10:39:02 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
2019-07-08T10:39:02.232583+00:00 app[web.1]: 2019/07/08 10:39:02 starting serve on :9000
2019-07-08T10:40:00.462841+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2019-07-08T10:40:00.462974+00:00 heroku[web.1]: Stopping process with SIGKILL
2019-07-08T10:40:00.555959+00:00 heroku[web.1]: Process exited with status 137
2019-07-08T10:40:00.573427+00:00 heroku[web.1]: State changed from starting to crashed
2019-07-08T13:33:25.021835+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=michaelchandrag-project.herokuapp.com request_id=e6b5c93a-7177-4080-9858-ed925a20c513 fwd="36.74.35.162" dyno= connect= service= status=503 bytes= protocol=https
2019-07-08T13:33:25.566036+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=michaelchandrag-project.herokuapp.com request_id=35c741a9-be4d-48f6-ba99-ba24ddc58b9b fwd="36.74.35.162" dyno= connect= service= status=503 bytes= protocol=https
2019-07-08T13:48:38.294082+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=michaelchandrag-project.herokuapp.com request_id=57a3a243-58f4-4efe-b478-eb660d84fe1f fwd="36.74.35.162" dyno= connect= service= status=503 bytes= protocol=https
2019-07-08T13:48:38.616077+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=michaelchandrag-project.herokuapp.com request_id=0687b8a8-86ae-4c49-a37f-9cbf9e0f75de fwd="36.74.35.162" dyno= connect= service= status=503 bytes= protocol=https
Your app starts but is getting killed because you don't bind your webserver to the specified port. This is clear from the log message:
2019-07-08T05:03:48.131507+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
On Heroku you have to bind your HTTP server to the port specified by the PORT environment variable. Your app will be publicly available on default HTTP and HTTPS ports with the help of Heroku gateways.
So start your server on the expected port. Instead of this:
err := grace.Serve(":9000", context.ClearHandler(http.DefaultServeMux))
Do this:
port := os.Getenv("PORT")
if port == "" {
port = "9000" // Default port if not specified
}
err := grace.Serve(":" + port, context.ClearHandler(http.DefaultServeMux))
Related
When I deploy my go fiber app to heroku, I got the following error
2022-08-23T15:33:03.334087+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/swagger/index.html" host=vast-ocean-37337.herokuapp.com request_id=9161bdfc-ff62-4b5e-bd99-1649f7f5e569 fwd="173.32.236.24" dyno= connect= service= status=503 bytes= protocol=https
and this is my main.go file
package main
import (
"os"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/recover"
"rocketstothemoon.com/configs"
"rocketstothemoon.com/routes"
fiber_swagger "github.com/swaggo/fiber-swagger"
_ "rocketstothemoon.com/docs"
)
func main() {
app := fiber.New()
app.Use(recover.New())
app.Use(cors.New())
configs.ConnectDB()
routes.UserRoute(app)
app.Get("/swagger/*", fiber_swagger.WrapHandler)
serveStatic(app)
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
app.Listen(":" + port)
}
func serveStatic(app *fiber.App) {
app.Static("/", "./build")
}
It works perfectly fine in my local, but keep throwing this error in heroku.
I have changed my port to os.Getenv("PORT") but it doesn't help, any help?
I have been recently attempting to deploy a simple golang app to heroku using the same method I used to deploy previous ones, and I'm running into error with my procfile execution.
2021-07-06T23:30:28.000000+00:00 app[api]: Build started by user user#yahoo.com
2021-07-06T23:30:40.271575+00:00 app[api]: Release v7 created by user user#yahoo.com
2021-07-06T23:30:40.271575+00:00 app[api]: Deploy 0d19e552 by user user#yahoo.com
2021-07-06T23:30:40.909377+00:00 heroku[web.1]: State changed from crashed to starting
2021-07-06T23:30:42.049118+00:00 heroku[web.1]: Starting process with command `bin/main`
2021-07-06T23:30:45.582197+00:00 app[web.1]: bash: bin/main: cannot execute binary file: Exec format error
2021-07-06T23:30:45.646220+00:00 heroku[web.1]: Process exited with status 126
2021-07-06T23:30:45.722720+00:00 heroku[web.1]: State changed from starting to crashed
2021-07-06T23:30:57.000000+00:00 app[api]: Build succeeded
2021-07-06T23:45:41.986316+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=testing-my-message-app.herokuapp.com request_id=fd3a8259-82dc-489f-b206-060ec0d8f8f6 fwd="67.81.209.179" dyno= connect= service= status=503 bytes= protocol=https
2021-07-06T23:45:42.522223+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=testing-my-message-app.herokuapp.com request_id=f3268609-cdcc-4c16-af24-f7dfa5568030 fwd="67.81.209.179" dyno= connect= service= status=503 bytes= protocol=https
I tried running both go build -o bin/main and go build -o bin/main -v .to build the binary file, but heroku is unable to build it for due to format error. How can I fix this? I'm running on a 64 bit window platform for reference.
Also, just in case, here is the code I am trying to deploy.
package main
import (
"fmt"
"log"
"net/http"
"os"
"strconv"
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/joho/godotenv"
)
type m map[string]interface{}
func main(){
godotenv.Load()
router := chi.NewRouter()
words := m{
"black": 10,
"red": 7,
"pink": 0,
"yellow": 5,
"blue": 9,
}
router.Get("/all", func(res http.ResponseWriter, req *http.Request) {
render.JSON(res, req, words)
})
router.Get("/add-color-rating/{color}/{rating}", func(res http.ResponseWriter, req *http.Request) {
color := chi.URLParam(req, "color")
rating, _ := strconv.Atoi(chi.URLParam(req, "rating"))
words[color] = rating
render.JSON(res, req, m{"added-rating": m{color: rating}})
})
fs := http.FileServer(http.Dir("./static"))
router.Handle("/static/*", http.StripPrefix("/static", fs))
fmt.Println("running on port:", os.Getenv("PORT"))
log.Fatal(http.ListenAndServe(os.Getenv("PORT"), router))
}
It could also be that you are trying to run an executable compiled for a specific architecture on a platform with a different architecture. Try specifying the the target os and architecture in which you expect to run the build as follows.
env GOOS=linux GOARCH=arm64 go build -o bin/main
If you're trying to build files in the bin/main folder, you need to do this:
go build bin/main
If you are trying to build in the current folder, you need to do this:
go build -o bin/main.exe
https://golang.org/cmd/go#hdr-Compile_packages_and_dependencies
My golang app runs on port 9000 at my localhost. After deploying it at heroku using godep support, i was able to push and deploy at heroku. However when i try to access the endpoint e.g '/', it shows Application Error. You can see below my code and log while deploying at heroku
package main
import (
"log"
"fmt"
"net/http"
"os"
"github.com/gorilla/mux"
"github.com/gorilla/context"
"gopkg.in/paytm/grace.v1"
// utilhttp "bitbucket.org/michaelchandrag/chit/pkg/util/http"
// util "bitbucket.org/michalechandrag/chit/pkg/util"
)
func main() {
log.Println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ CHIT STARTED $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
log.Println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
muxRouter := mux.NewRouter()
muxRouter.HandleFunc("/", Articles)
http.Handle("/", muxRouter)
// err = grace.Serve(":"+cfg.Server.Port, context.ClearHandler(http.DefaultServeMux))
err := grace.Serve(":9000", context.ClearHandler(http.DefaultServeMux))
if err != nil {
log.Println("[ERROR GRACEFUL]", err)
os.Exit(1)
}
os.Exit(0)
}
func Articles(w http.ResponseWriter, r *http.Request) {
// vars := mux.Vars(r)
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Hello")
// r.Close = true
// w.Header().Set("Content-Type", "application/json")
// w.Header().Set("Access-Control-Allow-Origin", "*")
/*if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
if err := fn(w, r); err != nil {
log.Println(err)
apiObject := ConstructAPIError(http.StatusInternalServerError, ErrGeneral, SysMsgErrGeneral, MsgErrGeneral)
SendAPIObject(w, apiObject)
return
}*/
}
heroku logs while deploying
-----> Go app detected
-----> Fetching jq... done
-----> Fetching stdlib.sh.v8... done
-----> Checking Godeps/Godeps.json file.
-----> New Go Version, clearing old cache
-----> Installing go1.12.6
-----> Fetching go1.12.6.linux-amd64.tar.gz... done
-----> Running: go install -v -tags heroku ./...
bitbucket.org/michaelchandrag/chit/pkg
bitbucket.org/michaelchandrag/chit/vendor/github.com/gorilla/context
bitbucket.org/michaelchandrag/chit/vendor/github.com/gorilla/mux
bitbucket.org/michaelchandrag/chit/vendor/gopkg.in/tylerb/graceful.v1
bitbucket.org/michaelchandrag/chit/vendor/gopkg.in/paytm/grace.v1
bitbucket.org/michaelchandrag/chit/pkg/util
bitbucket.org/michaelchandrag/chit
Installed the following binaries:
./bin/chit
-----> Discovering process types
Procfile declares types -> web
-----> Compressing...
Done: 7.5M
-----> Launching...
Released v3
https://michaelchandrag-project.herokuapp.com/ deployed to Heroku
app logs before and after access endpoint
2:47.954106+00:00 heroku[web.1]: Starting process with command `chit`
2019-07-08T05:02:49.413453+00:00 app[web.1]: 2019/07/08 05:02:49 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ CHIT STARTED $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
2019-07-08T05:02:49.413476+00:00 app[web.1]: 2019/07/08 05:02:49 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
2019-07-08T05:02:49.413647+00:00 app[web.1]: 2019/07/08 05:02:49 starting serve on :9000
2019-07-08T05:03:48.131507+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2019-07-08T05:03:48.131595+00:00 heroku[web.1]: Stopping process with SIGKILL
2019-07-08T05:03:48.214979+00:00 heroku[web.1]: State changed from starting to crashed
2019-07-08T05:03:48.193205+00:00 heroku[web.1]: Process exited with status 137
2019-07-08T10:38:59.721224+00:00 heroku[web.1]: State changed from crashed to starting
2019-07-08T10:39:00.359017+00:00 heroku[web.1]: Starting process with command `chit`
2019-07-08T10:39:02.232435+00:00 app[web.1]: 2019/07/08 10:39:02 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ CHIT STARTED $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
2019-07-08T10:39:02.232458+00:00 app[web.1]: 2019/07/08 10:39:02 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
2019-07-08T10:39:02.232583+00:00 app[web.1]: 2019/07/08 10:39:02 starting serve on :9000
2019-07-08T10:40:00.462841+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2019-07-08T10:40:00.462974+00:00 heroku[web.1]: Stopping process with SIGKILL
2019-07-08T10:40:00.555959+00:00 heroku[web.1]: Process exited with status 137
2019-07-08T10:40:00.573427+00:00 heroku[web.1]: State changed from starting to crashed
2019-07-08T13:33:25.021835+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=michaelchandrag-project.herokuapp.com request_id=e6b5c93a-7177-4080-9858-ed925a20c513 fwd="36.74.35.162" dyno= connect= service= status=503 bytes= protocol=https
2019-07-08T13:33:25.566036+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=michaelchandrag-project.herokuapp.com request_id=35c741a9-be4d-48f6-ba99-ba24ddc58b9b fwd="36.74.35.162" dyno= connect= service= status=503 bytes= protocol=https
2019-07-08T13:48:38.294082+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=michaelchandrag-project.herokuapp.com request_id=57a3a243-58f4-4efe-b478-eb660d84fe1f fwd="36.74.35.162" dyno= connect= service= status=503 bytes= protocol=https
2019-07-08T13:48:38.616077+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=michaelchandrag-project.herokuapp.com request_id=0687b8a8-86ae-4c49-a37f-9cbf9e0f75de fwd="36.74.35.162" dyno= connect= service= status=503 bytes= protocol=https
Your app starts but is getting killed because you don't bind your webserver to the specified port. This is clear from the log message:
2019-07-08T05:03:48.131507+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
On Heroku you have to bind your HTTP server to the port specified by the PORT environment variable. Your app will be publicly available on default HTTP and HTTPS ports with the help of Heroku gateways.
So start your server on the expected port. Instead of this:
err := grace.Serve(":9000", context.ClearHandler(http.DefaultServeMux))
Do this:
port := os.Getenv("PORT")
if port == "" {
port = "9000" // Default port if not specified
}
err := grace.Serve(":" + port, context.ClearHandler(http.DefaultServeMux))
My golang app runs on port 9000 at my localhost. After deploying it at heroku using godep support, i was able to push and deploy at heroku. However when i try to access the endpoint e.g '/', it shows Application Error. You can see below my code and log while deploying at heroku
package main
import (
"log"
"fmt"
"net/http"
"os"
"github.com/gorilla/mux"
"github.com/gorilla/context"
"gopkg.in/paytm/grace.v1"
// utilhttp "bitbucket.org/michaelchandrag/chit/pkg/util/http"
// util "bitbucket.org/michalechandrag/chit/pkg/util"
)
func main() {
log.Println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ CHIT STARTED $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
log.Println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
muxRouter := mux.NewRouter()
muxRouter.HandleFunc("/", Articles)
http.Handle("/", muxRouter)
// err = grace.Serve(":"+cfg.Server.Port, context.ClearHandler(http.DefaultServeMux))
err := grace.Serve(":9000", context.ClearHandler(http.DefaultServeMux))
if err != nil {
log.Println("[ERROR GRACEFUL]", err)
os.Exit(1)
}
os.Exit(0)
}
func Articles(w http.ResponseWriter, r *http.Request) {
// vars := mux.Vars(r)
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Hello")
// r.Close = true
// w.Header().Set("Content-Type", "application/json")
// w.Header().Set("Access-Control-Allow-Origin", "*")
/*if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
if err := fn(w, r); err != nil {
log.Println(err)
apiObject := ConstructAPIError(http.StatusInternalServerError, ErrGeneral, SysMsgErrGeneral, MsgErrGeneral)
SendAPIObject(w, apiObject)
return
}*/
}
heroku logs while deploying
-----> Go app detected
-----> Fetching jq... done
-----> Fetching stdlib.sh.v8... done
-----> Checking Godeps/Godeps.json file.
-----> New Go Version, clearing old cache
-----> Installing go1.12.6
-----> Fetching go1.12.6.linux-amd64.tar.gz... done
-----> Running: go install -v -tags heroku ./...
bitbucket.org/michaelchandrag/chit/pkg
bitbucket.org/michaelchandrag/chit/vendor/github.com/gorilla/context
bitbucket.org/michaelchandrag/chit/vendor/github.com/gorilla/mux
bitbucket.org/michaelchandrag/chit/vendor/gopkg.in/tylerb/graceful.v1
bitbucket.org/michaelchandrag/chit/vendor/gopkg.in/paytm/grace.v1
bitbucket.org/michaelchandrag/chit/pkg/util
bitbucket.org/michaelchandrag/chit
Installed the following binaries:
./bin/chit
-----> Discovering process types
Procfile declares types -> web
-----> Compressing...
Done: 7.5M
-----> Launching...
Released v3
https://michaelchandrag-project.herokuapp.com/ deployed to Heroku
app logs before and after access endpoint
2:47.954106+00:00 heroku[web.1]: Starting process with command `chit`
2019-07-08T05:02:49.413453+00:00 app[web.1]: 2019/07/08 05:02:49 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ CHIT STARTED $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
2019-07-08T05:02:49.413476+00:00 app[web.1]: 2019/07/08 05:02:49 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
2019-07-08T05:02:49.413647+00:00 app[web.1]: 2019/07/08 05:02:49 starting serve on :9000
2019-07-08T05:03:48.131507+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2019-07-08T05:03:48.131595+00:00 heroku[web.1]: Stopping process with SIGKILL
2019-07-08T05:03:48.214979+00:00 heroku[web.1]: State changed from starting to crashed
2019-07-08T05:03:48.193205+00:00 heroku[web.1]: Process exited with status 137
2019-07-08T10:38:59.721224+00:00 heroku[web.1]: State changed from crashed to starting
2019-07-08T10:39:00.359017+00:00 heroku[web.1]: Starting process with command `chit`
2019-07-08T10:39:02.232435+00:00 app[web.1]: 2019/07/08 10:39:02 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ CHIT STARTED $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
2019-07-08T10:39:02.232458+00:00 app[web.1]: 2019/07/08 10:39:02 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
2019-07-08T10:39:02.232583+00:00 app[web.1]: 2019/07/08 10:39:02 starting serve on :9000
2019-07-08T10:40:00.462841+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2019-07-08T10:40:00.462974+00:00 heroku[web.1]: Stopping process with SIGKILL
2019-07-08T10:40:00.555959+00:00 heroku[web.1]: Process exited with status 137
2019-07-08T10:40:00.573427+00:00 heroku[web.1]: State changed from starting to crashed
2019-07-08T13:33:25.021835+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=michaelchandrag-project.herokuapp.com request_id=e6b5c93a-7177-4080-9858-ed925a20c513 fwd="36.74.35.162" dyno= connect= service= status=503 bytes= protocol=https
2019-07-08T13:33:25.566036+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=michaelchandrag-project.herokuapp.com request_id=35c741a9-be4d-48f6-ba99-ba24ddc58b9b fwd="36.74.35.162" dyno= connect= service= status=503 bytes= protocol=https
2019-07-08T13:48:38.294082+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=michaelchandrag-project.herokuapp.com request_id=57a3a243-58f4-4efe-b478-eb660d84fe1f fwd="36.74.35.162" dyno= connect= service= status=503 bytes= protocol=https
2019-07-08T13:48:38.616077+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=michaelchandrag-project.herokuapp.com request_id=0687b8a8-86ae-4c49-a37f-9cbf9e0f75de fwd="36.74.35.162" dyno= connect= service= status=503 bytes= protocol=https
Your app starts but is getting killed because you don't bind your webserver to the specified port. This is clear from the log message:
2019-07-08T05:03:48.131507+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
On Heroku you have to bind your HTTP server to the port specified by the PORT environment variable. Your app will be publicly available on default HTTP and HTTPS ports with the help of Heroku gateways.
So start your server on the expected port. Instead of this:
err := grace.Serve(":9000", context.ClearHandler(http.DefaultServeMux))
Do this:
port := os.Getenv("PORT")
if port == "" {
port = "9000" // Default port if not specified
}
err := grace.Serve(":" + port, context.ClearHandler(http.DefaultServeMux))
I am testing Facebook Messenger bot. Part of the code with core functionality:
const schedule = require('node-schedule');
app.post('/webhook/', function(req, res) {
let messaging_events = req.body.entry[0].messaging
var j = schedule.scheduleJob('10 * * * * *', function(){
for (let i = 0; i < messaging_events.length; i++) {
let event = messaging_events[i]
let sender = event.sender.id
if (event.message && event.message.text) {
let text = event.message.text
sendText(sender, "Text echo: " + text.substring(0, 100))
}
}
res.sendStatus(200)
})
})
It should reply the same message that is sent to the bot every minute (when the clock crosses 10 seconds - 2:46:10 PM, 2:47:10 PM, etc.)
If I try similar code locally, it works. It does not on Heroku. It behaves irregularly - sometimes it replies the message 2 to 4 times at once and then crashes, sometimes it does not reply anything at all. Here is the Heroku log:
2018-03-19T13:48:43.574712+00:00 heroku[web.1]: Unidling
2018-03-19T13:48:43.575153+00:00 heroku[web.1]: State changed from down to starting
2018-03-19T13:48:45.854373+00:00 heroku[web.1]: Starting process with command `node index.js`
2018-03-19T13:48:48.046370+00:00 app[web.1]: running: port
2018-03-19T13:48:48.550571+00:00 heroku[web.1]: State changed from starting to up
2018-03-19T13:49:10.035608+00:00 heroku[router]: at=info method=POST path="/webhook/" host=dry-badlands-96368.herokuapp.com request_id=89a76e53-663d-4086-8457-64c0f26be8c7 fwd="31.13.113.20" dyno=web.1 connect=1ms service=11963ms status=200 bytes=201 protocol=https
2018-03-19T13:49:10.046345+00:00 heroku[router]: at=info method=POST path="/webhook/" host=dry-badlands-96368.herokuapp.com request_id=0da8c7a9-4d5f-45bc-8797-54f608968581 fwd="31.13.105.114" dyno=web.1 connect=0ms service=20401ms status=200 bytes=201 protocol=https
2018-03-19T13:49:41.297748+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/webhook/" host=dry-badlands-96368.herokuapp.com request_id=cd1568a1-68c3-455f-872e-260f8023d925 fwd="31.13.113.170" dyno=web.1 connect=1ms service=30002ms status=503 bytes=0 protocol=https
2018-03-19T13:49:41.747709+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/webhook/" host=dry-badlands-96368.herokuapp.com request_id=32947747-1c2b-475c-a2b8-46477e13c4a2 fwd="31.13.114.116" dyno=web.1 connect=1ms service=30000ms status=503 bytes=0 protocol=https
2018-03-19T13:50:10.059682+00:00 app[web.1]: _http_outgoing.js:491
2018-03-19T13:50:10.059738+00:00 app[web.1]: throw new Error('Can\'t set headers after they are sent.');
2018-03-19T13:50:10.059740+00:00 app[web.1]: ^
2018-03-19T13:50:10.059742+00:00 app[web.1]:
2018-03-19T13:50:10.059744+00:00 app[web.1]: Error: Can't set headers after they are sent.
2018-03-19T13:50:10.059745+00:00 app[web.1]: at validateHeader (_http_outgoing.js:491:11)
2018-03-19T13:50:10.059747+00:00 app[web.1]: at ServerResponse.setHeader (_http_outgoing.js:498:3)
2018-03-19T13:50:10.059748+00:00 app[web.1]: at ServerResponse.header (/app/node_modules/express/lib/response.js:767:10)
2018-03-19T13:50:10.059750+00:00 app[web.1]: at ServerResponse.contentType (/app/node_modules/express/lib/response.js:595:15)
2018-03-19T13:50:10.059752+00:00 app[web.1]: at ServerResponse.sendStatus (/app/node_modules/express/lib/response.js:357:8)
2018-03-19T13:50:10.059753+00:00 app[web.1]: at Job.job (/app/index.js:46:9)
2018-03-19T13:50:10.059755+00:00 app[web.1]: at Job.invoke (/app/node_modules/node-schedule/lib/schedule.js:173:10)
2018-03-19T13:50:10.059756+00:00 app[web.1]: at /app/node_modules/node-schedule/lib/schedule.js:543:11
2018-03-19T13:50:10.059758+00:00 app[web.1]: at Timeout._onTimeout (/app/node_modules/node-schedule/lib/schedule.js:501:7)
2018-03-19T13:50:10.059759+00:00 app[web.1]: at ontimeout (timers.js:482:11)
2018-03-19T13:50:10.130605+00:00 heroku[web.1]: Process exited with status 1
2018-03-19T13:50:10.241686+00:00 heroku[web.1]: State changed from up to crashed
Does anyone have any idea why is this happening? Maybe Heroku is not suited for this?
Thank you for all suggestions.