From: lionel Date: Wed, 30 Dec 2015 09:38:23 +0000 (+0100) Subject: Patch : reworked the way MySQL connection is managed to fix issue with logrotate... X-Git-Url: http://gitweb.enneade.fdn.org/?a=commitdiff_plain;h=fa68d3f40136fe9d5defa28b5ab8e7e30c9d8f5e;p=pyowl.git Patch : reworked the way MySQL connection is managed to fix issue with logrotate or not accessible SQL server --- diff --git a/cm160Server.py b/cm160Server.py index d309b84..8e413d8 100755 --- a/cm160Server.py +++ b/cm160Server.py @@ -696,8 +696,6 @@ class MySQLDataStore(object): def __init__ (self): self.__uo = uo self.__db = "owl" - self.__StoreConn = None - self.__StoreCursor = None self.__displayTime = time.time() self.__ensureStoreExists() @@ -715,11 +713,10 @@ class MySQLDataStore(object): conn, cursor = self.__connect() try: cursor.execute("CREATE TABLE cm160data(ts timestamp, current float, UNIQUE (ts))") - # Attempt to create table in case this is first time to run - # the server; but thereafter we expect an exception because the - # create will fail... + # Attempt to create table in case this is the first time we are launched. + # But thereafter we expect an exception because the create will fail if the table already exist except MySQLdb.OperationalError as e: -# print "Error %d: %s" % (e.args[0], e.args[1]) + # Debug : # print "Error %d: %s" % (e.args[0], e.args[1]) if not(e.args[0] == 1050): raise conn.commit() @@ -727,18 +724,22 @@ class MySQLDataStore(object): self.info("MySQL database OK: %s" % (self.__db) ) def store(self, cm160Data): + StoreConn = None + StoreCursor = None """Store the CM160 data in the MySQL database. There may be duplicate timestamps in the CM160data. If the data was added to the database return True, if not added to the database return False""" - #Create a connection if we don't already have one - if self.__StoreConn == None: - self.__StoreConn, self.__StoreCursor = self.__connect() + #Create a connection + StoreConn, StoreCursor = self.__connect() stored=False try: - self.__StoreCursor.execute("""INSERT INTO cm160data VALUES (%s,%s)""", (cm160Data.getDatetime(),cm160Data.getAmps(),)) + StoreCursor.execute("""INSERT INTO cm160data VALUES (%s,%s)""", (cm160Data.getDatetime(),cm160Data.getAmps(),)) + StoreConn.commit() + StoreConn.close() + stored=True except MySQLdb.Error as e: @@ -747,20 +748,15 @@ class MySQLDataStore(object): # INSERT will fail this way if this is a duplicate # record (as it commonly can be when receiving 30-day # record from CM160 device!) - #self.info("Duplicate record discarded: %s" % (cm160Data) ) + self.info("Duplicate record discarded: %s" % (cm160Data) ) pass else: raise - #Display data every few seconds to let user know download is - #in progress + # Display data every few seconds to let user know download is in progress if self.__displayTime < time.time(): self.info("stored record: %s" % cm160Data) self.__displayTime=time.time()+2 - #We used to commit every record this was very slow when - #downloading 30 days of data on slow (E.G raspberypi) - #platforms. Now we only comit data as it is displayed. - self.__StoreConn.commit() return stored @@ -820,12 +816,11 @@ if __name__=='__main__': sys.exit(0) uo.setLogFile("cm160Server.log") - uo.info("CM160Server v %2.2f" % (VERSION) ) + uo.info("CM160Server v%2.2f" % (VERSION) ) uo.setDebug(debug) # Démarrage du datastore : - DataStore = MySQLDataStore() # Démarrage du collect data