问题
I am using laravel queues for commenting on the facebook post. When ever i recieve data from facebook webhook, based on the recieved details i am commenting on the post. To handle 100 responses at once from facebook webhook i am using laravel queues, so that it can execute one by one. I have used the step by step process as mentioned in https://scotch.io/tutorials/why-laravel-queues-are-awesome
public function webhooks(Request $request)
{
$data = file_get_contents('php://input');
Log::info("Request Cycle with Queues Begins");
$job = (new webhookQueue($data)->delay(10);
$this->dispatch($job);
Log::info("Request Cycle with Queues Ends");
}
and this is my job class structure
class webhookQueue extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
private $data;
public function __construct($data)
{
$this->data = $data;
}
public function handle()
{
//handling the data here
}
}
I am hitting webhooks() function continuously, all the jobs are working simultaneously but not in queue, none of the jobs are storing in jobs table, i have given delay but it is also not working, please some one help me, I have been trying from yesterday, but no result.
And this is my log in laravel.log
[2017-02-08 14:18:42] local.INFO: Request Cycle with Queues Begins
[2017-02-08 14:18:44] local.INFO: Request Cycle with Queues Begins
[2017-02-08 14:18:47] local.INFO: Request Cycle with Queues Begins
[2017-02-08 14:18:47] local.INFO: Request Cycle with Queues Begins
[2017-02-08 14:18:47] local.INFO: Request Cycle with Queues Begins
[2017-02-08 14:18:47] local.INFO: Request Cycle with Queues Begins
[2017-02-08 14:18:48] local.INFO: Request Cycle with Queues Begins
[2017-02-08 14:18:48] local.INFO: Request Cycle with Queues Begins
[2017-02-08 14:18:48] local.INFO: Request Cycle with Queues Begins
[2017-02-08 14:18:48] local.INFO: Request Cycle with Queues Begins
[2017-02-08 14:18:48] local.INFO: Request Cycle with Queues Begins
[2017-02-08 14:18:48] local.INFO: Request Cycle with Queues Begins
[2017-02-08 14:18:55] local.INFO: Request Cycle with Queues Ends
[2017-02-08 14:18:55] local.INFO: Request Cycle with Queues Ends
[2017-02-08 14:18:55] local.INFO: Request Cycle with Queues Ends
[2017-02-08 14:18:59] local.INFO: Request Cycle with Queues Ends
[2017-02-08 14:19:00] local.INFO: Request Cycle with Queues Ends
[2017-02-08 14:19:00] local.INFO: Request Cycle with Queues Ends
[2017-02-08 14:19:00] local.INFO: Request Cycle with Queues Ends
[2017-02-08 14:19:01] local.INFO: Request Cycle with Queues Ends
[2017-02-08 14:19:01] local.INFO: Request Cycle with Queues Ends
[2017-02-08 14:19:01] local.INFO: Request Cycle with Queues Ends
[2017-02-08 14:19:01] local.INFO: Request Cycle with Queues Ends
[2017-02-08 14:19:01] local.INFO: Request Cycle with Queues Ends
回答1:
for use queue you should some work :
in .env file you should change queue_driver from sync to database, so open .env and do the follow
queue_driver=database
after it you should create queue table in your database with artisan command :
php artisan queue:table
php artisan migrate
and finally you should run your queue with php artisan queue:listen
or php artisan queue:work
回答2:
I had the same trouble, if you are using laravel 5.7, use this in .env file
QUEUE_CONNECTION=database
after, clear config cache like this
php artisan config:cache
回答3:
Update for Laravel 5.7:
In .env
, set QUEUE_CONNECTION=database
so that dispatched jobs go to the database driver.
Then:
# Creates a migration for the database table holding the jobs
php artisan queue:table
# Executes the migration
php artisan migrate
# Kicks off the process that executes jobs when they are dispatched
php artisan queue:work
回答4:
The accepted answer was a problem for me, but I also wound up on this question for 2 other similar problems which I solved, and maybe they will help other people that wind up here.
Other problem 1: job creation (constructor) works, but job handler does not fire - ever.
- the ever is key here because, typically, if none of them ever fire, then it could be because your job code is modified and your queue should be restarted.
Other problem 2: job creation (constructor) works, but job handler does not fire - sometimes.
- when sometimes is true for you, then it may be that your jobs that are not firing are because they are happening while in a transaction, like a
DB::beginTransaction
.
Assuming I want the job to fire even during a transaction, I can do this:
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($errorInfo)
{
$this->errorInfo = $errorInfo;
// Queues won't run the handlers during transactions
// so, detect the level, and if it is not 0, rollback to force job handling
if (\DB::transactionLevel() != 0) {
\DB::rollBack();
}
}
BUT DON'T DO THIS unless you want to fire your job and force rollback. My situation is unique in that my job sends emails on FATAL errors, so I want it to fire because I have an error breaking the process anyway (rollback going to happen and is unplanned due to uncaught error).
Here's a situation when you wouldn't want to do this:
- your job sends an email to a user when payment is successful
- your payment could be successful, could not be
- depending on successful payment, you rollback or commit
You should structure your dispatch to happen AFTER you rollback or commit. I did not have that luxury for my job because it happens when I cannot predict (a FATAL error). But if you have control over it, like knowing your payment is successful, dispatch after you have committed, or exited all levels of transactions!
I am not sure of the behavior of triggering a job while in the transaction, and then rolling back or committing. It could be worked around if it didn't work properly by adding a delay, but that seems unreliable (guessing at how long to wait) unless it was a significant delay.
回答5:
I am seeing that you already have Queue table.
Try running php artisan queue:listen --tries=3
or php artisan queue:work
etc.
Queue work is for executing only one Job per command. So if there are 20 jobs in the table you might have to run queue work 20 times. That's why you can run queue:listen
command. But it eats up a lot of CPU.
In the server, you might want to run your queue listen with max 3 tries in the background.
SSH to your server in the Terminal / Command Prompt. Then CD
to your project directory where the artisan file lives. Run this command:
nohup php artisan queue:listen --tries=3 > /dev/null 2>&1 &
In this case jobs will automatically be processed in the background. You just have to dispatch the job. And I would recommend using failed-jobs table. If you are using background queue listner.
Hope this helps.
回答6:
For future readers of that questions , if queues were working before but no longer , just try to delete everything from the jobs table in the database , that worked for me .
来源:https://stackoverflow.com/questions/42133628/laravel-queues-not-working