![](/img/trans.png)
[英]The connection “database” doesn't exist Django, CentOS7, NGINX, UWSGI
[英]Flask Not updating data from new sql data - Nginx, Uwsgi, Centos7, Mysql
我有一個使用燒瓶創建的Web報告工具,該工具連接到mysql數據庫,該數據庫全天從電話系統接收數據。 我有這個報告工具,可以很好地工作(在刷新屏幕或登錄時更新數字。)但是現在,自從我添加了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
根據sqlalchemy.engine.Connection
的文檔 :
Connection對象表示從連接池中檢出的單個dbapi連接。 在這種狀態下,連接池不會影響連接,包括連接的到期或超時狀態。 為了使連接池正確管理連接,只要連接不在use_中,就應將連接返回到連接池(即connection.close())。
因此,應該在每個請求的生命周期內創建並關閉連接,而不是在導入時將其創建為全局連接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.