Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An example docker compose implementation of chifra scrape with clean shutdown (with bonus unsupported chain) #399

Open
tjayrush opened this issue May 30, 2024 · 2 comments

Comments

@tjayrush
Copy link
Member

From a user in discord (dreadedhamish):

services:
  core:
    image: trueblocks/core:v2.5.8-release
    environment:
      - TB_SETTINGS_DEFAULTCHAIN=pulsechain
      - TB_SETTINGS_CACHEPATH=/cache
      - TB_SETTINGS_INDEXPATH=/unchained
    entrypoint: ["/bin/bash", "/root/.local/share/trueblocks/startup.sh"]  
    stop_grace_period: 120s
    # cpus: 4
    # mem_limit: 4gb
    restart: on-failure:3
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/when?blocks=0"]
      interval: 1m30s
      timeout: 10s
      retries: 3
      start_period: 40s
      start_interval: 5s
    ports:
      - "8083:8080"
    volumes:
      - type: bind
        source: /data/h-volumes/trueblocks/cache
        target: /cache
      - type: bind
        source: /data/h-volumes/trueblocks/unchained
        target: /unchained
      - type: bind
        source: /data/h-volumes/trueblocks/pulsechain
        target: /root/.local/share/trueblocks/config/pulsechain
      - type: bind
        source: /data/h-volumes/trueblocks/trueBlocks.toml
        target: /root/.local/share/trueblocks/trueBlocks.toml
      - type: bind
        source: /data/h-volumes/trueblocks/startup.sh
        target: /root/.local/share/trueblocks/startup.sh
        read_only: true
@dreadedhamish
Copy link

dreadedhamish commented Jun 1, 2024

Here is the accompanying startup.sh
There is currently an issue with the Trueblocks docker image, so that it doesn't shutdown cleanly (should be resolved soon). Additionally we are running multiple processes (chifra daemon and chifra scrape), and there are complexities with passing termination signals to the main process in a docker container. The following feels hacky but it was the only way to get all processes to shutdown before docker kills the container. It captures the shutdown signal and stalls while getting all process names that start with "chifra", and then terminating them in descending order of process id.

The issue with Trueblocks docker image results in temporary scrape files not being deleted, and on next startup chifra will interpret this as meaning an existing scraper is already underway. The delete on line 27 deals with this. In my own and Dawid's testing this hasn't caused any issues.

#!/bin/bash
set -Eeuo pipefail

# Function to stop processes
stop_processes() {
  echo "Received shutdown signal"
  
  # Get all running chifra processes
  chifra_pids=$(pgrep -f chifra)

  # Reverse the order of the PIDs
  chifra_pids=$(echo $chifra_pids | tr " " "\n" | tac | tr "\n" " ")

  # Send SIGTERM to each PID
  for pid in $chifra_pids; do
    echo "Sending SIGTERM to process $pid"
    kill -SIGTERM $pid
    # Wait for the process to terminate
    wait $pid
  done
}

# Do some stuff here, whatever you want.
echo "Starting Trueblocks"

# Delete the /cache/pulsechain/tmp/scrape.pid file
rm -f /cache/pulsechain/tmp/scrape.pid

# Start chifra daemon in the background
chifra daemon --url 0.0.0.0:8080 & sleep 10 && chifra scrape &

# Keep container alive until all artisan procs have stopped.
STAYALIVE="true"
IMPORTANT_PROC="chifra"

# Catch signals from docker-compose.
trap 'STAYALIVE="false"; stop_processes' TERM INT

# The same while true; sleep; loop as above, except this one uses the count of "artisan" processes and a trapped signal as its boolean val.
while [ "$STAYALIVE" = "true" ] || (pgrep -f $IMPORTANT_PROC | sed ':a;N;$!ba;s/\n/ /g'); do
    # Wait for x seconds to allow processes to finish.
    sleep 10

    # Echo something to give some logs to docker logs.
    echo "Latest version running: $STAYALIVE, Important processes running: $(pgrep -f $IMPORTANT_PROC | sed ':a;N;$!ba;s/\n/ /g' )"
done; (edited)

@dreadedhamish
Copy link

I edited my startup.sh script above to use: chifra daemon --url 0.0.0.0:8080 because no API requests were getting through.

from Dawid in Discord: Try changing API server listening URL to 0.0.0.0:8080: chifra daemon --url 0.0.0.0:8080. It's realted to Docker: if it have a process listening on "localhost" or 127.0.0.1, then it's blocking access from the outside

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants