def main(): """Process agent data. Args: None Returns: None """ # Initialize key variables use_mysql = True global POOL global URL pool_timeout = 30 pool_recycle = min(10, pool_timeout - 10) # Get configuration config = Config() # Define SQLAlchemy parameters from configuration pool_size = config.db_pool_size() max_overflow = config.db_max_overflow() # Create DB connection pool if use_mysql is True: URL = ('mysql+pymysql://{}:{}@{}/{}?charset=utf8mb4'.format( config.db_username(), config.db_password(), config.db_hostname(), config.db_name())) # Fix for multiprocessing on pools. # _add_engine_pidguard(QueuePool) # Add MySQL to the pool db_engine = create_engine(URL, echo=False, echo_pool=False, encoding='utf8', poolclass=QueuePool, max_overflow=max_overflow, pool_size=pool_size, pool_pre_ping=True, pool_recycle=pool_recycle, pool_timeout=pool_timeout) # Fix for multiprocessing on engines. # _add_engine_pidguard(db_engine) # Ensure connections are disposed before sharing engine. db_engine.dispose() # Create database session object POOL = scoped_session( sessionmaker(autoflush=True, autocommit=False, bind=db_engine)) else: POOL = None
def _mysql(): """Create database tables. Args: None Returns: None """ # Initialize key variables config = Config() pool_size = config.db_pool_size() max_overflow = config.db_max_overflow() # Add MySQL to the pool engine = create_engine(URL, echo=True, encoding='utf8', max_overflow=max_overflow, pool_size=pool_size, pool_recycle=3600) # Try to create the database print('??: Attempting to Connect to configured database.') try: sql_string = ('''\ ALTER DATABASE {} CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci\ '''.format(config.db_name())) engine.execute(sql_string) except: log_message = ('''\ ERROR: Cannot connect to database "{}" on server "{}". Verify database server \ is started. Verify database is created. Verify that the configured database \ authentication is correct.'''.format(config.db_name(), config.db_hostname())) log.log2die(20086, log_message) # Apply schemas print('OK: Database connected.') print('??: Attempting to create database tables.') BASE.metadata.create_all(engine) print('OK: Database tables created.')