#!/usr/bin/env bash
#
# This script is designed to be run inside the container
#

# fail hard and fast even on pipelines
set -eou pipefail

function start_redis() {
    cd /tmp
    nohup redis-server > /var/log/redis.log 2>&1 &
    cd -
}

function start_rabbitmq() {
    cd /tmp
    nohup rabbitmq-server > /var/log/rabbitmq.log 2>&1 &
    cd -
}

function start_celery() {
    cd /tmp
    nohup su-exec nobody celery -A api worker -Q priority.low,priority.middle,priority.high --loglevel=info > /var/log/celery.log 2>&1 &
    cd -
}

su-exec postgres pg_ctl -D "$PGDATA" start
start_redis
start_rabbitmq
start_celery
python3 manage.py check
coverage run manage.py test --settings=api.settings.testing --noinput api scheduler.tests --exclude-tag=auth
coverage report -m
