Trouble getting Cron job to run perfectly

1.2k Views Asked by At

I use Laravel/Lumen. I have set up a simple job to send an SMS:

<?php

namespace App\Jobs;

class SendDepositSMSAlertJob extends Job
{
    private $numbers;
    private $account;
    private $type;
    private $amount;
    private $date;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($account, $type, $amount, $date, $numbers)
    {
        $this->numbers = $numbers;
        $this->account = $account;
        $this->type = $type;
        $this->amount = $amount;
        $this->date = $date;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        foreach ($this->numbers as $number) {
            // Settings
            $url = "https://api.xxxxx.com/v3/messages/send";
            $from = "XX";
            $message = urlencode($this->account." has been credited with a " . $this->type . " deposit of GHs " . $this->amount . " on " . $this->date);

            $client_id = "xxxxxx";
            $client_secret = "xxxxxxx";
            $query_string = "?From=".$from."&To=".$number."&Content=".$message."&ClientId=".$client_id."&ClientSecret=".$client_secret."&RegisteredDelivery=true";

            $response = @file_get_contents($url.$query_string);
        }
    }
}

Now whenever a record is saved in my controller I dispatch the job:

$this->dispatch(new SendDepositSMSAlertJob($bank . ' - ' . $acNumber, $depositType, $depositAmount, $reportDate, $telNumbers));

Now I have the following setup in my kernel.php:

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        \JK\Dingo\Api\Console\Commands\RouteListCommand::class
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // Run once a minute
        $schedule->command('queue:work')->everyMinute()->withoutOverlapping();
    }
}

And in my cPanel I have the following cron job set up:

php -d register_argc_argv=On /home/xxxxxx/xxxxxx/artisan schedule:run

The above command is set to run ever minute.

Problem This setup simply does not work for me. On my Bluehost VPS it caused my server to crash many times forcing me to reboot. Apparently it was eating up so much memory and I don't understand why, and support wasn't able to help me. Let me clarify, it worked but caused my server to crash at least twice a day.

So thinking it had something to do with my hosting I purchased another hosting somewhere else (SiteGround). Now I can't even get cron to work. Each time cron executes this is sent to my mail:

Fatal error: Uncaught exception 'ErrorException' with message 'proc_open(): fork failed - Resource temporarily unavailable' in /home/xxxxx/xxxxx/vendor/symfony/console/Application.php:941
Stack trace:
#0 [internal function]: Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}(2, 'proc_open(): fo...', '/home/abc17060/...', 941, Array)
#1 /home/xxxxx/xxxxx/vendor/symfony/console/Application.php(941): proc_open('stty -a | grep ...', Array, NULL, NULL, NULL, Array)
#2 /home/xxxxx/xxxxx/vendor/symfony/console/Application.php(729): Symfony\Component\Console\Application->getSttyColumns()
#3 /home/xxxxx/xxxxx/vendor/symfony/console/Application.php(690): Symfony\Component\Console\Application->getTerminalDimensions()
#4 /home/xxxxx/xxxxx/vendor/symfony/console/Application.php(623): Symfony\Component\Console\Application->getTerminalWidth()
#5 /home/xxxxx/xxxxx/vendor/laravel/lumen-framework/src/Exceptions/Handler.php(147): Symfony\Component\Console\Application->renderException(Object(ErrorException),  in /home/xxxxx/xxxxx/vendor/symfony/console/Application.php on line 941


Fatal error: Uncaught exception 'ErrorException' with message 'proc_open(): fork failed - Resource temporarily unavailable' in /home/xxxxx/xxxxx/vendor/symfony/console/Application.php:941
Stack trace:
#0 [internal function]: Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}(2, 'proc_open(): fo...', '/home/abc17060/...', 941, Array)
#1 /home/xxxxx/xxxxx/vendor/symfony/console/Application.php(941): proc_open('stty -a | grep ...', Array, NULL, NULL, NULL, Array)
#2 /home/xxxxx/xxxxx/vendor/symfony/console/Application.php(729): Symfony\Component\Console\Application->getSttyColumns()
#3 /home/xxxxx/xxxxx/vendor/symfony/console/Application.php(690): Symfony\Component\Console\Application->getTerminalDimensions()
#4 /home/xxxxx/xxxxx/vendor/symfony/console/Application.php(623): Symfony\Component\Console\Application->getTerminalWidth()
#5 /home/xxxxx/xxxxx/vendor/laravel/lumen-framework/src/Exceptions/Handler.php(147): Symfony\Component\Console\Application->renderException(Object(Symfony\Component in /home/xxxxx/xxxxx/vendor/symfony/console/Application.php on line 941

Now I have no idea what is causing this or how to fix it. I believe I have followed all instructions in the docs and can't tell what I am doing wrong here. I have done research and contacted support on both hosting companies but still...

This was the last message I had from support:

I tried to ran the cron a few times but it does not have any scheduled commands to run:

xxxxxx@c27664 [~/xxxxx]# php -d register_argc_argv=On /home/xxxxxx/xxxxxx/artisan schedule:run
No scheduled commands are ready to run.

Also, I have increased the limits once more time. I have set 300 seconds for the execution of the script and 2048MB memory_limit.

Please, try again and if you need any assistance do not hesitate to update the ticket.

Is there something wrong with Lumen and jobs? I run command successfully in Laravel 5.1 what is going on here.

Alternatively if there is a much simpler or efficient way I can execute this script using regular PHP via cron, please show me how.

1

There are 1 best solutions below

0
On

The queue:work command is designed to be run as a daemon. Running inside a laravel cron task basically doubles it's footprint.

Running it in daemon mode can be tricky on shared hosting, and it's 100% dependent on what the cPanel allows you to do. If you have access to "supervisord" then magic can happen by following the Laravel Docs on setting it up.

If you cannot set it up as a daemon on your shared hosting, you can try to run the queue:work directly from the control panel's CRON runner instead of wrapping it inside of a Laravel CRON.

php -d register_argc_argv=On /home/xxxxxx/xxxxxx/artisan queue:work --once

This will run it, process one item in the queue and exit. It won't be as fast as a daemonized processor, but it should work.

You can always just run the queue from the command line (which you have access to) and watch it to make sure it even works.

php -d register_argc_argv=On /home/xxxxxx/xxxxxx/artisan queue:work will run it as a foreground process and you can watch if it does anything.

Hopefully something in here can help.