Heroku/foreman fail with process.env.PORT #59

Closed
sethvincent opened this Issue Aug 16, 2011 · 9 comments

Projects

None yet

3 participants

@sethvincent

I've been working on getting zappa into heroku, and I'm having trouble getting the port to work. I get the same error using foreman on my local machine as I do when I deploy the app to heroku.

If I specify a port like 5000 on my local machine foreman will run the app just fine, but heroku needs the app to grab the port using process.env.PORT to work (or maybe there's an alternate way to get the environment variable that i'm missing).

I thought that maybe process.env.PORT just wasn't working for some reason, but I got it to work using a plain express app.
I put process.env.PORT in console.log to see what it returns, and it shoots back a port number like nothing's wrong.

For a second I thought that maybe the switch statement at line 475 of zappa.coffee wasn't recognizing my variable port as a number. But that doesn't really make sense.
Because when I change port = process.env.PORT to port = #an_actual_number things work just fine.
That's as close as I got to figuring this out. I'm stumped.

Here's my test zappa app:

app = require 'zappa'

port = process.env.PORT

console.log 'process.env.PORT returns: ' + port

app port, ->
  get '/': 'hi'

and the Procfile:

web: coffee test.coffee

here's the error when I run foreman on my computer:

seth$ foreman start
12:57:16 web.1     | started with pid 486
12:57:18 web.1     | process.env.PORT returns: 5000
12:57:18 web.1     |    info  - socket.io started
12:57:18 web.1     | TypeError: Bad file descriptor argument
12:57:18 web.1     |     at HTTPServer.address (net.js:1127:10)
12:57:18 web.1     |     at /Users/seth/Sites/heroku_zappa/node_modules/zappa/lib/zappa.js:659:63
12:57:18 web.1     |     at Object.<anonymous> (/Users/seth/Sites/heroku_zappa/test.coffee:6:3)
12:57:18 web.1     |     at Object.<anonymous> (/Users/seth/Sites/heroku_zappa/test.coffee:11:4)
12:57:18 web.1     |     at Module._compile (module.js:404:26)
12:57:18 web.1     |     at Object.run (/usr/local/lib/node_modules/coffee-script/lib/coffee-script.js:62:19)
12:57:18 web.1     |     at /usr/local/lib/node_modules/coffee-script/lib/command.js:120:29
12:57:18 web.1     |     at /usr/local/lib/node_modules/coffee-script/lib/command.js:90:26
12:57:18 web.1     |     at [object Object].<anonymous> (fs.js:107:5)
12:57:18 web.1     |     at [object Object].emit (events.js:61:17)
12:57:18 web.1     | process terminated
12:57:18 system    | sending SIGTERM to all processes

and the error in the heroku logs:

2011-08-16T19:59:49+00:00 heroku[web.1]: State changed from created to starting
2011-08-16T19:59:54+00:00 heroku[web.1]: Starting process with command `coffee test.coffee`
2011-08-16T19:59:54+00:00 app[web.1]: process.env.PORT returns: 4474
2011-08-16T19:59:54+00:00 app[web.1]:    info  - socket.io started
2011-08-16T19:59:55+00:00 app[web.1]: TypeError: Bad file descriptor argument
2011-08-16T19:59:55+00:00 app[web.1]:     at HTTPServer.address (net.js:1127:10)
2011-08-16T19:59:55+00:00 app[web.1]:     at /app/node_modules/zappa/lib/zappa.js:659:63
2011-08-16T19:59:55+00:00 app[web.1]:     at Object.<anonymous> (/app/test.coffee:6:3)
2011-08-16T19:59:55+00:00 app[web.1]:     at Object.<anonymous> (/app/test.coffee:11:4)
2011-08-16T19:59:55+00:00 app[web.1]:     at Module._compile (module.js:404:26)
2011-08-16T19:59:55+00:00 app[web.1]:     at Object.run (/app/node_modules/coffee-script/lib/coffee-script.js:57:25)
2011-08-16T19:59:55+00:00 app[web.1]:     at /app/node_modules/coffee-script/lib/command.js:147:29
2011-08-16T19:59:55+00:00 app[web.1]:     at /app/node_modules/coffee-script/lib/command.js:115:26
2011-08-16T19:59:55+00:00 app[web.1]:     at [object Object].<anonymous> (fs.js:107:5)
2011-08-16T19:59:55+00:00 app[web.1]:     at [object Object].emit (events.js:61:17)
2011-08-16T19:59:55+00:00 heroku[web.1]: Process exited
2011-08-16T19:59:55+00:00 heroku[web.1]: State changed from starting to crashed

Thanks for taking a look.

@mauricemach
Owner

You were so close to the answer! :)

The problem is that process.env.PORT is given as a string by both foreman and heroku. Casting the port to number I was able to deploy a test app to heroku:

http://gentle-dusk-941.herokuapp.com/

My web.coffee:

port = Number(process.env.PORT || 3000)
require('zappa') port, ->
  use 'logger'
  get '/': 'Hello Worlde! (from zappa)'

Zappa treats a string argument as the host and a number as the port, so we don't have to fill "parameter slots" with null.

@sethvincent

Oh! Dang, I was close.
Thanks for the help. That makes a lot of sense.

I've got things working now.

Sidenote: Zappa is so cool!

@mauricemach
Owner

@timshadel implemented a clever solution to this in pull #61. Number-castable strings are ports.

@sethvincent

Oh, that's great! I've started blogging my zappa adventures and just updated my post on getting zappa on heroku: http://blog.superbigtree.com/blog/2011/08/19/hosting-zappa-0-2-x-on-heroku/

Let me know if I've got anything wonky going on there.

@mauricemach
Owner

Nice article! I'll put a link to it on the readme. Nothing wonky as far as I can see. :)

@DallanQ
DallanQ commented Apr 6, 2012

link is broken on zappajs.org, and I can't find a cached page on google or at archive.org. I'd like to host a zappa app on heroku. It sounds like the port issue has been solved with pull #61; is there anything else I should know?

You probably ought to remove the dead link on the zappjs.org homepage.

@sethvincent

Oh dang! My blog got crunched by tumblr a while back so I had to kinda start over. I need to fix that.

With the solution in #61 hosting on heroku is pretty straightforward.

Things to consider:

@DallanQ
DallanQ commented Apr 8, 2012

Thank you for the information!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment