--- - name: "Scale down excess download workers if necessary" when: (download_workers_per_profile | default(0) | int == 0) and (download_workers_total | default(0) | int > 0) block: - name: "Find running download simulator tmux sessions for this group" ansible.builtin.shell: cmd: "tmux list-sessions -F '#{session_name}' 2>/dev/null | grep -E '^stress-download-worker-[0-9]+$' || true" register: running_sessions changed_when: false ignore_errors: yes - name: "Identify excess download simulator sessions to stop" ansible.builtin.set_fact: excess_sessions_to_stop: "{{ excess_sessions_to_stop | default([]) + [item] }}" vars: worker_num_str: "{{ item | regex_replace('^stress-download-worker-', '') }}" when: worker_num_str is number and (worker_num_str | int > (download_workers_total | int)) loop: "{{ running_sessions.stdout_lines }}" loop_control: label: "Identifying excess session: {{ item }}" - name: "Get PIDs for excess download workers" ansible.builtin.shell: cmd: | PANE_PID=$(tmux list-panes -s "{{ item }}" -F '#{pane_pid}' | head -n 1) if [ -n "$PANE_PID" ]; then pgrep -P "$PANE_PID" || true fi register: excess_pids_raw loop: "{{ excess_sessions_to_stop | default([]) }}" changed_when: false ignore_errors: true - name: "Set fact for PIDs to kill" ansible.builtin.set_fact: pids_to_kill_gracefully: "{{ excess_pids_raw.results | map(attribute='stdout') | reject('==', '') | list }}" - name: "Gracefully terminate excess download workers" ansible.builtin.shell: cmd: "kill {{ item }}" loop: "{{ pids_to_kill_gracefully | default([]) }}" when: pids_to_kill_gracefully is defined and pids_to_kill_gracefully | length > 0 ignore_errors: true changed_when: false - name: "Wait for graceful shutdown of excess workers" ansible.builtin.pause: seconds: "{{ graceful_shutdown_timeout_seconds }}" when: pids_to_kill_gracefully is defined and pids_to_kill_gracefully | length > 0 - name: "Force kill any lingering excess workers" ansible.builtin.shell: cmd: "kill -9 {{ item }}" loop: "{{ pids_to_kill_gracefully | default([]) }}" when: pids_to_kill_gracefully is defined and pids_to_kill_gracefully | length > 0 ignore_errors: true changed_when: false - name: "Kill tmux sessions for excess workers" ansible.builtin.shell: cmd: "tmux kill-session -t {{ item }}" loop: "{{ excess_sessions_to_stop | default([]) }}" when: excess_sessions_to_stop is defined and excess_sessions_to_stop | length > 0 ignore_errors: true changed_when: false - name: "Start single download simulator for all profiles: {{ combined_prefixes | default('none') }}" ansible.builtin.command: >- ansible-playbook {{ playbook_dir }}/playbook-stress-download-simulation.yml -i {{ inventory_file }} --limit {{ inventory_hostname }} -e "start_download=true" -e "profile_prefix=worker" -e "display_prefix={{ combined_prefixes }}" -e "worker_num={{ worker_num }}" {% if dummy_batch | default(false) %}-e "dummy_batch=true"{% endif %} {% if download_min_seconds is defined %}-e "download_min_seconds={{ download_min_seconds }}"{% endif %} {% if download_max_seconds is defined %}-e "download_max_seconds={{ download_max_seconds }}"{% endif %} {% if extra_set_args is defined %}-e 'extra_set_args={{ extra_set_args | to_json }}'{% endif %} delegate_to: localhost changed_when: true when: download_workers_total | default(0) | int > 0 loop: "{{ range(1, (download_workers_total | default(1) | int) + 1) | list }}" loop_control: loop_var: worker_num label: "worker {{ worker_num }}" - name: "Start parallel download simulators for each profile" ansible.builtin.command: >- ansible-playbook {{ playbook_dir }}/playbook-stress-download-simulation.yml -i {{ inventory_file }} --limit {{ inventory_hostname }} -e "start_download=true" -e "profile_prefix={{ item }}" {% if dummy_batch | default(false) %}-e "dummy_batch=true"{% endif %} {% if download_min_seconds is defined %}-e "download_min_seconds={{ download_min_seconds }}"{% endif %} {% if download_max_seconds is defined %}-e "download_max_seconds={{ download_max_seconds }}"{% endif %} {% if extra_set_args is defined %}-e 'extra_set_args={{ extra_set_args | to_json }}'{% endif %} delegate_to: localhost changed_when: true loop: "{{ profile_prefixes }}" loop_control: loop_var: item label: "profile: {{ item }}" when: (download_workers_total | default(0) | int == 0) and (download_workers_per_profile | default(0) | int > 0)