#!/usr/bin/env bash
set -euo pipefail

# Wait for DB
echo "Waiting for DB ${MYSQL_HOST}:${MYSQL_PORT}…"
for i in {1..60}; do
  if mysqladmin ping -h"${MYSQL_HOST}" -P"${MYSQL_PORT}" -u"${MYSQL_USER}" -p"${MYSQL_PASSWORD}" --silent; then
    break
  fi
  sleep 1
done

if [[ -n "${SCRAPE_EVERY_MINUTES:-}" ]]; then
  echo "Looping every ${SCRAPE_EVERY_MINUTES} minute(s)"
  while true; do
    /app/run_all.sh
    sleep "$(( 60 * ${SCRAPE_EVERY_MINUTES} ))"
  done
else
  /app/run_all.sh
fi
