self-reconnecting Database Connection (less prone to crashing)
This commit is contained in:
parent
a86ac99f7d
commit
b5bd9f262a
@ -62,5 +62,7 @@ class ChatBot():
|
|||||||
def start(self):
|
def start(self):
|
||||||
self.sub_modules = {"clock" : self.commands.clock.Clock(self.db, self.modules["clock"], self.api_art)}
|
self.sub_modules = {"clock" : self.commands.clock.Clock(self.db, self.modules["clock"], self.api_art)}
|
||||||
self.add_commands()
|
self.add_commands()
|
||||||
self.telegram.start_polling()
|
self.telegram.start_polling(
|
||||||
|
poll_interval=0.2,
|
||||||
|
)
|
||||||
# self.telegram.idle()
|
# self.telegram.idle()
|
||||||
|
@ -12,7 +12,7 @@ class Launcher:
|
|||||||
def __init__(self, **modules):
|
def __init__(self, **modules):
|
||||||
""""""
|
""""""
|
||||||
self.persistence = p_io.PersistentDict("persistence/prst.json")
|
self.persistence = p_io.PersistentDict("persistence/prst.json")
|
||||||
self.db = p_out.DBLogging
|
self.db = p_out.DBConnector()
|
||||||
|
|
||||||
logger.info(self.__class__.__name__ + " initialized")
|
logger.info(self.__class__.__name__ + " initialized")
|
||||||
|
|
||||||
@ -31,7 +31,7 @@ class Launcher:
|
|||||||
logger.info("Starting module "+ module.__class__.__name__)
|
logger.info("Starting module "+ module.__class__.__name__)
|
||||||
module.modules = self.modules
|
module.modules = self.modules
|
||||||
module.persistence = self.persistence
|
module.persistence = self.persistence
|
||||||
module.db = self.db() # newly initialized for every module
|
module.db = self.db # pooled ie multithreaded
|
||||||
module.start()
|
module.start()
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,17 +1,28 @@
|
|||||||
from peewee import *
|
from peewee import *
|
||||||
import datetime
|
# from playhouse.pool import PooledMySQLDatabase
|
||||||
|
from playhouse.shortcuts import ReconnectMixin
|
||||||
import logging
|
import logging
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
from threading import Thread
|
|
||||||
|
|
||||||
from . import keys
|
from . import keys
|
||||||
dbk = keys.db_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):
|
class DBModel(Model):
|
||||||
# specific to the above DB
|
# specific to the above DB
|
||||||
class Meta:
|
class Meta:
|
||||||
@ -28,6 +39,13 @@ class DBModel(Model):
|
|||||||
logger.error(e)
|
logger.error(e)
|
||||||
# db.atomic().rollback()
|
# 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):
|
class Metric(DBModel):
|
||||||
time = DateTimeField()
|
time = DateTimeField()
|
||||||
@ -40,19 +58,24 @@ class SensorMetric(Metric):
|
|||||||
temperature = IntegerField()
|
temperature = IntegerField()
|
||||||
humidity = IntegerField()
|
humidity = IntegerField()
|
||||||
luminosity = IntegerField()
|
luminosity = IntegerField()
|
||||||
|
default = {"temperature": 100, "humidity": 100, "luminosity": 100}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ChatMetric(Metric):
|
class ChatMetric(Metric):
|
||||||
read = BooleanField()
|
read = BooleanField()
|
||||||
send = BooleanField()
|
send = BooleanField()
|
||||||
execute = BooleanField()
|
execute = BooleanField()
|
||||||
|
default = {"read": False, "send": False, "execute": False}
|
||||||
|
|
||||||
|
|
||||||
class ErrorMetric(Metric):
|
class ErrorMetric(Metric):
|
||||||
# same as above
|
# same as above
|
||||||
error = TextField()
|
error = TextField()
|
||||||
|
default = {"error": "SQL connection broke off"}
|
||||||
|
|
||||||
|
|
||||||
class List(DBModel):
|
class List(DBModel):
|
||||||
name = CharField(unique=True)
|
name = CharField(unique=True)
|
||||||
content = TextField() # unlimited length, use to serialise list into
|
content = TextField() # unlimited length, use to serialise list into
|
||||||
|
default = {"content": "SQL connection broke off"}
|
@ -1,7 +1,7 @@
|
|||||||
from . import models
|
from . import models
|
||||||
|
|
||||||
|
|
||||||
class DBLogging:
|
class DBConnector:
|
||||||
"""Create a connection to a remote database and log some quantities that will be visualized otherwhere"""
|
"""Create a connection to a remote database and log some quantities that will be visualized otherwhere"""
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.db = models.db
|
self.db = models.db
|
||||||
@ -16,42 +16,3 @@ class DBLogging:
|
|||||||
def create_tables(self):
|
def create_tables(self):
|
||||||
self.db.create_tables([self.sensors, self.chats, self.errors, self.lists])
|
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"
|
|
||||||
# )
|
|
Loading…
x
Reference in New Issue
Block a user