Flask 不从新 sql 数据更新数据 - Nginx、Uwsgi、Centos7,Mysql

Flask Not updating data from new sql data - Nginx, Uwsgi, Centos7, Mysql

我有一个网络报告工具,是我用 Flask 创建的,它连接到 mysql 数据库,该数据库全天从 phone 系统接收数据。我让这个报告工具工作得非常好(在您刷新屏幕或登录时更新数字。)但是现在自从我添加了 2 个查询并稍微更改了我的代码后,我仍然能够访问该站点,但它只是不会像以前那样更新它的数字。重新启动我的 uwsgi 服务和 nginx 后,数字是正确的,但数字不会随着时间的推移而更新,并且直到服务和 nginx 重新启动后才会进入。在部署和服务器维护方面,我是个菜鸟。我不确定是否需要在我的代码中的某处关闭我的 sql 连接并重新建立连接以便我们收到更新的号码? 这是我的代码,我将展示 uwsgi.py、我的 main.py 的某些部分(特别是我添加了 2 个查询的地方)以及我的 nginx.conf ...给你.. . 这只是该项目的骨架。我真的不认为它在我的代码中购买我可能是错的。 Main.py

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret'
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://root@localhost:3306/asteriskcdrdb"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)
login_manager = LoginManager()
login_manager.init_app(app)
engine = db.engine
connection = engine.connect()

location_to_phone = {
    "TX-Billing": "5123597546",
    "TX-Bee Cave": "5123668568",
    "TX-Central Austin": "5124543781",
    "TX-North Austin": "5128373376",
    "TX-Pflugerville": "5122523700",
    "TX-San Antonio": "2106160448",
    "TX-Steiner Ranch": "5122660007",
    "LA-Baton Rouge": "2553039500",
    "LA-Bossier City": "3187425124",
    "La-Lafayette": "3378392773",
    "La-Old Metairie": "5048362050",
    "La-Shreveport": "3186862021",
    "LA-Uptown": "5048975899"
}

location_to_center = {
    "TX-Billing": {"Front Desk": "7000", "Medical": "7001"},
    "TX-Bee Cave": {"Front Desk": "7040", "Medical": "7041"},
    "TX-Central Austin": {"Front Desk": "7050", "Medical": "7051"},
    "TX-North Austin": {"Front Desk": "6000", "Medical": "7031"},
    "Tx-Pflugerville": {"Front Desk": "7070", "Medical": "7071"},
    "Tx-San Antonio": {"Front Desk": "7060", "Medical": "7061"},
    "Tx-Steiner Ranch": {"Front Desk": "7120", "Medical": "7121"},
    "LA-Baton Rouge": {"Front Desk": "7080", "Medical": "7081"},
    "LA-Bossier City": {"Front Desk": "0", "Medical": "0"},
    "La-Lafayette": {"Front Desk": "7100", "Medical": "7101"},
    "La-Old Metairie": {"Front Desk": "0", "Medical": "0"},
    "La-Shreveport": {"Front Desk": "0", "Medical": "0"},
    "LA-Uptown": {"Front Desk": "0", "Medical": "0"}
}

def reports():
    if current_user.is_authenticated:
        print("Authenticated")
    else:
        return redirect(url_for('login'))

    form = ReportConfig(prefix='a')
    form2 = Details(prefix='b')
    form3 = ReportConfig2(prefix='c')

    start_date = datetime.today().strftime('%Y-%m-%d')
    end_date = datetime.now().strftime("%Y-%m-%d")

    totals = []
    answered = []
    no_answer = []
    average = []
    client_num = []
    medicals = []
    fronts = []

    calls = 0
    notan = 0
    ans = 0


    for (loc, num), (site, data) in zip(location_to_phone.items(),location_to_center.items()):
        md = data['Medical']
        fd = data['Front Desk']
        test = num


        totalcalls = connection.execute(f"SELECT COUNT(calldate) FROM cdr WHERE did='{test}' AND NOT lastapp='background' and calldate between '{start_date} 08:00:00' and '{end_date} 23:59:59'")
        answered_count = connection.execute(f"SELECT COUNT(calldate) FROM cdr WHERE did='{test}' AND NOT lastapp='background' AND disposition='ANSWERED' and calldate between '{start_date} 08:00:00' and '{end_date} 23:59:59'")
        no = connection.execute(f"SELECT COUNT(calldate) FROM cdr WHERE did='{test}' AND NOT lastapp='background' AND disposition='NO ANSWER' and calldate between '{start_date} 08:00:00' and '{end_date} 23:59:59'")
        av = connection.execute(f"SELECT duration from cdr WHERE did='{test}' AND NOT lastapp='background' AND calldate between '{start_date} 08:00:00' AND '{end_date} 23:59:59'")
        medical = connection.execute(f"SELECT COUNT(calldate) FROM cdr WHERE did='{test}' AND NOT lastapp='background' and dst='{md}' and calldate between '{start_date} 08:00:00' and '{end_date} 23:59:59'")
        front_desk = connection.execute(f"SELECT COUNT(calldate) FROM cdr WHERE did='{test}' AND NOT lastapp='background' and dst='{fd}' and calldate between '{start_date} 08:00:00' and '{end_date} 23:59:59'")
        answer_num = [row[0] for row in answered_count]

        nono = [row[0] for row in no]

        total = [row[0] for row in totalcalls]

        med = [row[0] for row in medical]
        front = [row[0] for row in front_desk]


        sum = 0
        count = total[0]

        calls = calls + total[0]
        notan = notan + nono[0]
        ans = ans + answer_num[0]
        for x in av:
            sum = sum + x[0]

        try:
            avg = (sum/count)
        except:
            avg = 0
        # avg = (sum/count)
        average.append(round(avg, 2))
        totals.append(total[0])
        answered.append(answer_num[0])
        no_answer.append(nono[0])
        client_num.append(test)
        medicals.append(med[0])
        fronts.append(front[0])

return render_template('reports.html', start_date = start_date, month_date = start_date, assign=assign, form=form, form2=form2,  form3=form3, end_date = end_date, all_calls=calls, all_answered=ans, not_answered=notan, location=zip(totals, location_to_phone, answered, no_answer, average, client_num, medicals, fronts))

wsgi.py

from main import app as application

if __name__ == '__main__':
    application.run()

itinapinch_rep.ini

[uwsgi]
module = wsgi


master = true
processes = 5


socket = itinapinch_rep.sock
chmod-socket = 660
vacuum = true


die-on-term = true

itinapinch_rep.service

[Unit]
Description=uWSGI instance to serve itinapinch_rep
After=network.target


[Service]
User=reports
Group=nginx
WorkingDirectory=/home/reports/itinapinch_rep
Environment="PATH=/home/reports/itinapinch_rep/it_venv/bin"
ExecStart=/home/reports/itinapinch_rep/it_venv/bin/uwsgi --ini itinapinch_rep.ini

[Install]
WantedBy=multi-user.target

根据 docs 对于 sqlalchemy.engine.Connection:

The Connection object represents a single dbapi connection checked out from the connection pool. In this state, the connection pool has no affect upon the connection, including its expiration or timeout state. For the connection pool to properly manage connections, connections should be returned to the connection pool (i.e. connection.close()) whenever the connection is not in use_.

因此,与其在导入时创建为全局连接,不如在每个请求的 life-cycle 内创建和关闭连接。