即使我使用PORT环境变量,Heroku H10的启动超时

时间:2019-09-04 19:09:54

标签: python github heroku flask

大约6个月前,我使用Cloud 9构建了Flask应用,并试图在Heroku上运行。

我将代码推送到GitHub,然后按照步骤将其发布到Heroku。但是,每当我尝试部署此应用程序时,该应用程序都会失败,并且会出现应用程序错误:

2019-09-04T19:05:14.000000+00:00 app[api]: Build succeeded
2019-09-04T19:06:11.452568+00:00 heroku[web.1]: State changed from starting to crashed
2019-09-04T19:06:11.352975+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2019-09-04T19:06:11.353092+00:00 heroku[web.1]: Stopping process with SIGKILL
2019-09-04T19:06:11.442403+00:00 heroku[web.1]: Process exited with status 137
2019-09-04T19:06:14.530145+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=programplanner.herokuapp.com request_id=4ae3d531-3fbb-4e9d-94da-39318e81ebec fwd="86.177.219.206" dyno= connect= service= status=503 bytes= protocol=https
2019-09-04T19:06:15.165632+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=programplanner.herokuapp.com request_id=d6b266be-4232-4a87-9ecc-90a756dd91d2 fwd="86.177.219.206" dyno= connect= service= status=503 bytes= protocol=https

这是我的app.py文件:

import os
from flask import Flask, render_template, redirect, request, url_for
from flask_pymongo import PyMongo
from bson.objectid import ObjectId

app = Flask(__name__)
app.config["MONGO_DBNAME"] = 'tv-planner'
app.config[
    "MONGO_URI"] = 'mongodb://<user>:<password>@ds125723.mlab.com:25723/tv-planner'

mongo = PyMongo(app)


@app.route('/')
@app.route('/get_programs')
def get_programs():
    return render_template("programs.html",
                           programs=mongo.db.programs.find())


@app.route('/add_program')
def add_program():
    return render_template('addprogram.html',
                           categories=mongo.db.categories.find())


@app.route('/insert_program', methods=['POST'])
def insert_program():
    programs = mongo.db.programs
    programs.insert_one(request.form.to_dict())
    return redirect(url_for('get_programs'))


@app.route('/edit_program/<program_id>')
def edit_program(program_id):
    the_program = mongo.db.programs.find_one({"_id": ObjectId(program_id)})
    all_categories = mongo.db.categories.find()
    return render_template('editprogram.html', program=the_program,
                           categories=all_categories)


@app.route('/update_program/<program_id>', methods=['POST'])
def update_program(program_id):
    programs = mongo.db.programs
    programs.update({'_id': ObjectId(program_id)},
                    {
                        'program_name': request.form.get['program_name'],
                        'category_name': request.form.get['category_name'],
                        'program_description': request.form.get[
                            'program_description'],
                        'date_on': request.form.get['date_on'],
                        'must_watch': request.form.get['must_watch']
                    })
    return redirect(url_for('get_programs'))


@app.route('/delete_program/<program_id>')
def delete_program(program_id):
    mongo.db.programs.remove({'_id': ObjectId(program_id)})
    return redirect(url_for('get_programs'))


@app.route('/get_categories')
def get_categories():
    return render_template('categories.html',
                           categories=mongo.db.categories.find())


@app.route('/edit_category/<category_id>')
def edit_category(category_id):
    return render_template('editcategory.html',
                           category=mongo.db.categories.find_one(
                               {'_id': ObjectId(category_id)}))


@app.route('/update_category/<category_id>', methods=['POST'])
def update_category(category_id):
    mongo.db.categories.update(
        {'_id': ObjectId(category_id)},
        {'category_name': request.form['category_name']})
    return redirect(url_for('get_categories'))


@app.route('/delete_category/<category_id>')
def delete_category(category_id):
    mongo.db.categories.remove({'_id': ObjectId(category_id)})
    return redirect(url_for("get_categories"))


@app.route('/insert_category', methods=['POST'])
def insert_category():
    categories = mongo.db.categories
    category_doc = {'category_name': request.form['category_name']}
    categories.insert_one(category_doc)
    return redirect(url_for('get_categories'))


@app.route('/new_category')
def new_category():
    return render_template('addcategory.html')


if __name__ == '__main__':
    app.run(host=os.environ.get('IP'),
            port=int(os.environ.get('PORT')),
            debug=True)

我的Procfile包含

web: python app.py

1 个答案:

答案 0 :(得分:0)

您正在尝试侦听IP环境变量给定的IP地址,但这不是Heroku提供的。尝试改用0.0.0.0监听所有可用的IP地址:

if __name__ == '__main__':
    app.run(host='0.0.0.0',
            port=int(os.environ.get('PORT')),
            debug=True)