"""Main.""" from logging import INFO from uuid import uuid4 from pebble import ProcessPool import config from connectors.log import logger as log TIMEOUT_SECONDS = 10 def send_loguru_msg(correlation_id=None): """Sends a msg to log via loguru wrapper.""" from owsloguru import get_ows_log if not correlation_id: correlation_id = uuid4() print(f'Created correlation_id: {correlation_id}.') log = get_ows_log() context = {'correlation_id': correlation_id} log = log.bind(**context) log.info('Info log test') log.debug('Debug log test.') log.error('Error log test') return log def send_loguru_multi_msg(msg, correlation_id=None): """Sends a msg to log via loguru wrapper.""" if not correlation_id: correlation_id = uuid4() print(f'Created correlation_id: {correlation_id}.') with log.contextualize(multi=True): log.info(f'Info log test: {msg} - {correlation_id}') log.debug(f'Debug log test: {msg} - {correlation_id}') log.error(f'Error log test: {msg} - {correlation_id}') return log def send_owslogger_msg(correlation_id=None): """Sends a msg via owslogger.""" from owslogger import logger if not correlation_id: correlation_id = uuid4() print(f'Created correlation_id: {correlation_id}.') log = logger.setup( environment=config.ENVIRONMENT, logger_name=config.LOGGER_NAME, logger_level=INFO, service_name=config.APPLICATION_NAME, service_version=config.APP_VERSION, correlation_id=correlation_id, dsn=config.LOGGER_DSN ) log.info('Info log test.') log.debug('Debug log test.') log.error('Error log test') return log def send_logs(iteration, correlation_id): send_loguru_multi_msg(iteration, correlation_id) def task_done(future): try: future.result() # blocks until results are ready except TimeoutError as error: print(f"Function took longer than {error.args[1]} seconds") except Exception as error: print(f"Function raised {error}") print(error.traceback) # traceback of the function def main(): """Main method.""" correlation_id = 'lvona_test_1234' # send_owslogger_msg(correlation_id) # send_loguru_msg(correlation_id) with ProcessPool(max_workers=5, max_tasks=10) as pool: for index in range(1, 6): future = pool.schedule( send_logs, (index, correlation_id)) # noqa - timeout=TIMEOUT_SECONDS) future.add_done_callback(task_done) print("Pool's closed.") print('done.') if __name__ == "__main__": main()