Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SymlinkAndCleanup async delete and allow custom extra arguments… #165

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading