Skip to content

Commit

Permalink
Merge pull request hpcugent#80 from stdweird/no_close_on_report
Browse files Browse the repository at this point in the history
handle fpy3 file exception
  • Loading branch information
itkovian authored Apr 15, 2021
2 parents 2f2a18b + ad11de2 commit e772ce7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 25 deletions.
6 changes: 3 additions & 3 deletions lib/vsc/utils/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import time

from vsc.utils import fancylogger
from vsc.utils.py2vs3 import pickle
from vsc.utils.py2vs3 import pickle, FileNotFoundErrorExc


class FileCache(object):
Expand Down Expand Up @@ -82,7 +82,7 @@ def __init__(self, filename, retain_old=True, raise_unpickable=False):
try:
g = gzip.GzipFile(mode='rb', fileobj=f) # no context manager available in python 26 yet
s = g.read()
except IOError as err:
except (IOError) as err:
self.log.error("Cannot load data from cache file %s as gzipped json", self.filename)
try:
f.seek(0)
Expand All @@ -106,7 +106,7 @@ def __init__(self, filename, retain_old=True, raise_unpickable=False):
finally:
g.close()

except (OSError, IOError, ValueError) as err:
except (OSError, IOError, ValueError, FileNotFoundErrorExc) as err:
self.log.warning("Could not access the file cache at %s [%s]", self.filename, err)
self.shelf = {}
self.log.info("Cache in %s starts with an empty shelf", (self.filename,))
Expand Down
4 changes: 2 additions & 2 deletions lib/vsc/utils/nagios.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

from vsc.utils.cache import FileCache
from vsc.utils.fancylogger import getLogger
from vsc.utils.py2vs3 import is_string
from vsc.utils.py2vs3 import is_string, FileNotFoundErrorExc

log = getLogger(__name__)

Expand Down Expand Up @@ -316,7 +316,7 @@ def cache(self, nagios_exit, nagios_message):
else:
self.log.warn("Not running as root: Cannot chown the nagios check file %s to %s",
self.filename, self.nagios_username)
except OSError:
except (OSError, FileNotFoundErrorExc):
self.log.raiseException("Cannot chown the nagios check file %s to the nagios user" % (self.filename))

return True
Expand Down
14 changes: 7 additions & 7 deletions lib/vsc/utils/pickle_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@
@author: Stijn De Weirdt (Ghent University)
"""

import logging
import os
import stat

from vsc.utils import fancylogger
from vsc.utils.py2vs3 import pickle

from vsc.utils.py2vs3 import pickle, FileNotFoundErrorExc


class TimestampPickle(object):
"""Stores a timestamp in some format in a file."""

def __init__(self, filename):
self.filename = filename
self.log = fancylogger.getLogger(self.__class__.__name__)

def read(self):
"""Read a timestamp value from a pickled file.
Expand All @@ -61,8 +61,8 @@ def read(self):

try:
timestamp = pickle.load(open(self.filename, "rb"))
except (IOError, OSError):
self.log.exception("Failed to load timestamp pickle from filename %s.", self.filename)
except (IOError, OSError, FileNotFoundErrorExc):
logging.exception("Failed to load timestamp pickle from filename %s.", self.filename)
return None

return timestamp
Expand All @@ -80,11 +80,11 @@ def write(self, timestamp):
try:
pickle.dump(timestamp, open(self.filename, "wb"))
except Exception:
self.log.exception("Failed to dump timestamp %s to pickle in filename %s", timestamp, self.filename)
logging.exception("Failed to dump timestamp %s to pickle in filename %s", timestamp, self.filename)
raise

try:
os.chmod(self.filename, stat.S_IRWXU)
except Exception:
self.log.exception("Failed to set permissions on filename %s", self.filename)
logging.exception("Failed to set permissions on filename %s", self.filename)
raise
20 changes: 9 additions & 11 deletions lib/vsc/utils/timestamp_pid_lockfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@
@author: Andy Georges (Ghent University)
"""
import errno
import logging
import os
import signal
import time

from lockfile.linklockfile import LockBase, LockFailed, NotLocked, NotMyLock

from vsc.utils import fancylogger

from vsc.utils.py2vs3 import FileNotFoundErrorExc, FileExistsErrorExc

class LockFileReadError(Exception):
'''Exception raised when we cannot get the expected information from the lock file.'''
Expand All @@ -54,7 +53,6 @@ def __init__(self, path, threshold=60):
'''Intializer.'''
LockBase.__init__(self, path, False)
self.threshold = threshold
self.logger = fancylogger.getLogger(self.__class__.__name__)

def read_pid_timestamp(self):
'''Obtain the PID and timestamp from the lockfile.
Expand Down Expand Up @@ -84,24 +82,24 @@ def acquire(self, timeout=None):
timeout = self.threshold
try:
_write_pid_timestamp_file(self.path)
self.logger.info('Obtained lock on timestamped pid lockfile %s', self.path)
except OSError as err:
logging.info('Obtained lock on timestamped pid lockfile %s', self.path)
except (OSError, FileNotFoundErrorExc, FileExistsErrorExc) as err:
doraise = True
if err.errno == errno.EEXIST:
## Check if the timestamp is older than the threshold
(pid, timestamp) = _read_pid_timestamp_file(self.path)
if time.time() - timestamp > timeout:
_find_and_kill(pid)
os.unlink(self.path)
self.logger.warning('Obsolete lockfile detected at %s: pid = %d, timestamp = %s',
self.path, pid, time.ctime(timestamp))
logging.warning('Obsolete lockfile detected at %s: pid = %d, timestamp = %s',
self.path, pid, time.ctime(timestamp))
try:
_write_pid_timestamp_file(self.path)
doraise = False
except Exception:
pass
if doraise:
self.logger.error('Unable to obtain lock on %s: %s', self.path, err)
logging.error('Unable to obtain lock on %s: %s', self.path, err)
raise LockFailed

def release(self):
Expand All @@ -110,10 +108,10 @@ def release(self):
Remove the lockfile to indicate the lock was released.
'''
if not self.is_locked():
self.logger.error('Trying to release a lock that does not exist at %s.', self.path)
logging.error('Trying to release a lock that does not exist at %s.', self.path)
raise NotLocked
if not self.i_am_locking():
self.logger.error('Trying to release a lock the current process is not holding at %s', self.path)
logging.error('Trying to release a lock the current process is not holding at %s', self.path)
raise NotMyLock
os.remove(self.path)

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from vsc.install.shared_setup import ag, sdw

install_requires = [
'vsc-base >= 3.0.2',
'vsc-base >= 3.2.4',
'lockfile >= 0.9.1',
'netifaces',
'jsonpickle',
Expand All @@ -54,7 +54,7 @@


PACKAGE = {
'version': '2.1.9',
'version': '2.1.10',
'author': [ag, sdw],
'maintainer': [ag, sdw],
'excluded_pkgs_rpm': ['vsc', 'vsc.utils'], # vsc is default, vsc.utils is provided by vsc-base
Expand Down

0 comments on commit e772ce7

Please sign in to comment.