Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

webdevb's avatar

Laravel Job -> Stuck in loop, MySQL gone away in logs.

Hi Guys,

Hope you're well, I'm having a little issue with a job I've created. The job resizes images that I've uploaded to the application.

Here is the job:


<?php

namespace App\Jobs;

use App\Jobs\Job;
use Image;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class resizeImages extends Job implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    protected $img;
    protected $destinationPath;
    protected $filename;
    protected $uploadcount;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($img,$destinationPath,$filename,$uploadcount)
    {
        $this->img = $img;
        $this->destinationPath = $destinationPath;
        $this->filename = $filename;
        $this->uploadcount = $uploadcount;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
            
            // crop the best fitting 5:3 (600x360) ratio and resize to 600x360 pixel
            // backup status
            echo 'make backup';
            $img = Image::make($this->img);
            $img->backup();

            // perform some modifications
            echo 'start 1';
            $img->fit(1024, 768);
            $img->save($this->destinationPath.'/'.'1024x768-'.$this->filename);

            // reset image (return to backup state)
            echo 'reset';
            $img->reset();

            // perform other modifications
            echo 'start 2';
            $img->fit(1152, 864);
            $img->save($this->destinationPath.'/'.'1152x864-'.$this->filename);

            // reset image (return to backup state)
            echo 'reset';
            $img->reset();

            // perform other modifications
            echo 'start 3';
            $img->fit(1208, 720);
            $img->save($this->destinationPath.'/'.'1208x720-'.$this->filename);

            // reset image (return to backup state)
            echo 'reset';
            $img->reset();

            // perform other modifications
            echo 'start 4';
            $img->fit(1280, 800);
            $img->save($this->destinationPath.'/'.'1280x800-'.$this->filename);

            // reset image (return to backup state)
            echo 'reset';
            $img->reset();

            // perform other modifications
            echo 'start 5';
            $img->fit(1280, 960);
            $img->save($this->destinationPath.'/'.'1280x960-'.$this->filename);

            // reset image (return to backup state)
            echo 'reset';
            $img->reset();

            // perform other modifications
            echo 'start 6';
            $img->fit(1280, 1024);
            $img->save($this->destinationPath.'/'.'1280x1024-'.$this->filename);

            // reset image (return to backup state)
            echo 'reset';
            $img->reset();

            // perform other modifications
            echo 'start 7';
            $img->fit(1366, 768);
            $img->save($this->destinationPath.'/'.'1366x768-'.$this->filename);

            // reset image (return to backup state)
            echo 'reset';
            $img->reset();

            // perform other modifications
            echo 'start 8';
            $img->fit(1440, 900);
            $img->save($this->destinationPath.'/'.'1440x900-'.$this->filename);

            // reset image (return to backup state)
            echo 'reset';
            $img->reset();

            // perform other modifications
            echo 'start 9';
            $img->fit(1600, 1200);
            $img->save($this->destinationPath.'/'.'1600x1200-'.$this->filename);

            // reset image (return to backup state)
            echo 'reset';
            $img->reset();

            // perform other modifications
            echo 'start 10';
            $img->fit(1680, 1050);
            $img->save($this->destinationPath.'/'.'1680x1050-'.$this->filename);

            // reset image (return to backup state)
            echo 'reset';
            $img->reset();

            // perform other modifications
            echo 'start 11';
            $img->fit(1920, 1080);
            $img->save($this->destinationPath.'/'.'1920x1080-'.$this->filename);

            // reset image (return to backup state)
            echo 'reset';
            $img->reset();

            // perform other modifications
            echo 'start 12';
            $img->fit(1920, 1200);
            $img->save($this->destinationPath.'/'.'1920x1200-'.$this->filename);

            // reset image (return to backup state)
            echo 'reset';
            $img->reset();

            // perform other modifications
            echo 'start 13';
            $img->fit(2048, 1536);
            $img->save($this->destinationPath.'/'.'2048x1536-'.$this->filename);

            // reset image (return to backup state)
            echo 'reset';
            $img->reset();

            // perform other modifications
            echo 'start 14';
            $img->fit(2560, 1600);
            $img->save($this->destinationPath.'/'.'2560x1600-'.$this->filename);
            echo 'finish 14';
    }
}

Any help would be great.

0 likes
7 replies
webdevb's avatar

Haha sorry, basically when you run the job it seems to process 3 jobs and then get stuck in a loop... I've added some echos to the job so I can see where it's getting stuck but it doesnt seem to be... it runs all the way through and echos finish14 but it doesn't close the job it just retries it.

Thanks.

shez1983's avatar

