fbielejec
fbielejec

Reputation: 3700

Deploying clojure web-app on Heroku, $PORT binding

I wrote a 20-minute web-service that I needed quickly deployed, so I thought of Heroku. I went through their example, added a Procfile as per the documentation:

web: java $JVM_OPTS -cp target/random-pairs.jar Clojure.main -m random-pairs.system

The server is just jetty wrapped in a Component/Lifecycle interface implementation, so I added calls that read PORT/HOST from Heroku ENV, also didn't take long:

(defn new []
  (let [host (or (System/getenv "HOST") "localhost")
        port (or (Integer. (System/getenv "PORT")) 8080)]
    (map->Server {:host host :port port :server nil :database nil})))

Then I pushed to heroku branch and was very happy to see that it builds:

remote: -----> Clojure (Leiningen 2) app detected
remote: -----> Installing OpenJDK 1.8... done
remote: -----> Using cached Leiningen 2.7.1
remote:        Writing: lein script
remote: -----> Building with Leiningen
remote:        Running: lein uberjar
remote:        Compiling random-pairs.api
remote:        Compiling random-pairs.routing
remote:        Compiling random-pairs.server
remote:        Compiling random-pairs.system
remote:        Compiling random-pairs.utils
remote:        Created /tmp/build_39db3b5354727cb980092a4caa080664/target/random-pairs-0.1.0-SNAPSHOT.jar
remote:        Created /tmp/build_39db3b5354727cb980092a4caa080664/target/random-pairs.jar
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote: 
remote: -----> Compressing...
remote:        Done: 70.5M
remote: -----> Launching...

I peeked at the logs to confirm:

2017-09-27T10:11:43.214499+00:00 heroku[web.1]: Starting process with command `java $JVM_OPTS -cp target/random-pairs.jar clojure.main -m random-pairs.system`
2017-09-27T10:11:46.312843+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2017-09-27T10:11:46.316262+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx300m -Xss512k -Dfile.encoding=UTF-8 
2017-09-27T10:11:49.562990+00:00 app[web.1]: 2017-09-27 10:11:49.559 INFO  default    org.eclipse.jetty.util.log - Logging initialized @3234ms
2017-09-27T10:11:50.401779+00:00 app[web.1]: 2017-09-27 10:11:50.401 INFO  default    random-pairs.system - :random-pairs.system/create-system Creating system
2017-09-27T10:11:50.404459+00:00 app[web.1]: 2017-09-27 10:11:50.404 INFO  default    random-pairs.system - :random-pairs.system/start Starting the application
2017-09-27T10:11:50.409370+00:00 app[web.1]: 2017-09-27 10:11:50.409 INFO  default    random-pairs.server - :random-pairs.server/server-start Starting Server component host: localhost port: 15701
2017-09-27T10:11:50.435805+00:00 app[web.1]: 2017-09-27 10:11:50.435 INFO  default    org.eclipse.jetty.server.Server - jetty-9.2.z-SNAPSHOT
2017-09-27T10:11:50.478304+00:00 app[web.1]: 2017-09-27 10:11:50.477 INFO  default    o.e.jetty.server.ServerConnector - Started ServerConnector@1a1f79ce{HTTP/1.1}{localhost:15701}
2017-09-27T10:11:50.478742+00:00 app[web.1]: 2017-09-27 10:11:50.478 INFO  default    org.eclipse.jetty.server.Server - Started @4162ms
2017-09-27T10:11:50.479247+00:00 app[web.1]: 2017-09-27 10:11:50.479 INFO  default    random-pairs.system - :random-pairs.system/main Application fully functional

Nice, Heroku seems neat. So I went on to send some requests to test everything works OK. Only that it didn't. I looked once again at the logs only to see:

2017-09-27T10:13:48.345516+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2017-09-27T10:13:48.345575+00:00 heroku[web.1]: Stopping process with SIGKILL
2017-09-27T10:13:48.488069+00:00 heroku[web.1]: State changed from starting to crashed
2017-09-27T10:13:48.476415+00:00 heroku[web.1]: Process exited with status 137

What is this in the name of Zeus? Failed to bind to $PORT? I clearly see in the logs Jetty picked up the port and the system went online?

Upvotes: 0

Views: 166

Answers (1)

fbielejec
fbielejec

Reputation: 3700

I know what happens - it's classic IP4/IP6, "localhost" resolves to ip6 address on heroku cloud machine. Putting 0.0.0.0 (or presumably also specifying no host as suggested in the comment) solves the problem.

Upvotes: 3

Related Questions