Exemplo n.º 1
0
    def execute(self, query):
        log.debug(u'[DB thread %s] processing execute(\"%s\")' % (self.__number, query))
        if self.__config[u'DB'][u'TYPE'] == 'mysql':
            from mysql.connector import Error as MySQLError
            from mysql.connector import ProgrammingError as MySQLProgrammingError

            try:
                cursor = self.__connector.cursor()
                affected_rows = cursor.execute(query)
                self.__connector.commit()
                self.__err_count_db = 0
                self.__err_count_sql = 0
                return affected_rows
            except MySQLProgrammingError, e:
                self.__err_count_sql += 1
                log.error(u'[DB thread %s] MySQL programming exception at execute("%s"): %s' %
                          (self.__number, query, e))
                return False
            except MySQLError, e:
                self.__err_count_sql += 1
                log.error(u'[DB thread %s] MySQL exception at execute("%s"): %s' % (self.__number, query, e))
                self.__connector.disconnect()
                self.__db_connect()
                if self.__err_count_db < self.__config[u'DB'][u'ERRRETRY']:
                    if self.__err_count_sql < self.__config[u'DB'][u'ERRRETRY']:
                        if self.__err_count_db == 0:
                            log.error(u'[DB thread %s] Next try in %d seconds...' %
                                      (self.__number, self.__config[u'DB'][u'ERRDELAY']))
                            __sleep__(self.__config[u'DB'][u'ERRDELAY'])
                        return self.execute(query)
                    else:
                        self.__err_count_sql = 0
                        return False
                else:
                    return False
Exemplo n.º 2
0
    def __init__(self, config_file='isida-plus.cfg'):
        self.__cfg_file = config_file
        if not self.__threads:
            cfg = ConfigParser.RawConfigParser()
            cfg.read(self.__cfg_file)

            try:
                # Define debug options
                self.__threads_count = cfg.getint(u'DB', u'THREADS')

                # Define logging parameters
                log.setLevel(cfg.getint(u'LOGS', u'DEBUG_LVL'))
                fh = logging.FileHandler(u'%s/%s.log' %
                                         (cfg.get(u'LOGS', u'LOG_DIR'), cfg.get(u'LOGS', u'PREFIX_DB')))
                fh.setLevel(cfg.getint(u'LOGS', u'DEBUG_LVL'))
                formatter = logging.Formatter(u'[%(asctime)s %(levelname)s] %(message)s')
                fh.setFormatter(formatter)
                log.addHandler(fh)

            except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
                pass
            except IOError, e:
                print(u'IOError exception with get config values in IsidaSQL.__init__(): %s' % e)

            # Starting SQL connectors
            for th in range(0, self.__threads_count):
                self.__threads.append({'object': IsidaSQLThread(self.__cfg_file, th)})
                self.__threads[th]['thread'] = threading.Thread(target=self.__threads[th]['object'].run)
                self.__threads[th]['thread'].start()
                __sleep__(0.5)
Exemplo n.º 3
0
    def __db_connect(self):
        if self.__config[u'DB'][u'TYPE'] == 'mysql':
            import mysql.connector as mysqldb

            try:
                self.__connector = mysqldb.connect(
                    database=self.__config[u'DB'][u'DBNAME'],
                    user=self.__config[u'DB'][u'DBUSER'],
                    host=self.__config[u'DB'][u'HOST'],
                    password=self.__config[u'DB'][u'DBPASS'],
                    port=self.__config[u'DB'][u'PORT']
                )
                self.__err_count_db = 0
            except mysqldb.Error, e:
                self.__err_count_db += 1
                log.error(u'[DB thread %s] MySQL exception #%d at __db_connect(): %s' %
                          (self.__number, self.__err_count_db, e))
                if self.__err_count_db < self.__config[u'DB'][u'ERRRETRY']:
                    log.error(u'[DB thread %s] Next try in %d seconds...' %
                              (self.__number, self.__config[u'DB'][u'ERRDELAY']))
                    __sleep__(self.__config[u'DB'][u'ERRDELAY'])
                    self.__db_connect()
                else:
                    log.error(u'[DB thread %s] Reached limit for exceptions at __db_connect(). Execution stops.' %
                              self.__number)
                    return False
Exemplo n.º 4
0
 def start(self):
     if len(self.__threads) == 0:
         for th in range(0, self.__threads_count):
             self.__threads.append({'object': IsidaSQLThread(self.__cfg_file, th)})
             self.__threads[th]['thread'] = threading.Thread(target=self.__threads[th]['object'].run)
             self.__threads[th]['thread'].start()
             __sleep__(0.5)
         return True
     else:
         log.error(u'Can not start new connections to the DB. They are already exists!')
         return False
Exemplo n.º 5
0
    def run(self):
        delay = 60
        if self.__config['DB']['ERRDELAY'] >= 10:
            delay = self.__config['DB']['ERRDELAY']

        # Infinite loop
        while not self.kill_flag:
            __sleep__(delay)

        # Disconnect from the database
        self.__connector.disconnect()
        log.debug(u'[Old DB thread %s] Has been stopped.' % self.__number)