0

I'm building an app to generate catalogs. Data I need to load are often more than 50mb so to do not disrupt user experience I tried to use a queue in Laravel.

I have a job class:

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Http\Request;


use PDF;
use App\Jobs\ProcessCatalog;

class ProcessCatalog implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $id;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($id)
    {
        $this->id=$id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
             //code which generate catalogs
    }
}

I tried to run this with:

public function generateC() {

    ProcessCatalog::dispatch(1);

return 'it works'; 
}

and everything works fine when the queue is sync but when I QUEUE_DRIVER=sync to QUEUE_DRIVER=database everything seems that work but catalog never generated...

I need to run queue async? What is the best way to do that?

2
  • did you install database table? Commented Apr 1, 2020 at 22:20
  • yes, I install database table of course... when I run with artisan work well but when I try to run from controller doesn't work Commented Apr 2, 2020 at 9:33

1 Answer 1

1

To Laravel database queue driver, you must first migrate the queue table:

php artisan queue:table

php artisan migrate

And you must then run the queue worker:

php artisan queue:work
Sign up to request clarification or add additional context in comments.

5 Comments

I migrate the tables but I want to run queue from Controller, not from artisan... when I run from artisan works well...
@AleksPer Then use the 'sync' driver but it completely defeats the point of using queues. Controllers only run when there's a request, it's synchronous. Queues are used to run tasks asynchronously, when there's no requests. To accomplish this, you need a process to run in the background and watch for jobs in the queue.
I'm creating catalogs and I need to create only when the user requests it. Because of UX I don't want to block a user from other actions so I need to generate catalogs in the background using queue... I'm thinking now might be better to use CRON
@AleksPer CRON is for scheduled tasks. I think queues are perfect for what you want to do. I know it can be annoying to run and monitor a queue worker but it's the best way to do it IMO. All the best.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.