Fix an issue with MySQL dropping connection after some time

This commit is contained in:
Lucas Verney 2018-06-27 22:11:30 +02:00
parent b097bc2d2a
commit 01fa63f916
2 changed files with 14 additions and 0 deletions

View File

@ -14,6 +14,8 @@ from server.models import db, Report
def init():
db.connect()
db.create_tables([Report])
if not db.is_closed():
db.close()
# Use DateAwareJSONEncoder to dump JSON strings
# From http://stackoverflow.com/questions/21282040/bottle-framework-how-to-return-datetime-in-json-response#comment55718456_21282666. pylint: disable=locally-disabled,line-too-long

View File

@ -6,6 +6,7 @@ Models and database definition
import os
import arrow
import bottle
import peewee
from playhouse.db_url import connect
from playhouse.shortcuts import model_to_dict
@ -13,6 +14,17 @@ from playhouse.shortcuts import model_to_dict
db = connect(os.environ.get('DATABASE', 'sqlite:///reports.db'))
@bottle.hook('before_request')
def _connect_db():
db.connect()
@bottle.hook('after_request')
def _close_db():
if not db.is_closed():
db.close()
class BaseModel(peewee.Model):
"""
Common base class for all models