2019-07-08 08:38:24 +02:00
---
# Pre-checks
- name : Fail if Postgres not enabled
2022-07-18 09:39:08 +02:00
ansible.builtin.fail :
2019-07-08 08:38:24 +02:00
msg : "Postgres via the matrix-postgres role is not enabled (`matrix_postgres_enabled`). Cannot run vacuum."
2022-07-18 10:22:05 +02:00
when : "not matrix_postgres_enabled | bool"
2019-07-08 08:38:24 +02:00
# Defaults
- name : Set postgres_start_wait_time, if not provided
2022-07-18 09:39:08 +02:00
ansible.builtin.set_fact :
2019-07-08 08:38:24 +02:00
postgres_start_wait_time : 15
2022-07-18 10:22:05 +02:00
when : "postgres_start_wait_time | default('') == ''"
2019-07-08 08:38:24 +02:00
- name : Set postgres_vacuum_wait_time, if not provided
2022-07-18 09:39:08 +02:00
ansible.builtin.set_fact :
2019-07-08 08:38:24 +02:00
postgres_vacuum_wait_time : "{{ 7 * 86400 }}"
2022-07-18 10:22:05 +02:00
when : "postgres_vacuum_wait_time | default('') == ''"
2019-07-08 08:38:24 +02:00
# Actual vacuuming work
- name : Ensure matrix-postgres is started
2022-07-18 09:39:08 +02:00
ansible.builtin.service :
2019-07-08 08:38:24 +02:00
name : matrix-postgres
state : started
2022-02-05 21:32:54 +01:00
daemon_reload : true
2019-07-08 08:38:24 +02:00
- name : Wait a bit, so that Postgres can start
2022-07-18 10:22:05 +02:00
ansible.builtin.wait_for :
2019-07-08 08:38:24 +02:00
timeout : "{{ postgres_start_wait_time }}"
delegate_to : 127.0 .0 .1
become : false
2022-07-18 15:15:04 +02:00
- ansible.builtin.import_tasks : tasks/detect_existing_postgres_version.yml
2019-07-08 08:38:24 +02:00
- name : Abort, if no existing Postgres version detected
2022-07-18 09:39:08 +02:00
ansible.builtin.fail :
2019-07-08 08:38:24 +02:00
msg : "Could not find existing Postgres installation"
2022-07-18 10:22:05 +02:00
when : "not matrix_postgres_detected_existing | bool"
2019-07-08 08:38:24 +02:00
- name : Generate Postgres database vacuum command
2022-07-18 09:39:08 +02:00
ansible.builtin.set_fact :
2019-07-08 08:38:24 +02:00
matrix_postgres_vacuum_command : >-
2020-05-27 22:18:24 +02:00
{{ matrix_host_command_docker }} run --rm --name matrix-postgres-synapse-vacuum
2019-07-08 08:38:24 +02:00
--user={{ matrix_user_uid }}:{{ matrix_user_gid }}
--cap-drop=ALL
--network={{ matrix_docker_network }}
--env-file={{ matrix_postgres_base_path }}/env-postgres-psql
{{ matrix_postgres_docker_image_latest }}
psql -v ON_ERROR_STOP=1 -h matrix-postgres {{ matrix_synapse_database_database }} -c 'VACUUM FULL VERBOSE'
- name : Note about Postgres vacuum alternative
2022-07-18 09:39:08 +02:00
ansible.builtin.debug :
2019-07-08 08:38:24 +02:00
msg : >-
2022-07-18 09:39:08 +02:00
Running vacuum with the following Postgres ansible.builtin.command : `{{ matrix_postgres_vacuum_command }}`.
2019-07-08 08:38:24 +02:00
If this crashes, you can stop all processes (`systemctl stop matrix-*`),
start Postgres only (`systemctl start matrix-postgres`)
and manually run the above command directly on the server.
- name : Populate service facts
2022-07-18 10:22:05 +02:00
ansible.builtin.service_facts :
2019-07-08 08:38:24 +02:00
2022-07-18 09:39:08 +02:00
- ansible.builtin.set_fact :
2022-07-18 14:08:10 +02:00
matrix_postgres_synapse_was_running : "{{ ansible_facts.services['matrix-synapse.service'] | default(none) is not none and ansible_facts.services['matrix-synapse.service'].state == 'running' }}"
2019-07-08 08:38:24 +02:00
2022-10-28 16:39:53 +02:00
- name : Ensure services are stopped
2022-07-18 09:39:08 +02:00
ansible.builtin.service :
2022-10-28 16:39:53 +02:00
name : "{{ item }}"
2019-07-08 08:38:24 +02:00
state : stopped
2022-02-05 21:32:54 +01:00
daemon_reload : true
2022-10-28 16:39:53 +02:00
with_items : "{{ matrix_postgres_systemd_services_to_stop_for_maintenance_list }}"
2019-07-08 08:38:24 +02:00
- name : Run Postgres vacuum command
2022-07-18 09:39:08 +02:00
ansible.builtin.command : "{{ matrix_postgres_vacuum_command }}"
2019-07-08 08:38:24 +02:00
async : "{{ postgres_vacuum_wait_time }}"
poll : 10
register : matrix_postgres_synapse_vacuum_result
2022-10-06 18:48:11 +02:00
failed_when : not matrix_postgres_synapse_vacuum_result.finished
changed_when : matrix_postgres_synapse_vacuum_result.finished and matrix_postgres_synapse_vacuum_result.rc == 0
2019-07-08 08:38:24 +02:00
# Intentionally show the results
2022-10-25 05:43:57 +02:00
- ansible.builtin.debug :
var : "matrix_postgres_synapse_vacuum_result"
2019-07-08 08:38:24 +02:00
2022-10-28 16:39:53 +02:00
- name : Ensure services are started
2022-07-18 09:39:08 +02:00
ansible.builtin.service :
2022-10-28 16:39:53 +02:00
name : "{{ item }}"
2019-07-08 08:38:24 +02:00
state : started
2022-02-05 21:32:54 +01:00
daemon_reload : true
2022-10-28 16:39:53 +02:00
with_items : "{{ matrix_postgres_systemd_services_to_stop_for_maintenance_list }}"