Framework: Laravel 5.2 Job Queue not processing handle() method

Created on 28 Apr 2016  路  16Comments  路  Source: laravel/framework

Hi ,

I am trying to make a queued command that collects a listing of data and saving to database. The command has two methods - the constructor and handle().

After the constructor does its thing, the handle() method should be called. In turn it should trigger makeRequest() which, when complete, should save data to database. I have found that if I place makeRequest() inside the handle() method, it is never called. If I place handle() in the constructor, it called fine but like sync driver (synchronously) .

One other thing to note, it works fine when I set QUEUE_DRIVER=sync in .env but with QUEUE_DRIVER=database does't not work .

here is my code :

`class SearchClansFromApi extends Job implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels;
protected $response;
protected $request;
protected $requestParams;

/**
 * Create a new job instance.
 *
 * @return void
 */
public function __construct($apiResponse, $requestParams)
{
    $this->response = $apiResponse;
    $this->request = new \Illuminate\Http\Request();
    $this->requestParams = $requestParams;

    Log::info('in construction');
}

/**
 * Execute the job.
 *
 * @return void
 */
public function handle()
{
    //this method never called 
    Log::useFiles(storage_path().'/laravel.log');
    makeRequest();

}`

Most helpful comment

Using
'default' => env('QUEUE_DRIVER', 'database'),
instead of
'default' => env('QUEUE_CONNECTION', 'database'),
on queue.php worked for me!

All 16 comments

but with QUEUE_DRIVER=database does't not work .

I'm not exactly sure what you mean? You have setup a queue job runner?

$this->request = new IlluminateHttpRequest();

You shouldn't put that in the constructor. Serializing that is probably a bad idea.

Thanks @GrahamCampbell . When i change queue_driver to database , then handle() method inside Job never called and only __construct() method called in Job .

When i change queue_driver to sync ,then both __construct() and handle() methods called and everything work correctly. But i need to use database driver and i need to get handle() method work in this driver.

That is correct. We call only construct, then queue it. You need to process the job off the queue.

@GrahamCampbell Thank you, i used supervisor for this and work perfect !
also i removed below code from constructor

$this->request = new IlluminateHttpRequest();

Thank you for time!

@alizamani I used supervisor too. But handle method is not called. I use supervisor with php artisan queue:work --daemon. construct method is called every time. I called job with Job::dispatch($model). Do you have any idea for it?

@GrahamCampbell If I called Command php artisan command:foo from construct, it takes too long to get response from browser. Am I doing something wrong? I use database as queue driver.

Same problem here,

php artisan queue:work just increments the "attempts" column, but the job handle() method is never called when driver is set to database.

Works fine with sync.

If I use php artisan queue:listen it just increments the "attempts" counter over and over on the same job, but the job handle() method is never called

Laravel 5.4

I have the same problem where the jobs created stuck in jobs table and never run, It happened after I manually clear the jobs table in database by deleting all rows, please let me know how to fix this.

update: I can use php artisan queue:work to make those jobs run, otherwise they won't get off from the table.

A quick thought, and this may or may not work, but the fact that you are using supervisor means that your workers are running in ram the whole time. If you make a change anywhere that your workers need to acess, you will be required to either restart your workers from supervisor, or use

./artisan queue:restart

(make sure your supervisor command is set to handle his properly as it actually tells the workers to quite/restart and doing it too often will cause supervisor to stop trying (or.. set startretries=99999999 :smile:)

supervisor

i have same problem.
how can i install supervisor in windows??

Thanks, @hrabbit. Running with supervisor on Ubuntu 16, and php artisan queue:restart solved some randon error that happened only in specific cases (but not always)

Using
'default' => env('QUEUE_DRIVER', 'database'),
instead of
'default' => env('QUEUE_CONNECTION', 'database'),
on queue.php worked for me!

Using
'default' => env('QUEUE_DRIVER', 'database'),
instead of
'default' => env('QUEUE_CONNECTION', 'database'),
on queue.php worked for me!

Best solution

Changing this option in queue.php worked for me in laravel 7.x
'default' => env('QUEUE_DRIVER', 'database'),
instead of
'default' => env('QUEUE_CONNECTION', 'database'),

Was this page helpful?
0 / 5 - 0 ratings

Related issues

klimentLambevski picture klimentLambevski  路  3Comments

gabriellimo picture gabriellimo  路  3Comments

JamborJan picture JamborJan  路  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments

Anahkiasen picture Anahkiasen  路  3Comments