self-reconnecting Database Connection (less prone to crashing)
This commit is contained in:
		| @@ -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 | ||||
|     content = TextField() # unlimited length, use to serialise list into | ||||
|     default = {"content": "SQL connection broke off"} | ||||
| @@ -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" | ||||
| #         ) | ||||
		Reference in New Issue
	
	Block a user
	 Remy Moll
					Remy Moll