Digital Ocean Laravel Schedule without a Worker

Digital Ocean Laravel Schedule without a Worker

2 months ago I came to the resolution to move an app I was working on from a droplet on Digital Ocean that was running Dokku. It was running well only that with the advent of the Digital Ocean App Platform which comes at a reasonable cost and is easier to deploy on, I felt it prudent to shift the code there. In addition, the Git-based deployment was a plus as I only had to push code to Github and everything is taken care of.

After the initial deployment was done, I had one requirement that I hadn't fulfilled. On the previous droplet, there was a Cron job responsible for sending notifications via Firebase to mobile clients and with the app platform, it would mean I making a worker which would incur an extra cost.

Initially, the cron job was hitting and endpoint but then Laravel has a cool schedule feature and I could remove the cron job. But the scheduler has a requirement where there needs to be a cron job running it. The other alternative is to have the scheduler running locally using the php artisan schedule:work command.

With this background and a number of searches, I came across a nifty solution which would enable running of the scheduler by editing the run command on the app platform interface as follows.

vendor/bin/heroku-php-apache2 public/ &
    while true; do
        echo "=> Running scheduler"
        php artisan schedule:work || true;
        echo "=> Sleeping for 60 seconds"
        sleep 60;
    done

To run the queue alone, the command can be edited to:

vendor/bin/heroku-php-apache2 public/ &
    while true; do
        echo "=> Running queue"
        php artisan queue:work || true;
        echo "=> Sleeping for 60 seconds"
        sleep 60;
    done

Cheers