From 9dcab958bdaadb2787027b9b10af362a0bc2f861 Mon Sep 17 00:00:00 2001 From: Marko Kreen Date: Wed, 14 Oct 2009 16:47:31 +0300 Subject: skytools.DBScript: safer pidfile writing - signal_pidfile: Clarify ValueError error message. Thrown usually on empty pidfiles, the error messega will now mention the pidfile name. - run_single_process: restructure pidfile writing, so that the pidfile is removed if .write() failed, but not when open() failed. This should avoid the chance that empty pidfiles are hanging around. --- python/skytools/scripting.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'python/skytools/scripting.py') diff --git a/python/skytools/scripting.py b/python/skytools/scripting.py index 83e050eb..0d4b0fab 100644 --- a/python/skytools/scripting.py +++ b/python/skytools/scripting.py @@ -42,7 +42,7 @@ def signal_pidfile(pidfile, sig): """Send a signal to process whose ID is located in pidfile. Read only first line of pidfile to support multiline - pifiles like postmaster.pid. + pidfiles like postmaster.pid. Returns True is successful, False if pidfile does not exist or process itself is dead. Any other errors will passed @@ -58,6 +58,8 @@ def signal_pidfile(pidfile, sig): except OSError, ex: if ex.errno != errno.ESRCH: raise + except ValueError, ex: + raise ValueError('Corrupt pidfile: %s' % pidfile) return False # @@ -90,12 +92,6 @@ def daemonize(): # Pidfile locking+cleanup & daemonization combined # -def _write_pidfile(pidfile): - pid = os.getpid() - f = open(pidfile, 'w') - f.write(str(pid)) - f.close() - def run_single_process(runnable, daemon, pidfile): """Run runnable class, possibly daemonized, locked on pidfile.""" @@ -107,17 +103,23 @@ def run_single_process(runnable, daemon, pidfile): else: print("Ignoring stale pidfile") - # daemonize if needed and write pidfile + # daemonize if needed if daemon: daemonize() - if pidfile: - _write_pidfile(pidfile) - - # run and clean pidfile later + + # clean only own pidfile + own_pidfile = False + try: + if pidfile: + f = open(pidfile, 'w') + own_pidfile = True + f.write(str(os.getpid())) + f.close() + runnable.run() finally: - if pidfile: + if own_pidfile: try: os.remove(pidfile) except: pass -- cgit v1.2.3