Add service priorities - try to stop/start them in an optimal order

This commit is contained in:
Slavi Pantaleev
2022-11-23 08:43:46 +02:00
parent 0ea7cb5d18
commit 360e643f84
8 changed files with 123 additions and 86 deletions

View File

@ -253,9 +253,26 @@ matrix_well_known_matrix_server_enabled: true
# See `matrix_homeserver_admin_contacts`, `matrix_homeserver_support_url`, etc.
matrix_well_known_matrix_support_enabled: false
# This will contain a list of enabled services that the playbook is managing.
# Each component is expected to append its service name to this list.
matrix_systemd_services_list: []
# matrix_systemd_services_list_auto contains a list of systemd services and their priorities.
# This list is managed by the playbook. You're not meant to override this variable.
# To add your own items to the list, use `matrix_systemd_services_list_additional`
matrix_systemd_services_list_auto: []
# matrix_systemd_services_list_additional contains your own list of systemd services and their priorities.
#
# Example:
# matrix_systemd_services_list_additional:
# - name: some-service.service
# priority: 1250
# - name: another-service.service
# priority: 3500
matrix_systemd_services_list_additional: []
# matrix_systemd_services_list contains a list of systemd services and their priorities.
matrix_systemd_services_list: "{{ matrix_systemd_services_list_auto + matrix_systemd_services_list_additional }}"
# matrix_systemd_services_autostart_enabled controls whether systemd services should auto-start when the system reboots
matrix_systemd_services_autostart_enabled: true
matrix_homeserver_container_extra_arguments_auto: []
matrix_homeserver_app_service_config_files_auto: []

View File

@ -1,26 +1,22 @@
---
- name: Determine whether we should make services autostart
ansible.builtin.set_fact:
matrix_services_autostart_enabled_bool: "{{ true if matrix_services_autostart_enabled | default('') == '' else matrix_services_autostart_enabled | bool }}"
- name: Ensure systemd is reloaded
ansible.builtin.service:
daemon_reload: true
- name: Ensure Matrix services are stopped
ansible.builtin.service:
name: "{{ item }}"
name: "{{ item.name }}"
state: stopped
with_items: "{{ matrix_systemd_services_list }}"
with_items: "{{ matrix_systemd_services_list | sort (attribute='priority,name', reverse=true) }}"
when: not ansible_check_mode
- name: Ensure Matrix services are started
ansible.builtin.service:
name: "{{ item }}"
enabled: "{{ matrix_services_autostart_enabled_bool }}"
name: "{{ item.name }}"
state: started
with_items: "{{ matrix_systemd_services_list }}"
enabled: "{{ matrix_systemd_services_autostart_enabled }}"
with_items: "{{ matrix_systemd_services_list | sort (attribute='priority,name') }}"
when: not ansible_check_mode
# If we check service state immediately, we may succeed,
@ -48,7 +44,7 @@
If you're on a slow or overloaded server, it may be that services take a longer time to start and that this error is a false-positive.
You can consider raising the value of the `matrix_common_after_systemd_service_start_wait_for_timeout_seconds` variable.
See `roles/custom/matrix-common-after/defaults/main.yml` for more details about that.
with_items: "{{ matrix_systemd_services_list }}"
with_items: "{{ matrix_systemd_services_list | map(attribute='name') }}"
when:
- "item.endswith('.service') and (ansible_facts.services[item] | default(none) is none or ansible_facts.services[item].state != 'running')"
@ -59,7 +55,7 @@
# Therefore iterating here manually
- name: Fetch systemd information
ansible.builtin.systemd:
name: "{{ item }}"
name: "{{ item.name }}"
register: systemdstatus
with_items: "{{ matrix_systemd_services_list }}"

View File

@ -2,6 +2,6 @@
- name: Ensure Matrix services stopped
ansible.builtin.service:
name: "{{ item }}"
name: "{{ item.name }}"
state: stopped
with_items: "{{ matrix_systemd_services_list }}"
with_items: "{{ matrix_systemd_services_list | sort (attribute='priority,name', reverse=true) }}"

View File

@ -1,5 +1,5 @@
---
- ansible.builtin.set_fact:
matrix_systemd_services_list: "{{ ['matrix-jitsi-jvb.service'] }}"
matrix_systemd_services_list: "{{ [{'name': 'matrix-jitsi-jvb.service', 'priority': 1000}] }}"
when: matrix_jitsi_enabled | bool

View File

@ -7,12 +7,16 @@ matrix_ssl_renewal_systemd_units_list:
- name: matrix-ssl-lets-encrypt-certificates-renew.service
applicable: "{{ matrix_ssl_retrieval_method == 'lets-encrypt' }}"
enableable: false
priority: 5000
- name: matrix-ssl-lets-encrypt-certificates-renew.timer
applicable: "{{ matrix_ssl_retrieval_method == 'lets-encrypt' }}"
enableable: true
priority: 5000
- name: matrix-ssl-nginx-proxy-reload.service
applicable: "{{ matrix_ssl_retrieval_method == 'lets-encrypt' and matrix_nginx_proxy_enabled | bool }}"
enableable: false
priority: 5000
- name: matrix-ssl-nginx-proxy-reload.timer
applicable: "{{ matrix_ssl_retrieval_method == 'lets-encrypt' and matrix_nginx_proxy_enabled | bool }}"
enableable: true
priority: 5000

View File

@ -27,7 +27,7 @@
state: stopped
enabled: false
with_items: "{{ matrix_synapse_workers_current_systemd_services.files }}"
when: "not ansible_check_mode and item.path | basename not in matrix_systemd_services_list"
when: "not ansible_check_mode and item.path | basename not in matrix_systemd_services_list | map(attribute='name')"
- name: Ensure unnecessary worker systemd services are cleaned
ansible.builtin.file:

View File

@ -53,7 +53,7 @@
when: "'replication_port' not in matrix_synapse_worker_details"
- ansible.builtin.set_fact:
matrix_systemd_services_list: "{{ matrix_systemd_services_list + [matrix_synapse_worker_details.name + '.service'] }}"
matrix_systemd_services_list_auto: "{{ matrix_systemd_services_list_auto + [{'name': (matrix_synapse_worker_details.name + '.service'), 'priority': 1100}] }}"
- ansible.builtin.set_fact:
matrix_synapse_webserving_workers_systemd_services_list: "{{ matrix_synapse_webserving_workers_systemd_services_list + [matrix_synapse_worker_details.name + '.service'] }}"