Skip to content

Commit

Permalink
Handle 2 reported errors better
Browse files Browse the repository at this point in the history
Fixes #6
Fixes #5
  • Loading branch information
yuvipanda committed Nov 14, 2023
1 parent 7733685 commit 8fc1014
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions prometheus_dirsize_exporter/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,36 @@ def get_dir_info(self, path: str) -> Optional[DirInfo]:
)

def get_subdirs_info(self, dir_path):
children = [
os.path.abspath(os.path.join(dir_path, c))
for c in self.do_iops_action(os.listdir, dir_path)
]

dirs = [c for c in children if self.do_iops_action(os.path.isdir, c)]

for c in dirs:
yield self.get_dir_info(c)

try:
children = [
os.path.abspath(os.path.join(dir_path, c))
for c in self.do_iops_action(os.listdir, dir_path)
]

dirs = [c for c in children if self.do_iops_action(os.path.isdir, c)]

for c in dirs:
yield self.get_dir_info(c)
except OSError as e:
if e.errno == 116:
# See https://github.com/yuvipanda/prometheus-dirsize-exporter/issues/6
# Stale file handle, often because the file we were looking at
# changed in the NFS server via another client in such a way that
# a new inode was created. This is a race, so let's just ignore and
# not report any data for this file. If this file was recreated,
# our next run should catch it
return None
# Any other errors should just be propagated
raise
except PermissionError as e:
if e.errno == 13:
# See https://github.com/yuvipanda/prometheus-dirsize-exporter/issues/5
# A file we are trying to open is owned in such a way that we don't have
# access to it. Ideally this should not really happen, but when it does,
# we just ignore it and continue.
return None
# Any other permission error should be propagated
raise

def main():
argparser = argparse.ArgumentParser()
Expand Down

0 comments on commit 8fc1014

Please sign in to comment.