self-reconnecting Database Connection (less prone to crashing)

This commit is contained in:
Remy Moll 2021-10-07 12:11:00 +02:00
parent a86ac99f7d
commit b5bd9f262a
4 changed files with 33 additions and 47 deletions

View File

@ -62,5 +62,7 @@ class ChatBot():
def start(self):
self.sub_modules = {"clock" : self.commands.clock.Clock(self.db, self.modules["clock"], self.api_art)}
self.add_commands()
self.telegram.start_polling()
self.telegram.start_polling(
poll_interval=0.2,
)
# self.telegram.idle()

View File

@ -12,7 +12,7 @@ class Launcher:
def __init__(self, **modules):
""""""
self.persistence = p_io.PersistentDict("persistence/prst.json")
self.db = p_out.DBLogging
self.db = p_out.DBConnector()
logger.info(self.__class__.__name__ + " initialized")
@ -31,7 +31,7 @@ class Launcher:
logger.info("Starting module "+ module.__class__.__name__)
module.modules = self.modules
module.persistence = self.persistence
module.db = self.db() # newly initialized for every module
module.db = self.db # pooled ie multithreaded
module.start()

View File

@ -1,17 +1,28 @@
from peewee import *
import datetime
# from playhouse.pool import PooledMySQLDatabase
from playhouse.shortcuts import ReconnectMixin
import logging
logger = logging.getLogger(__name__)
from threading import Thread
from . import keys
dbk = keys.db_keys
db = MySQLDatabase(dbk["name"], user=dbk["username"], password=dbk["password"], host=dbk["url"], port=dbk["port"], autorollback=True)
class ReconnectDataBase(ReconnectMixin, MySQLDatabase):
pass
db = ReconnectDataBase(
dbk["name"],
user=dbk["username"],
password=dbk["password"],
host=dbk["url"],
port=dbk["port"],
autorollback=True
)
class DBModel(Model):
# specific to the above DB
class Meta:
@ -28,6 +39,13 @@ class DBModel(Model):
logger.error(e)
# db.atomic().rollback()
def get(self, *query, **filters):
try:
return super().get(*query, **filters)
except:
logger.error("Error while 'getting' from db.")
print(query, filters)
class Metric(DBModel):
time = DateTimeField()
@ -40,19 +58,24 @@ class SensorMetric(Metric):
temperature = IntegerField()
humidity = IntegerField()
luminosity = IntegerField()
default = {"temperature": 100, "humidity": 100, "luminosity": 100}
class ChatMetric(Metric):
read = BooleanField()
send = BooleanField()
execute = BooleanField()
default = {"read": False, "send": False, "execute": False}
class ErrorMetric(Metric):
# same as above
error = TextField()
default = {"error": "SQL connection broke off"}
class List(DBModel):
name = CharField(unique=True)
content = TextField() # unlimited length, use to serialise list into
default = {"content": "SQL connection broke off"}

View File

@ -1,7 +1,7 @@
from . import models
class DBLogging:
class DBConnector:
"""Create a connection to a remote database and log some quantities that will be visualized otherwhere"""
def __init__(self):
self.db = models.db
@ -16,42 +16,3 @@ class DBLogging:
def create_tables(self):
self.db.create_tables([self.sensors, self.chats, self.errors, self.lists])
# writin to the db gets handled through the model directly
# create_tables()
# # read from json, excel, txt ... whatever
# now = dt.datetime.timestamp(dt.datetime.now())
# for i in range(1000):
# with db:
# sensor_data = SensorMetric.create(
# time = now + i,
# temperature = 23,
# humidity = 30 + randint(0,20),
# luminosity = 1
# )
# chat = ChatMetric(
# time = now + i,
# activity = "Hello world"
# )
# errors = ErrorMetric(
# time = now + i,
# error = "Could not load module"
# )