#!/usr/bin/env bash
set -e

APP_ROLE="${APP_ROLE:-web}"

# Run pending migrations before starting any role.
python manage.py migrate --noinput

if [ "$APP_ROLE" = "scheduler" ]; then
  SCHEDULER_INTERVAL_SECONDS="${SCHEDULER_INTERVAL_SECONDS:-60}"
  while true; do
    python manage.py run_newsletter_scheduler --limit 20
    sleep "$SCHEDULER_INTERVAL_SECONDS"
  done
else
  python manage.py collectstatic --noinput
  exec "$@"
fi