i seem to recall that I was also having this problem - whereby it processes the job but also retries it (or rather doesnt remove it from the queue - i changed it from queue to sync as it wasnt really worth my while investigating..

try returning true at the end to let it know it has done what you need it to do, although I dont think that is necessary.. :s

webdevb's avatar

Anyone able to help on this one guys??

webdevb's avatar

Ok, so I've changed the way it runs now to this... it seems to run locally but on the server it seems to get stuck... I can see it echo's 'done' but doesn't actually release the job.

<?php

namespace App\Jobs;

use App\Jobs\Job;
use Image;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class resizeImages extends Job implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    protected $img;
    protected $destinationPath;
    protected $filename;
    protected $uploadcount;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($img,$destinationPath,$filename,$uploadcount)
    {
        $this->img = $img;
        $this->destinationPath = $destinationPath;
        $this->filename = $filename;
        $this->uploadcount = $uploadcount;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle() {
            
            // crop the best fitting 5:3 (600x360) ratio and resize to 600x360 pixel        
            resizeFit('1024', '768', $this->img, $this->destinationPath, $this->filename);
            resizeFit('1152', '864', $this->img, $this->destinationPath, $this->filename);
            resizeFit('1208', '720', $this->img, $this->destinationPath, $this->filename);
            resizeFit('1280', '800', $this->img, $this->destinationPath, $this->filename);
            resizeFit('1280', '960', $this->img, $this->destinationPath, $this->filename);
            resizeFit('1280', '1024', $this->img, $this->destinationPath, $this->filename);
            resizeFit('1366', '768', $this->img, $this->destinationPath, $this->filename);
            resizeFit('1440', '900', $this->img, $this->destinationPath, $this->filename);
            resizeFit('1600', '1200', $this->img, $this->destinationPath, $this->filename);
            resizeFit('1680', '1050', $this->img, $this->destinationPath, $this->filename);
            resizeFit('1920', '1080', $this->img, $this->destinationPath, $this->filename);
            resizeFit('1920', '1200', $this->img, $this->destinationPath, $this->filename);
            resizeFit('2048', '1536', $this->img, $this->destinationPath, $this->filename);
            resizeFit('2560', '1600', $this->img, $this->destinationPath, $this->filename);

    }

}

    function resizeFit($size1, $size2, $img, $destination, $filename) {
        $img = Image::make($img);
        $img->fit($size1, $size2);
        $img->save($destination.'/'.$size1.'x'.$size2.'-'.$filename);
    }

The error that I get in the logs is

[2017-01-30 12:32:02] local.ERROR: ErrorException: PDO::beginTransaction(): MySQL server has gone away in /home/admin/web/admin/public_html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:
576
Stack trace:
#0 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'PDO::beginTrans...', '/home/admin/web...', 576, Array)
#1 /home/admin/web/admin/public_html/vendor/laravel/framework/src/Illuminate/Database/Connection.php(576): PDO->beginTransaction()
#2 /home/admin/web/admin/public_html/vendor/laravel/framework/src/Illuminate/Queue/DatabaseQueue.php(258): Illuminate\Database\Connection->beginTransaction()
#3 /home/admin/web/admin/public_html/vendor/laravel/framework/src/Illuminate/Queue/Jobs/DatabaseJob.php(61): Illuminate\Queue\DatabaseQueue->deleteReserved('default', 1)
#4 /home/admin/web/admin/public_html/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(44): Illuminate\Queue\Jobs\DatabaseJob->delete()
#5 /home/admin/web/admin/public_html/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(130): Illuminate\Queue\CallQueuedHandler->call(Object(Illuminate\Queue\Jobs\DatabaseJob), Array)
#6 /home/admin/web/admin/public_html/vendor/laravel/framework/src/Illuminate/Queue/Jobs/DatabaseJob.php(49): Illuminate\Queue\Jobs\Job->resolveAndFire(Array)
#7 /home/admin/web/admin/public_html/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(213): Illuminate\Queue\Jobs\DatabaseJob->fire()
#8 /home/admin/web/admin/public_html/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(156): Illuminate\Queue\Worker->process('database', Object(Illuminate\Queue\Jobs\DatabaseJob), '0', '0')
#9 /home/admin/web/admin/public_html/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(125): Illuminate\Queue\Worker->pop('', 'default', '0', '3', '0')
#10 /home/admin/web/admin/public_html/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(78): Illuminate\Queue\Console\WorkCommand->runWorker('', 'default', '0', '1024', false)
#11 [internal function]: Illuminate\Queue\Console\WorkCommand->fire()
#12 /home/admin/web/admin/public_html/vendor/laravel/framework/src/Illuminate/Container/Container.php(507): call_user_func_array(Array, Array)
#13 /home/admin/web/admin/public_html/vendor/laravel/framework/src/Illuminate/Console/Command.php(169): Illuminate\Container\Container->call(Array)
#14 /home/admin/web/admin/public_html/vendor/symfony/console/Command/Command.php(256): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Conso
le\Output\ConsoleOutput))

I'm running VestaCP.

Cronix's avatar

Google the error "mysql has gone away". There are several possible reasons, like the job it taking too long and hits WAIT_TIMEOUT limit or MAX_ALLOWED_PACKET of mysql or something. This is more likely a mysql setting issue rather than an actual laravel issue.

Please or to participate in this conversation.