Вопрос

I am writing a small websites using Flask, the database is mysql and using flask-sqlalchemy to handle it. but I am stuck by a strange 502 error, I do have search a lot via Google and debug for quite a long time but still fail. Wish you guys can help.

In Debug mode, everything works fine, but when setting app.debug = False, it will return 502 error.

I have debug this for quite a long time and one of the useful finding is that the 502 error occurs when running the db.session.commit().

I am not sure whether this problems is related to the jsonify.

Part of the code:

@app.route('/sell/update', methods=('GET', 'POST'))
@login_required
def sell_update():
    """docstring for sell_update"""
    res = {}
    # It should be call with post method
    id = int(request.form.get('id', 0))
    status = int(request.form.get('status', 0))
    sell = lib.get_sell_by_id(id)
    if sell.user.id != g.user.id:
        res['error'] = MSG_SELL_PERMISSION_INVALID
    if not id or not sell:
        res['error'] = MSG_SELL_ID_INVALID
    res['status'] = res.get('error') and 'ERROR' or 'OK'
    sell.status = status
    # return 'TEST' # this line works fine in both debug and production
    db.session.commit() # Error occurs right here
    return jsonify(**res)

Any ideas and suggestions are welcome. Thanks in advance.

Это было полезно?

Решение

Finally, I solved the problem myself by coincidence.

The problem is that I also use flask-whooshalchemy for search feature and the whoosh need to write on the index file.

When running debug mode, the index file is created with ownership of current user, but when deploy the production mode, the user is www-data (with nginx in Ubuntu) so it have no permission to write.

The key point of finding this problem is to use try except and return the message of the exception as the return of views. Without this, we can not see any trackback or something like that in production deployment.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top