laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.21k stars 10.9k forks source link

Laravel 5.2 Job Queue not processing handle() method #13362

Closed alizamani closed 8 years ago

alizamani commented 8 years ago

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();

}`

GrahamCampbell commented 8 years ago

but with QUEUE_DRIVER=database does't not work .

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

GrahamCampbell commented 8 years ago

$this->request = new \Illuminate\Http\Request();

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

alizamani commented 8 years ago

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.

GrahamCampbell commented 8 years ago

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

GrahamCampbell commented 8 years ago

https://laravel.com/docs/5.2/queues#running-the-queue-listener

alizamani commented 8 years ago

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

$this->request = new \Illuminate\Http\Request();

Thank you for time!

yanlinaung30 commented 6 years ago

@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?

yanlinaung30 commented 6 years ago

@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.

mirko77 commented 6 years ago

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

WueXiang commented 6 years ago

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.

hrabbit commented 6 years ago

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:)

badrizahra commented 5 years ago

supervisor

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

fititnt commented 5 years ago

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)

jorgaires commented 5 years ago

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

symbol3382 commented 4 years ago

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

Best solution

somayajulu commented 4 years ago

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'),

Dilshan97 commented 3 years ago

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

It's work for me. thanks