#!/bin/bash

_timeout=150
_count=0
_status=0

# Try to start crono as many times as needed so it can connect to the DB.
while [ "$_count" -le "$_timeout" ]; do
    bundle exec crono
    _status=$?
    if [ -z $_status ]; then
        echo "Crono terminated successfully"
        exit 0
    fi
    if [ "$_status" -ge "128" ]; then
        echo "Signal $((_status-128)) received"
        exit $_status
    fi

    # There has been a failure, increment the count and sleep.
    echo "Crono terminated with status: $_status. Trying again in 10 seconds."
    sleep 10
    _count=$((_count+10))
done

# After trying 15 times we couldn't make it, just quit.
echo "Cannot start crono in any way. Check the logs..."
exit $_status
