1

I am trying to store data in database by using laravel queue. I want to show user a quick message that "your file is being imported" and want to store data using queue. I have used database as QUEUE_DRIVER in env file. When I try to store data it creates entry in jobs table but doesn't store anything in demo_tbl.

Here is what I have done so far -
Controller

public function import(Request $request)
{
    Excel::filter('chunk')->load($request->file('import_file')->getRealPath())->chunk(250, function($reader)
    {
        $this->dispatch(new ImportDistributor($reader->toArray()));
    });

    dd('your file is being imported. we will inform you once it is done.');
}

Job -

<?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 App\Model\DemoTableModel;

use DB;

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

    /**
     * Create a new job instance.
     *
     * @return void
     */
   protected $data;

    public function __construct(array $request)
    {
        $this->data = $request;
    }

    public function handle()
    {
        if(isset($this->data) && !empty($this->data))
        {
            foreach($this->data as $val)
            {
                DemoTableModel::insert($val);
            }
        }
    }
}    

Model

<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class DemoTableModel extends Model
{
    protected $table    = 'demo_tbl';
    protected $fillable = [
                              'name', 
                              'city'
                          ];
}

Any help would be appreciated.
Thanks.

amM
  • 509
  • 2
  • 14
  • 33

1 Answers1

1

If you are using queue, you are probably doing some time-consuming tasks that you want Laravel to handle asynchronously. The tasks that you put into your jobs database need to be fetched one by one by Laravel's Queue Worker.

You have to manually call

$ php artisan queue:work

to tell artisan to start your queue worker.

Check out Laravel's official documentation on this:

Laravel Queue Worker

Eagle
  • 327
  • 3
  • 13
  • Yes used php artisan queue:work and reflects changes on job table but doesn't store data in demo_tbl – amM Oct 11 '18 at 05:24
  • @Deepak @Deepak `php artisan queue:work` calls the queue worker to actually process your tasks in your jobs table. If you run that command, check if there's any output saying that the worker is processing your jobs, if yes, check if the output says that processing is successful (and the entries in your jobs table would be automatically deleted). Also check the jobs table to see the tries field to check if the queue worker has tried to process your task beyond default limit times. In that case, check the logic in your job to see if you are doing right. – Eagle Oct 11 '18 at 05:31
  • I run php artisan queue:work it shows [2018-10-11 05:21:38][998] Processing but doesn't get any success message. If i change queue_driver to sync in that case it stores data in demo_tbl. – amM Oct 11 '18 at 05:37
  • @Deepak Can you check your jobs table to see if entries in your jobs table decrease? What does `attempts` columns show in your jobs entries, 0 or larger? – Eagle Oct 11 '18 at 05:45
  • firstly attempt column has value 1 but at the end it is 255. – amM Oct 11 '18 at 05:48
  • @Deepak The default maximum tries for the queue worker is set to 255, if you see 255 in your `attempts` columns, it means that the worker tried to run your single job that many times and has failed. Try to pin point the error when the worker is executing your job. – Eagle Oct 11 '18 at 05:54
  • Can you tell me how to find error in queue jobs? – amM Oct 11 '18 at 05:56
  • @Deepak In your job logic, you can still put `echo` or debugging statements, and try to print out something to see if the expected function(s) are called, and if there's any error output when the worker is processing your job(s). – Eagle Oct 11 '18 at 06:04
  • @Deepak You can also check out `Failed Job Events` section in the official doc: https://laravel.com/docs/5.7/queues/#dealing-with-failed-jobs – Eagle Oct 11 '18 at 06:10
  • Okay let me try it. Thanks – amM Oct 11 '18 at 06:12
  • `queue:work` are just processing one queue item but I have 20 queue items in the database and more getting added up. @Eagle – Ankit Jindal Sep 03 '20 at 06:36