Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # mysqldb.py need this file
- import logging
- import time
- import mysql.connector
- # pip install mysql-connector-python
- # Set up logger
- logger = logging.getLogger(__name__)
- logger.setLevel(logging.INFO)
- formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
- # Log to console
- handler = logging.StreamHandler()
- handler.setFormatter(formatter)
- logger.addHandler(handler)
- # Also log to a file
- file_handler = logging.FileHandler("cpy-errors.log")
- file_handler.setFormatter(formatter)
- logger.addHandler(file_handler)
- def connect_to_mysql(config, attempts=3, delay=2):
- attempt = 1
- # Implement a reconnection routine
- while attempt < attempts + 1:
- try:
- return mysql.connector.connect(**config)
- except (mysql.connector.Error, IOError) as err:
- if (attempts is attempt):
- # Attempts to reconnect failed; returning None
- logger.info("Failed to connect, exiting without a connection: %s", err)
- return None
- logger.info(
- "Connection failed: %s. Retrying (%d/%d)...",
- err,
- attempt,
- attempts-1,
- )
- # progressive reconnect delay
- time.sleep(delay ** attempt)
- attempt += 1
- return None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement