#!/bin/sh
set -e

echo "Deploying production application ..."
    # down laravel
    php artisan down
    # Update codebase
    git fetch origin prod_deploy
    git reset --hard origin/prod_deploy
    #composer install
    composer install --quiet
    # migrate db
    yes | php artisan migrate
    # clear cache
    php artisan optimize:clear
    # caching
    php artisan optimize

    # Restart Queue Worker
    echo "Restarting queue worker..."
    if pgrep -f "artisan queue:work" > /dev/null; then
        echo "Stopping existing queue worker..."
        pkill -f "artisan queue:work"
    fi
    nohup /usr/local/bin/ea-php80 artisan queue:work --tries=3 --timeout=1800 --queue=default > storage/logs/prod_queue.log 2>&1 &

    #up laravel
    php artisan up

echo "Application production deployed!"