Skip to content

Commit

Permalink
Add systemd-run cleanup option for SymlinkAndCleanup removals
Browse files Browse the repository at this point in the history
  • Loading branch information
elikoga committed Apr 16, 2024
1 parent 52d3ce1 commit 3355fd5
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions src/batou_ext/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os.path
import shutil
import urllib.parse
import subprocess

import batou
import batou.component
Expand All @@ -25,6 +26,8 @@ class SymlinkAndCleanup(batou.component.Component):

prefix = None

use_systemd_run_async_cleanup = False

def configure(self):
self._current_link = (
f"{self.prefix}-current" if self.prefix else "current"
Expand Down Expand Up @@ -97,13 +100,36 @@ def update(self):
if current:
batou.output.annotate("last -> {}".format(current))
os.symlink(current, self._last_link)
for el in self._list_removals():
batou.output.annotate("Removing: {}".format(el))
candidates = self._list_removals()
if self.use_systemd_run_async_cleanup:
# spawns a IOPS limited systemd-run cleanup job
try:
if os.path.isdir(el):
shutil.rmtree(el)
else:
os.remove(el)
except OSError as e:
batou.output.error(f'Failed to remove "{el}": {e.strerror}')
pass
batou.output.annotate("Removing using systemd-run: {}".format(candidates))
rm_cmd = [
"rm",
"-rf",
*candidates, # consider: ARG_MAX is limited
]
subprocess.run(
[
"systemd-run",
"--unit",
f"batou-cleanup-{self.prefix}",
"--property=IOReadIOPSMax=250",
"--property=IOWriteIOPSMax=250",
*rm_cmd,
],
check=True,
)
except subprocess.CalledProcessError as e:
batou.output.error(f"Failed to remove: {e}")
else:
for el in candidates:
batou.output.annotate("Removing: {}".format(el))
try:
if os.path.isdir(el):
shutil.rmtree(el)
else:
os.remove(el)
except OSError as e:
batou.output.error(f'Failed to remove "{el}": {e.strerror}')

0 comments on commit 3355fd5

Please sign in to comment.