From 06a3658b94d78296797b9f2af2d12d73f2c54932 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 12 May 2025 11:14:34 -0500 Subject: Handle exceptions within the `with` to prevent silent failure One, or both of the context managers is "swallowing" exceptions, leading to silent failures. This change manually handles the exceptions within the context manager to avoid such silent failures. --- scripts/rqtl2/entry.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'scripts/rqtl2/entry.py') diff --git a/scripts/rqtl2/entry.py b/scripts/rqtl2/entry.py index 327ed2c..e837aa8 100644 --- a/scripts/rqtl2/entry.py +++ b/scripts/rqtl2/entry.py @@ -24,16 +24,17 @@ def build_main( ) -> Callable[[],int]: """Build a function to be used as an entry-point for scripts.""" def main(): - try: logging.basicConfig( format=( "%(asctime)s - %(levelname)s %(name)s: " "(%(pathname)s: %(lineno)d) %(message)s"), level=args.loglevel) logger = logging.getLogger(loggername) - with (Redis.from_url(args.redisuri, decode_responses=True) as rconn, - database_connection(args.databaseuri) as dbconn): - fqjobid = jobs.job_key(args.redisprefix, args.jobid) + with (Redis.from_url(args.redisuri, decode_responses=True) as rconn, + database_connection(args.databaseuri) as dbconn): + fqjobid = jobs.job_key(args.redisprefix, args.jobid) + + try: rconn.hset(fqjobid, "status", "started") logger.addHandler(setup_redis_logger( rconn, @@ -54,9 +55,9 @@ def build_main( return returncode rconn.hset(fqjobid, "status", "completed:error") return returncode - except Exception as _exc:# pylint: disable=[broad-except] - logger.error("The process failed!", exc_info=True) - rconn.hset(fqjobid, "status", "completed:error") - return 4 + except Exception as _exc:# pylint: disable=[broad-except] + logger.error("The process failed!", exc_info=True) + rconn.hset(fqjobid, "status", "completed:error") + return 4 return main -- cgit v1.2.3