From 418d62b34c38a532028428e1d51c8088b7558d4a Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Sun, 2 May 2021 22:16:29 -0400 Subject: [PATCH] fix: explain failure to open new log files --- src/plotman/manager.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/plotman/manager.py b/src/plotman/manager.py index 1116ea98..06ed2947 100644 --- a/src/plotman/manager.py +++ b/src/plotman/manager.py @@ -126,11 +126,27 @@ def maybe_start_new_plot(dir_cfg, sched_cfg, plotting_cfg): logmsg = ('Starting plot job: %s ; logging to %s' % (' '.join(plot_args), logfile)) - # start_new_sessions to make the job independent of this controlling tty. - p = subprocess.Popen(plot_args, - stdout=open(logfile, 'w'), - stderr=subprocess.STDOUT, - start_new_session=True) + try: + open_log_file = open(logfile, 'w') + except FileNotFoundError as e: + message = ( + f'Unable to open log file. Verify that the directory exists' + f' and has proper write permissions: {logfile!r}' + ) + raise Exception(message) from e + + # Preferably, do not add any code between the try block above + # and the with block below. IOW, this space intentionally left + # blank... As is, this provides a good chance that our handle + # of the log file will get closed explicitly while still + # allowing handling of just the log file opening error. + + with open_log_file: + # start_new_sessions to make the job independent of this controlling tty. + p = subprocess.Popen(plot_args, + stdout=open_log_file, + stderr=subprocess.STDOUT, + start_new_session=True) psutil.Process(p.pid).nice(15) return (True, logmsg)