From 6c031ca0faf1292a8c00c3907cf0dc7e9f56650a Mon Sep 17 00:00:00 2001 From: HF Date: Sun, 14 Jan 2024 00:30:48 +0100 Subject: [PATCH] add ability to clean state of a specific room to purge script --- purge/matrixpurge.sh | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/purge/matrixpurge.sh b/purge/matrixpurge.sh index 796a4e2..bf9c033 100755 --- a/purge/matrixpurge.sh +++ b/purge/matrixpurge.sh @@ -59,16 +59,18 @@ purge_room () { return 1 fi printf "Waiting for deletion ${D_ID} for ${ROOM}" - D_STATUS="purging" + D_STATUS="active" D_STATUSRET="" sleep 4 - while [ "${D_STATUS}" = "purging" ]; do + while [ "${D_STATUS}" = "purging" ] || [ "${D_STATUS}" = "active" ]; do D_STATUSRET=`curl --silent --max-time 900 --insecure -XGET -H "Authorization: Bearer ${TOKEN}" "${MATRIXURL}/_synapse/admin/v2/rooms/delete_status/${D_ID}"` D_STATUS=`echo ${D_STATUSRET} | jq -r '.status'` printf "." sleep 5 done printf "\nPurge finished ${D_STATUSRET}\n" + echo "You might want to additionally run:" + echo " ./matrixpurge.sh clean_states '${ROOM}'" } # purge events in a room - we do that here instead of per auto_retention, @@ -130,6 +132,21 @@ check_rooms () { done } +# clear unreferenced state groups of given room +clear_state_groups_of_room () { + ROOM="${1}" + [ -f "/tmp/sgstmp.txt" ] && rm "/tmp/sgstmp.txt" + printf "=> ${L}${ROOM}${NC}\n" + nice -n 10 ${SYNAPSE_UNREFERENCED_STATES} -p postgresql://${SQLUSER}:${SQLPASSWD}@localhost/${SQLDB} -r "${ROOM}" -o "/tmp/sgstmp.txt" + [ -f "/tmp/sgstmp.txt" ] && { + # can'tdo that while synapse is running + systemctl stop matrix-synapse + psql -t postgresql://${SQLUSER}:${SQLPASSWD}@localhost/${SQLDB} -c "CREATE TEMPORARY TABLE unreffed(id BIGINT PRIMARY KEY); COPY unreffed FROM stdin WITH (FORMAT 'csv'); DELETE FROM state_groups_state WHERE state_group IN (SELECT id FROM unreffed); DELETE FROM state_group_edges WHERE state_group IN (SELECT id FROM unreffed); DELETE FROM state_groups WHERE id IN (SELECT id FROM unreffed);" < /tmp/sgstmp.txt + systemctl start matrix-synapse + rm "/tmp/sgstmp.txt" + } +} + # check unreferenced state groups for 20 largest rooms check_state_groups () { files="${1}" @@ -241,6 +258,7 @@ print_stats () { [ -f "${PIDFILE}" ] && ps -p `cat "${PIDFILE}"` > /dev/null && { echo "matrixpurge.sh already running, exiting." + echo "If you think this is a mistake, delete this file: ${PIDFILE}" exit 1 } echo $$ > "${PIDFILE}" @@ -248,7 +266,11 @@ echo $$ > "${PIDFILE}" get_admin_token [ "${1}" = "clean_states" ] && { - clear_state_groups + if [ "${2}" ]; then + clear_state_groups_of_room "${2}" + else + clear_state_groups + fi exit 0 } @@ -257,6 +279,11 @@ get_admin_token exit 0 } +[ "${1}" ] && [ "${1}" != "reset" ] && { + echo "Unrecognized argument: ${1}" 1>&2 + exit 1 +} + check_rooms disable_ratelimit check_api_rooms