Deploying Flask with Heroku - heroku

I'm trying to deploy a Flask app to Heroku however upon pushing the code I get the error
2013-06-23T11:23:59.264600+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
I'm not sure what to try, I've tried changing the port from 5000 to 33507, but to no avail. My Procfile looks like this:
web: python main.py
main.py is the main Flask file which initiates the server.
Thanks.

In my Flask app hosted on Heroku, I use this code to start the server:
if __name__ == '__main__':
# Bind to PORT if defined, otherwise default to 5000.
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
When developing locally, this will use port 5000, in production Heroku will set the PORT environment variable.
(Side note: By default, Flask is only accessible from your own computer, not from any other in the network (see the Quickstart). Setting host='0.0.0.0' will make Flask available from the network)

In addition to msiemens's answer
import os
from run import app as application
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
application.run(host='0.0.0.0', port=port)
Your Procfile should specify the port address which in this case is stored in the heroku environment variable ${PORT}
web: gunicorn --bind 0.0.0.0:${PORT} wsgi

Your main.py script cannot bind to a specific port, it needs to bind to the port number set in the $PORT environment variable. Heroku sets the port it wants in that variable prior to invoking your application.
The error you are getting suggests you are binding to a port that is not the one Heroku expects.

This also fixes the problem of H20: App boot timeout.
My Procfile looks like this:
web: gunicorn -t 150 -c gunicorn_config.py main:app --bind 0.0.0.0:${PORT}
and main.py:
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)

Related

NestJS + Heroku - Crashes immediately after a successful build without reason

Procfile
web: npm run start:prod
Package.json
"start:prod": "NODE_ENV=production node dist/main",
Heroku logs
2022-05-10T09:01:50.583126+00:00 app[web.1]: [Nest] 22 - 05/10/2022, 9:01:50 AM LOG [NestApplication] Nest application successfully started +42ms
2022-05-10T09:01:50.818469+00:00 heroku[web.1]: Process exited with status 1
2022-05-10T09:01:50.875464+00:00 heroku[web.1]: State changed from starting to crashed
Everything works fine on local (heroku local and npm run start), but crashes on Heroku. I've tried these before:
Listen on process.env.PORT || 8080
All environments are set (heroku run printenv)
Set 0.0.0.0/0 IP on MongoDB Atlas
These are the environments that Heroku adds:
MEMORY_AVAILABLE=512
PWD=/app
PORT=42675
NODE_ENV=production
NODE_HOME=/app/.heroku/node
LINES=17
HOME=/app
COLUMNS=150
SHLVL=0
WEB_MEMORY=512
WEB_CONCURRENCY=1
PS1=\[\033[01;34m\]\w\[\033[00m\] \[\033[01;32m\]$ \[\033[00m\]
PATH=/app/.heroku/node/bin:/app/.heroku/yarn/bin:/usr/local/bin:/usr/bin:/bin:/app/bin:/app/node_modules/.bin
DYNO=run.5170
I'm not sure, but could it be for minimum hardware specification?
The issue was using port 80 for gateway and http-session-affinity was not enabled as it's required for Socket.io. Set process.env.PORT for both HTTP and gateway.
To enable http-session-affinity, run heroku features:enable http-session-affinity

Heroku port number for a given application

how to view heroku port number for a given application?
I tried to use "heroku config:get PORT" command.but windows command prompt gave the result as " ! No app specified ". what should I do?
The port number is not per-application, but per-dyno. Only your dyno has that information.
And you don't need to get it externally either. No matter which port is set for that dyno, your app will always be available externally on port 80.

configure sinatra as a server

I have written an app in ruby using sinatra. the app works fine and I am testing the post/get request using postman.
Right now I start the app using the command rackup but it starts the server locally on the port 9292. using postman, I send the POST on localhost:9292
I would like to test the app when access from another computer. I expect something using POSTMAN sending a POST on http://182.12.34.1:9292 but I didn't find how to do this.
config.ru
load './app/init.rb'
run Sinatra::Application
Procfile
web: bundle exec unicorn -p $PORT -E $RACK_ENV -c ./config/unicorn.rb
Any idea, how to switch from local test to a server ?
Thansks
The easiest way is to use an existing tool like ngrok or localtunnel.
If you have npm installed, then you can do this in a new terminal:
sudo npm install -g localtunnel
lt --port 9292
It will then give you a URL that you can share. Keep in mind these two things:
The URL is only valid as long as the localtunnel process is running
You still need to have your server running on localhost:9292 for it to work.
Did you perhaps listen to localhost only in the config?
You need to bind the host to 0.0.0.0 otherwise it will only only be available locally...

Heroku Boot Timeout (Error R10)

Every time I launch my app it cannot get past the 60 second point without:
2012-05-06T22:41:11+00:00 heroku[web.1]: Stopping process with SIGKILL
2012-05-06T22:41:11+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2012-05-06T22:41:11+00:00 heroku[web.1]: Process exited with status 137
2012-05-06T22:41:12+00:00 heroku[web.1]: State changed from starting to crashed
Here is my Procfile:
web: bundle exec thin start -p $PORT
Any responses will be thoroughly appreciated.
If your app does take longer than 60 seconds for "good" reasons, you can work around the 60s boot time limit with https://github.com/dblock/heroku-forward.
The solution was that I had forgotten to include the -p $PORT in my Procfile line.
in Procfile change:
web: bundle exec thin start
to
web: bundle exec thin start -p $PORT
That fixed it for me.
Heroku's boot timeout bit me too. I read several blog posts about how to get around it and ended up automating some of the solutions into a gem.
To reduce the startup time on deploy, you can trim the gems loaded at boot time (this doesn't mean you have to trim them from the app, just boot time).
gem_bench evaluates which gems are likely to not be needed at boot time.
I have an app with about 250 gems and was able to add :require => false to about 60 of them, with dramatic effects.
https://github.com/acquaintable/gem_bench
Disclaimer: I am the author of this open source ruby gem. I wrote the gem to aid myself in solving this exact problem: the 60 second timeout on Heroku.
Hi i was facing the same issue.I have resolved this issue by increase the timeout in /config/unicorn.rb
change timeout 15 to timeout 20 in /config/unicorn.rb
In my case using nodejs I solved this adding a Procfile file with content:
worker: node index.js and push it to heroku.
After that make sure to disable the check "web npm start" and turn on the check "worker node index.js" just like the image attached below
herokuResourcesConfig
I was having the same error when deploying my Node app on Heroku.
I got it solved by adding a Procfile.
web: node app.js
It tells Heroku how to start the application.
The error is because of Heroku is not able to configure on which PORT to run the application.
It can be solved by specifying the PORT for Heroku, ie: in app.js
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`App is running on port ${ PORT }`);
});
Error R10 (Boot timeout)
is this hidden section of heroku allows you to increase the deployment time.
https://tools.heroku.support/limits/boot_timeout
I got this error because Heroku didn't have access to the Mongo Atlas database. You need to change this in the database settings
have the same issue, solved by creating file with proxy server
https://www.npmjs.com/package/http-proxy#setup-a-basic-stand-alone-proxy-server
proxy.js:
httpProxy.createProxyServer({
target, // target that can't cant be exposed, e.g. localhost:4000
changeOrigin: true,
}).listen(process.env.PORT); // port from heroku runtime
then
node server/proxy.js

How to configure Play not to listen on jpda port when using Heroku?

I'm getting this error when trying to start my play app on Heroku:
heroku[web.1]: Error R11 (Bad bind) -> Process bound to port 8000, should be 46275 (see environment variable PORT)
I think this is play trying to listen on the jpda port, which Heroku prohibits. My Procfile looks like this:
web: play run --http.port=$PORT $PLAY_OPTS
My PLAY_OPTS in heroku config looks like this:
PLAY_OPTS => --%prod -Dprecompiled=true
In my application.conf, mode is set like this:
application.mode=dev
What do I need to do to get this to deploy? Do I need to somehow disable the jpda port, or set the mode to something else?
In your application.conf file you need a line to define prod mode
%prod.application.mode=prod
Try locally to run play run --%prod to be sure in this mode you don't open a jpda port.
It doesn't look like there is a good way to do this in dev mode. In play-1.2.3/framework/pym/play/application.py you can see that it always starts the debug stuff when in dev mode:
if self.readConf('application.mode') == 'dev':
if not self.play_env["disable_check_jpda"]: self.check_jpda()
java_args.append('-Xdebug')
java_args.append('-Xrunjdwp:transport=dt_socket,address=%s,server=y,suspend=n' % self.jpda_port)
java_args.append('-Dplay.debug=yes')

Resources