From 471ad21c781d8c6a60bcaec8cfd44bafd3bc7b24 Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Wed, 16 Jun 2021 19:28:41 +0200 Subject: [PATCH] services: matrix: automatic room compress service --- services/matrix.nix | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/services/matrix.nix b/services/matrix.nix index 05dc395..431f789 100644 --- a/services/matrix.nix +++ b/services/matrix.nix @@ -287,5 +287,44 @@ in { # this is just caching for other servers media, doesn't need backup exclude = [ "${dataDir}/media/remote_*" ]; }; + + systemd.services.matrix-synapse-compress-state = { + path = [ + config.services.postgresql.package + pkgs.matrix-synapse-tools.rust-synapse-compress-state + ]; + + serviceConfig = { + Type = "oneshot"; + User = "matrix-synapse"; + Group = "matrix-synapse"; + }; + + script = '' + set -eux + + # select the 20 biggest rooms + psql \ + -U matrix-synapse \ + -d matrix-synapse \ + -c 'SELECT room_id, count(*) AS count + FROM state_groups_state + GROUP BY room_id + ORDER BY count DESC;' \ + -t | sed -r 's/\s([^ ]+)\s*\|.*/\1/' | head -n 20 > /tmp/synapse_compress_rooms.txt + + while read -r room_id; do + synapse-compress-state \ + -p "postgresql:///matrix-synapse?user=matrix-synapse&host=/var/run/postgresql" \ + -r "''${room_id}" \ + -o /tmp/compress.sql \ + -t + psql -U matrix-synapse -d matrix-synapse < /tmp/compress.sql + rm /tmp/compress.sql + done < /tmp/synapse_compress_rooms.txt + + rm /tmp/synapse_compress_rooms.txt + ''; + }; }; }