sumonmselim's avatar

Laravel Jobs, Queue_driver=sync vs Queue_driver=database

I am trying to upload an image to the server and process the image in the background. When the user uploads an image, I dispatch a job from the controller. The procedures are, first upload the file to the temporary location. In this case my temporary location isstorage>uploads> and then I grab the file, rename the file and resize it and then finally move the file to the public/ folder. After this, I update the database and remove the file from the temporary location. Everything works fine when the QUEUE_DRIVER is set to sync. But when I change the QUEUE_DRIVER=sync to QUEUE_DRIVER=database, I see a row in the jobs table attempting again and again to dispatch the job properly.(php artisan queue:work command is running). I cant figure out what the problem is. Please help, Here is whats in the jobs table:

| id | queue   | payload| attempts | reserved_at | available_at | created_at |

| 53 | default | {"job":"Illuminate\\Queue\\CallQueuedHandler@call","data":{"commandName":"App\\Jobs\\UploadImage","command":"O:20:\"App\\Jobs\\UploadImage\":6:{s:7:\"channel\";O:45:\"Illuminate\\Contracts\\Database\\ModelIdentifier\":2:{s:5:\"class\";s:18:\"App\\Models\\Channel\";s:2:\"id\";i:1;}s:6:\"fileId\";s:14:\"1580f88fb85a46\";s:6:\"\u0000*\u0000job\";N;s:10:\"connection\";N;s:5:\"queue\";N;s:5:\"delay\";N;}"}} |       52 |        NULL |   1477413281 | 1477413281 |

In controller method:

if ($request->file('image')) {
          $fileId = uniqid(true);
          $request->file('image')->move(storage_path().'/uploads', $fileId);
          // dispatching job
          $this->dispatch(new UploadImage($user_channel, $fileId));
        }

UploadImage.php

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Models\Channel;
use File;
use Image;
class UploadImage implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    public $channel;
    public $fileId;
    
    public function __construct(Channel $channel, $fileId)
    {
        $this->channel = $channel;
        $this->fileId = $fileId;
    }

    
    public function handle()
    {
      // grabing the file from storage
      $file = storage_path()."/uploads/$this->fileId";
      
      // renaming/converting the file to jpg
      $file_name = $this->fileId.'.jpg';
      
      $original_path = public_path('/channel/original/');
      $thumbnail_path = public_path('/channel/thumbnail/');
      // resizing it
      Image::make($file)
              ->resize(320,null,function ($constraint) {
                $constraint->aspectRatio();
                 })
              ->save($original_path . $file_name)
              ->resize(90, 90)
              ->save($thumbnail_path . $file_name);

        //save to database
        $user_channel = Channel::findOrFail($this->channel->id);
        $user_channel->update([
          'image_filename' => $file_name
        ]);
        //remove it from storage
        unlink(storage_path()."/uploads/$this->fileId");
    }
}
0 likes
1 reply
Benja's avatar

i know am late to the party, but i think the problem is that you have not migrated the queues table, use this command php artisan queue:table then this php artisan migrate

1 like

Please or to participate in this conversation.