php Laravel 队列不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42133628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel queues not working
提问by Punabaka Abhinav
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
我正在使用 laravel 队列对 facebook 帖子发表评论。当我从 facebook webhook 收到数据时,根据收到的详细信息,我正在评论该帖子。为了一次处理来自 facebook webhook 的 100 个响应,我使用了 laravel 队列,以便它可以一一执行。我已经使用了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;
使用 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.
我一直在使用 webhooks() 函数,所有作业都在同时工作但不在队列中,没有作业存储在作业表中,我给了延迟但它也不起作用,请有人帮助我,我一直在从昨天开始尝试,但没有结果。
And this is my log in laravel.log
这是我在 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
回答by Mahdi Youseftabar
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
在 .env 文件中,您应该将 queue_driver 从同步更改为数据库,因此打开 .env 并执行以下操作
queue_driver=database
after it you should create queue table in your database with artisan command :
之后,您应该使用 artisan 命令在您的数据库中创建队列表:
php artisan queue:table
php artisan migrate
and finally you should run your queue with php artisan queue:listen
or php artisan queue:work
最后你应该用php artisan queue:listen
或运行你的队列php artisan queue:work
回答by Haydar ?AH?N
I had the same trouble, if you are using laravel 5.7, use this in .env file
我遇到了同样的问题,如果您使用的是 laravel 5.7,请在 .env 文件中使用它
QUEUE_CONNECTION=database
after, clear config cache like this
之后,像这样清除配置缓存
php artisan config:cache
回答by Jon McClung
Update for Laravel 5.7:
Laravel 5.7 更新:
In .env
, set QUEUE_CONNECTION=database
so that dispatched jobs go to the database driver.
在 中.env
,设置QUEUE_CONNECTION=database
以便分派的作业进入数据库驱动程序。
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
回答by amurrell
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.
接受的答案对我来说是个问题,但我也解决了我解决的其他 2 个类似问题的这个问题,也许它们会帮助到这里结束的其他人。
Other problem 1: job creation (constructor) works, but job handler does not fire - ever.
其他问题 1:作业创建(构造函数)有效,但作业处理程序永远不会触发。
- the everis 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.
其他问题 2:作业创建(构造函数)有效,但作业处理程序不触发 -有时。
- when sometimesis 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
.
- 如果有时对您来说是正确的,那么您的作业没有被解雇可能是因为它们在事务中发生,例如
DB::beginTransaction
.
Assuming I wantthe 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 THISunless you wantto 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).
但不要这样做,除非你想解雇你的工作并强制回滚。我的情况是独一无二的,因为我的工作发送关于 FATAL 错误的电子邮件,所以我希望它被触发,因为我有一个错误破坏了进程(回滚将会发生并且由于未捕获的错误而没有计划)。
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 thenrolling 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.
我不确定在事务中触发作业然后回滚或提交的行为。如果它不能通过添加延迟正常工作,它可以解决,但这似乎不可靠(猜测要等待多长时间),除非它是一个显着的延迟。
回答by ADEL NAMANI
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 .
对于那些问题的未来读者,如果队列以前有效但不再有效,只需尝试从数据库中的作业表中删除所有内容,这对我有用。
回答by Jose G.
Make sure your app is not in maintenance mode... I had mine in maintenance, but allowing my local ip address... I couldn't figure out why it was not running. I had to finally go debugging the WorkCommand to find out...
确保您的应用程序未处于维护模式...我的应用程序处于维护状态,但允许我的本地 IP 地址...我无法弄清楚为什么它没有运行。我不得不最终去调试 WorkCommand 以找出...
./artisan up;
./工匠起来;
回答by TipuZaynSultan
I am seeing that you already have Queue table.
我看到你已经有了 Queue table。
Try running php artisan queue:listen --tries=3
or php artisan queue:work
etc.
尝试运行php artisan queue:listen --tries=3
或php artisan queue:work
等。
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.
队列工作用于每个命令仅执行一个作业。因此,如果表中有 20 个作业,您可能必须运行 queue work 20 次。这就是为什么你可以运行queue:listen
命令。但它吃掉了很多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:
在服务器中,您可能希望在后台运行最多 3 次尝试的队列侦听。在终端/命令提示符中通过 SSH 连接到您的服务器。然后CD
到工匠文件所在的项目目录。运行此命令:
nohup php artisan queue:listen --tries=3 > /dev/null 2>&1 &
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.
在这种情况下,作业将在后台自动处理。你只需要派遣工作。我建议使用 failed-jobs 表。如果您使用后台队列侦听器。
Hope this helps.
希望这可以帮助。