Laravel Database Queue on Shared Hosting

Sometimes it’s necessary to host a Laravel app on shared / cPanel hosting, but we recently ran in to an issue where using the queue was necessary.

Given that Beanstalkd, SQS and Redis weren’t possible, we looked into using the database driver and a the scheduler cron job.

#

protected function osProcessIsRunning($needle)
    {
        // get process status. the "-ww"-option is important to get the full output!
        exec('ps aux -ww', $process_status);

        // search $needle in process status
        $result = array_filter($process_status, function($var) use ($needle) {
            return strpos($var, $needle);
        });

        // if the result is not empty, the needle exists in running processes
        if (!empty($result)) {
            return true;
        }
        return false;
    }

And this code to call it in method schedule()

// start the queue worker, if its not running
if (!$this->osProcessIsRunning('queue:work')) {
    $schedule->command('queue:work')->everyMinute();
}

https://laracasts.com/discuss/channels/servers/database-queue-on-shared-hosting