-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cron service and cleanup script (#167)
* Add cron service and cleanup script * Fix rockfile * Fix restart_synapse() * Remove unused fn * Fix restart_synapse() * Fix linting * Fix rockfile * Throttle cleanup operations * Add missing headers * Add some licensing exceptions * Try to fix headers ignore once again * Make scripts added to the rock executable * Rerun CI * Only modify rockcraft.yml where necessary * Fix rockcraft scripts permissions * Fix typo --------- Co-authored-by: Amanda H. L. de Andrade Katz <amanda.katz@canonical.com> Co-authored-by: arturo-seijas <102022572+arturo-seijas@users.noreply.github.com>
- Loading branch information
1 parent
5ea89ab
commit dbe44c4
Showing
7 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
synapse_rock/cron/cron.weekly/remote_content_empty_directory_cleanup.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/usr/bin/python3 | ||
# Copyright 2024 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
|
||
""" | ||
Synapse does not purge empty directories from its media content storage locations. | ||
Those can accumulate and eat up inodes and space. | ||
Related: https://github.com/matrix-org/synapse/issues/16229 | ||
Related: https://github.com/matrix-org/synapse/issues/7690 | ||
""" | ||
|
||
import os | ||
import json | ||
import time | ||
|
||
# We assume that pyyaml is present thanks to synapse | ||
import yaml | ||
|
||
""" | ||
To make sure that we don't steal IOPS from the running synapse instance, | ||
we need to throttle the walk/rmdir iterations. | ||
Given the MAX_IOPS, the formula is: | ||
ITERATIONS_BEFORE_SLEEP = (TARGET_IOPS * SLEEP_TIME * MAX_IOPS)/(MAX_IOPS - TARGET_IOPS) | ||
The values choosen here are for a TARGET_IOPS of 100 on a disk of 1600 MAX_IOPS. | ||
Which is very conservative if using a SSD. Check the table at https://en.wikipedia.org/wiki/IOPS#Solid-state_devices | ||
""" | ||
ITERATIONS_BEFORE_SLEEP = 100 | ||
SLEEP_TIME = 1 | ||
|
||
|
||
# This function is meant to fail if the media_store_path can't be found | ||
def load_media_store_path(file_path: str) -> str: | ||
with open(file_path, "r", encoding="utf-8") as file: | ||
data = yaml.safe_load(file) | ||
return data["media_store_path"] | ||
|
||
|
||
def delete_empty_dirs(path: str) -> None: | ||
i = 0 | ||
for root, dirs, _ in os.walk(path, topdown=False): | ||
i += 1 | ||
if i > ITERATIONS_BEFORE_SLEEP: | ||
i = 0 | ||
time.sleep(SLEEP_TIME) | ||
for dir in dirs: | ||
try: | ||
os.rmdir(os.path.join(root, dir)) | ||
except OSError: | ||
continue | ||
|
||
|
||
if __name__ == "__main__": | ||
# load the environment from the file produced by /usr/local/bin/run_cron.py | ||
if os.path.isfile("/var/local/cron/environment.json"): | ||
with open("/var/local/cron/environment.json", "r") as env_fd: | ||
env = json.load(env_fd) | ||
if os.path.isfile(env["SYNAPSE_CONFIG_PATH"]): | ||
path = load_media_store_path(env["SYNAPSE_CONFIG_PATH"]) | ||
if path: | ||
delete_empty_dirs(path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright 2024 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
|
||
""" | ||
The goal of this script is to serialize the environment passed by pebble to a file | ||
and then to start the cron service. We're doing that because the environment of the cron service | ||
is not passed to the commands started by it. | ||
An alternative would have been to write to /etc/profile and start the called cron scripts with a login shell. | ||
But we wanted to use python scripts and not pollute the environment of the users | ||
(Creating a specific user for the cron service would be an added complexity). | ||
""" | ||
|
||
import json | ||
import os | ||
|
||
if __name__ == "__main__": | ||
file_path = "/var/local/cron/environment.json" | ||
os.makedirs(os.path.dirname(file_path), exist_ok=True) | ||
|
||
with open(file_path, "w") as file: | ||
json.dump(dict(os.environ), file) | ||
|
||
os.execv("/usr/sbin/cron", ["/usr/sbin/cron", "-f", "-P"]) |