#!/bin/sh
set -eu

/app/api &
api_pid="$!"

/app/worker &
worker_pid="$!"

term() {
  kill "$api_pid" "$worker_pid" 2>/dev/null || true
  wait "$api_pid" "$worker_pid" 2>/dev/null || true
}

trap term INT TERM

while :; do
  if ! kill -0 "$api_pid" 2>/dev/null; then
    wait "$api_pid" || true
    term
    exit 1
  fi

  if ! kill -0 "$worker_pid" 2>/dev/null; then
    wait "$worker_pid" || true
    term
    exit 1
  fi

  sleep 2
done
