5

I have done a search on StackOverflow looking for an answer that will help me to get my Web API working, but somehow, I still don't have the answer that I need. I have followed all the necessarily steps up till the moment I can deploy the project to heroku (through Github), and it shows no build error. Procfile is already included, so is my requirements.txt. Please see below:

Project Directory:

app-directory/
  |-main-api.py
  |-Procfile
  |-requirements.txt
  |-..other files..

Procfile (Updated):

web: gunicorn --bind 127.0.0.1:5000 main-api:app

requirements.txt:

  • Flask
  • PyMongo
  • Gunicorn

In main-api.py...

@app.route('/', methods=['GET'])
def get_index():
    customers = mongo.db.customers

    output = []

    for c in customers.find():
        output.append({'_id': c['_id'], 'first_name': c['first_name'],
                   'last_name': c['last_name'],
                   'date_of_birth': c['date_of_birth'],
                   'is_online': c['is_online']})

    return jsonify({'results': output})

And finally...running the web project in Heroku itself:

Heroku Web - Python-Flask-Error

Anyone with similar experience or have the right solution to this problem, appreciate if you can share it. Thanks a million folks!

P.S. Sorry that I forgot to add the log from Heroku:

2017-04-24T18:22:17.118866+00:00 heroku[web.1]: Starting process with command `gunicorn --bind 127.0.0.1:5000 main-api:app`
2017-04-24T18:22:20.224956+00:00 app[web.1]: [2017-04-24 18:22:20 +0000] [4] [INFO] Starting gunicorn 19.7.1
2017-04-24T18:22:20.225914+00:00 app[web.1]: [2017-04-24 18:22:20 +0000] [4] [INFO] Listening at: http://127.0.0.1:5000 (4)
2017-04-24T18:22:20.226111+00:00 app[web.1]: [2017-04-24 18:22:20 +0000] [4] [INFO] Using worker: sync
2017-04-24T18:22:20.230648+00:00 app[web.1]: [2017-04-24 18:22:20 +0000] [9] [INFO] Booting worker with pid: 9
2017-04-24T18:22:20.332390+00:00 app[web.1]: [2017-04-24 18:22:20 +0000] [10] [INFO] Booting worker with pid: 10
2017-04-24T18:23:17.510473+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2017-04-24T18:23:17.510556+00:00 heroku[web.1]: Stopping process with SIGKILL
2017-04-24T18:23:17.686086+00:00 heroku[web.1]: State changed from starting to crashed
2017-04-24T18:23:17.667150+00:00 heroku[web.1]: Process exited with status 137
2017-04-24T18:23:19.078229+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=cx-machinelearning.herokuapp.com request_id=1ce56539-862b-4355-ba36-a6453a646890 fwd="101.127.102.75" dyno= connect= service= status=503 bytes= protocol=https
2017-04-24T18:23:19.971719+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=cx-machinelearning.herokuapp.com request_id=55d8cad7-3967-466b-b78c-d9e377be8cbc fwd="101.127.102.75" dyno= connect= service= status=503 bytes= protocol=https

I have checked to ensure that debug=True has been set in the main section of main_api.py.

Sherman Chen
  • 63
  • 1
  • 4

2 Answers2

9

Looks like the Heroku load balancer cannot reach your application because it listens on localhost only (IP address 127.0.0.1). Moreover, Heroku allocates port numbers dynamically and assigns the current value to the PORT environment variable.

Try the following Procfile instead:

web: gunicorn --bind 0.0.0.0:$PORT main-api:app
Adam Byrtek
  • 12,011
  • 2
  • 32
  • 32
  • 1
    Thanks @adam-brytek for the assistance! It works like a charm. Setting this as the answer. I hope someone will benefit from this question. – Sherman Chen Apr 25 '17 at 00:49
  • You're welcome @ShermanChen but looks like you didn't tick the answer mark in the end :) – Adam Byrtek Apr 25 '17 at 23:44
  • 1
    Yeap, it's done. Sorry, its my first time proactively taking part in stackoverflow. Hope to make some contributions of my own in terms of providing answers in other areas. – Sherman Chen Apr 27 '17 at 04:25
  • I'm stuck with the same error for a week and wasted tens of hours online searching for answers! The heroku official document does no help, thank you so much! – mzoz Jan 07 '19 at 03:57
  • 2
    I have used this same but still facing issue? any other options guys? – Ameerudheen.K Jun 07 '20 at 04:22
  • I am having the same error. The solution mentioned in not solving the issue. – Praveen Kumar-M Nov 09 '20 at 03:45
  • not working for me too. followed every step as per the **heroku** docs but getting same error _at=error code=H10 desc="App crashed"_ – Ashish Bharam Jun 16 '21 at 13:21
0

For me the problem was that in my Procfile I wrote:

web: gunicorn appname:app

where appname is the application name I used in Heroku website, instead of:

web: gunicorn MainScriptName:app

That means get yourscriptname.py and just copy it without extension, so you get yourscriptname:app