Skip to content

Commit

Permalink
Merge pull request #165 from flyingcircusio/fix-async-delete
Browse files Browse the repository at this point in the history
Fix `SymlinkAndCleanup` async delete and allow custom extra arguments…
  • Loading branch information
zagy authored Apr 29, 2024
2 parents d2e2a7b + c0b3b42 commit 684f93d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
7 changes: 7 additions & 0 deletions CHANGES.d/20240426_072058_cz_fix_async_delete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!--
A new scriv changelog fragment.
Uncomment the section that is right (remove the HTML comment wrapper).
-->

- Fix `SymlinkAndCleanup` async delete and allow custom extra arguments to `systemd run`.
49 changes: 28 additions & 21 deletions src/batou_ext/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class SymlinkAndCleanup(batou.component.Component):

use_systemd_run_async_cleanup = False

# Use extra args to e.g. limit IOPS:
# ["--property=IOReadIOPSMax=/dev/vda 100",
# "--property=IOWriteIOPSMax=/dev/vda 100"]
systemd_extra_args: list = None

def configure(self):
self._current_link = (
f"{self.prefix}-current" if self.prefix else "current"
Expand Down Expand Up @@ -100,29 +105,31 @@ def update(self):
if current:
batou.output.annotate("last -> {}".format(current))
os.symlink(current, self._last_link)

candidates = self._list_removals()
if self.use_systemd_run_async_cleanup:
# spawns a IOPS limited systemd-run cleanup job
if not candidates:
batou.output.annotate("Nothing to remove.")
elif self.use_systemd_run_async_cleanup:
# Spawns an systemd-run cleanup job with custom args.
candidates = [os.path.join(self.dir, c) for c in candidates]
rm_cmd = [
"rm",
"-rfv",
*candidates, # consider: ARG_MAX is limited
]
extra_args = self.systemd_extra_args or []
cmd = [
"systemd-run",
"--unit",
f"batou-cleanup-{self.prefix}",
"--user",
*extra_args,
*rm_cmd,
]
batou.output.annotate(f"Removing: {candidates}")
batou.output.annotate(f" {cmd}")
try:
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=100",
"--property=IOWriteIOPSMax=100",
*rm_cmd,
],
check=True,
)
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError as e:
batou.output.error(f"Failed to remove: {e}")
else:
Expand Down

0 comments on commit 684f93d

Please sign in to comment.