graceful-shutdown.yaml

Overview

This Ansible playbook file orchestrates the graceful shutdown of a Docker container named according to the pattern node{{ NODE_ID }} within a specified directory ({{ BK_DIR }}). The playbook executes a sequence of shell commands to terminate the node process running inside the container, monitor its shutdown status by analyzing logs, and forcibly stop the container if the process does not terminate within a given timeout.

This file is primarily used to ensure that the node container is stopped cleanly, allowing any internal shutdown routines to complete before the container is halted, minimizing risk of data corruption or inconsistent states.


Playbook Tasks

1. Send graceful shutdown request

- name: Send graceful shutdown request
  ansible.builtin.shell:
    chdir: "{{ BK_DIR }}"
    cmd: docker compose exec "node{{ NODE_ID }}" pkill node
  ignore_errors: true

2. Wait for container to stop or shutdown to finish

- name: Wait for container to stop or shutdown to finish
  ansible.builtin.shell:
    chdir: "{{ BK_DIR }}"
    cmd: |
      (docker compose ps "node{{ NODE_ID }}" -q | wc -l | grep -q "0") || (tail -n "{{ NODE_STOP_TEST_OUTER_TAIL }}" "{{ BK_LOGS_DIR }}/node.log" | grep -v -e TRACE -e pub_sub | tail -n "{{ NODE_STOP_TEST_INNER_TAIL }}" | grep -q "monit: Shutdown finished")
  register: container_check
  until: container_check is success
  retries: "{{ GS_WAIT | default(WAIT_FOR_NODE_STOP_SECS) }}"
  delay: 1
  when: GS_WAIT is not defined or GS_WAIT

3. Stop node container if the process still lingering

- name: Stop node container if the process still lingering
  ansible.builtin.shell:
    chdir: "{{ BK_DIR }}"
    cmd: docker compose stop "node{{ NODE_ID }}" -t 5

Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram

flowchart TD
A[Send graceful shutdown request] --> B[Wait for container to stop or shutdown to finish]
B -->|Shutdown detected| C[End]
B -->|Timeout expired| D[Stop node container forcibly]
D --> C