---

# Pre-checks

- name: Fail if Postgres not enabled
  ansible.builtin.fail:
    msg: "Postgres via the matrix-postgres role is not enabled (`matrix_postgres_enabled`). Cannot run vacuum."
  when: "not matrix_postgres_enabled | bool"


# Defaults

- name: Set postgres_start_wait_time, if not provided
  ansible.builtin.set_fact:
    postgres_start_wait_time: 15
  when: "postgres_start_wait_time | default('') == ''"

- name: Set postgres_vacuum_wait_time, if not provided
  ansible.builtin.set_fact:
    postgres_vacuum_wait_time: "{{ 7 * 86400 }}"
  when: "postgres_vacuum_wait_time | default('') == ''"


# Actual vacuuming work

- name: Ensure matrix-postgres is started
  ansible.builtin.service:
    name: matrix-postgres
    state: started
    daemon_reload: true

- name: Wait a bit, so that Postgres can start
  ansible.builtin.wait_for:
    timeout: "{{ postgres_start_wait_time }}"
  delegate_to: 127.0.0.1
  become: false

- ansible.builtin.import_tasks: tasks/detect_existing_postgres_version.yml

- name: Abort, if no existing Postgres version detected
  ansible.builtin.fail:
    msg: "Could not find existing Postgres installation"
  when: "not matrix_postgres_detected_existing | bool"

- name: Generate Postgres database vacuum command
  ansible.builtin.set_fact:
    matrix_postgres_vacuum_command: >-
      {{ matrix_host_command_docker }} run --rm --name matrix-postgres-synapse-vacuum
      --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
  ansible.builtin.debug:
    msg: >-
      Running vacuum with the following Postgres ansible.builtin.command: `{{ matrix_postgres_vacuum_command }}`.
      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
  ansible.builtin.service_facts:

- ansible.builtin.set_fact:
    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' }}"

- name: Ensure matrix-synapse is stopped
  ansible.builtin.service:
    name: matrix-synapse
    state: stopped
    daemon_reload: true

- name: Run Postgres vacuum command
  ansible.builtin.command: "{{ matrix_postgres_vacuum_command }}"
  async: "{{ postgres_vacuum_wait_time }}"
  poll: 10
  register: matrix_postgres_synapse_vacuum_result
  changed_when: matrix_postgres_synapse_vacuum_result.rc == 0

# Intentionally show the results
- ansible.builtin.debug: var="matrix_postgres_synapse_vacuum_result"

- name: Ensure matrix-synapse is started, if it previously was
  ansible.builtin.service:
    name: matrix-synapse
    state: started
    daemon_reload: true
  when: "matrix_postgres_synapse_was_running | bool"