2019-07-08 06:38:24 +00:00
---
# Pre-checks
- name : Fail if Postgres not enabled
2022-07-18 07:39:08 +00:00
ansible.builtin.fail :
2019-07-08 06:38:24 +00:00
msg : "Postgres via the matrix-postgres role is not enabled (`matrix_postgres_enabled`). Cannot run vacuum."
2022-07-18 08:22:05 +00:00
when : "not matrix_postgres_enabled | bool"
2019-07-08 06:38:24 +00:00
# Defaults
- name : Set postgres_start_wait_time, if not provided
2022-07-18 07:39:08 +00:00
ansible.builtin.set_fact :
2019-07-08 06:38:24 +00:00
postgres_start_wait_time : 15
2022-07-18 08:22:05 +00:00
when : "postgres_start_wait_time | default('') == ''"
2019-07-08 06:38:24 +00:00
- name : Set postgres_vacuum_wait_time, if not provided
2022-07-18 07:39:08 +00:00
ansible.builtin.set_fact :
2019-07-08 06:38:24 +00:00
postgres_vacuum_wait_time : "{{ 7 * 86400 }}"
2022-07-18 08:22:05 +00:00
when : "postgres_vacuum_wait_time | default('') == ''"
2019-07-08 06:38:24 +00:00
# Actual vacuuming work
- name : Ensure matrix-postgres is started
2022-07-18 07:39:08 +00:00
ansible.builtin.service :
2019-07-08 06:38:24 +00:00
name : matrix-postgres
state : started
2022-02-05 20:32:54 +00:00
daemon_reload : true
2019-07-08 06:38:24 +00:00
- name : Wait a bit, so that Postgres can start
2022-07-18 08:22:05 +00:00
ansible.builtin.wait_for :
2019-07-08 06:38:24 +00:00
timeout : "{{ postgres_start_wait_time }}"
delegate_to : 127.0 .0 .1
become : false
2022-07-18 13:15:04 +00:00
- ansible.builtin.import_tasks : tasks/detect_existing_postgres_version.yml
2019-07-08 06:38:24 +00:00
- name : Abort, if no existing Postgres version detected
2022-07-18 07:39:08 +00:00
ansible.builtin.fail :
2019-07-08 06:38:24 +00:00
msg : "Could not find existing Postgres installation"
2022-07-18 08:22:05 +00:00
when : "not matrix_postgres_detected_existing | bool"
2019-07-08 06:38:24 +00:00
- name : Generate Postgres database vacuum command
2022-07-18 07:39:08 +00:00
ansible.builtin.set_fact :
2019-07-08 06:38:24 +00:00
matrix_postgres_vacuum_command : >-
2020-05-27 20:18:24 +00:00
{{ matrix_host_command_docker }} run --rm --name matrix-postgres-synapse-vacuum
2019-07-08 06:38:24 +00: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 07:39:08 +00:00
ansible.builtin.debug :
2019-07-08 06:38:24 +00:00
msg : >-
2022-07-18 07:39:08 +00:00
Running vacuum with the following Postgres ansible.builtin.command : `{{ matrix_postgres_vacuum_command }}`.
2019-07-08 06:38:24 +00: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 08:22:05 +00:00
ansible.builtin.service_facts :
2019-07-08 06:38:24 +00:00
2022-07-18 07:39:08 +00:00
- ansible.builtin.set_fact :
2022-07-18 12:08:10 +00: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 06:38:24 +00:00
- name : Ensure matrix-synapse is stopped
2022-07-18 07:39:08 +00:00
ansible.builtin.service :
2019-07-08 06:38:24 +00:00
name : matrix-synapse
state : stopped
2022-02-05 20:32:54 +00:00
daemon_reload : true
2019-07-08 06:38:24 +00:00
- name : Run Postgres vacuum command
2022-07-18 07:39:08 +00:00
ansible.builtin.command : "{{ matrix_postgres_vacuum_command }}"
2019-07-08 06:38:24 +00:00
async : "{{ postgres_vacuum_wait_time }}"
poll : 10
register : matrix_postgres_synapse_vacuum_result
2022-07-18 12:08:10 +00:00
changed_when : matrix_postgres_synapse_vacuum_result.rc == 0
2019-07-08 06:38:24 +00:00
# Intentionally show the results
2022-07-18 07:39:08 +00:00
- ansible.builtin.debug : var="matrix_postgres_synapse_vacuum_result"
2019-07-08 06:38:24 +00:00
- name : Ensure matrix-synapse is started, if it previously was
2022-07-18 07:39:08 +00:00
ansible.builtin.service :
2019-07-08 06:38:24 +00:00
name : matrix-synapse
state : started
2022-02-05 20:32:54 +00:00
daemon_reload : true
2022-07-18 08:22:05 +00:00
when : "matrix_postgres_synapse_was_running | bool"