如何在 Laravel 中检查 Beanstalkd 队列中的作业是否已完成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27888396/
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
How to check if job in in Beanstalkd queue has been completed in Laravel
提问by Marcin Nabia?ek
I need to check if job added to queue (Beanstalkd) has been completed in Laravel (Laravel 5) and in case it's completed I need to return updated record (task added to queue updates record in database). I've added to my composer.json
:
我需要检查添加到队列(Beanstalkd)的作业是否已在 Laravel(Laravel 5)中完成,如果完成,我需要返回更新的记录(添加到数据库中队列更新记录的任务)。我已经添加到我的composer.json
:
"pda/pheanstalk": "3.*"
I add job to queue this way:
我以这种方式将作业添加到队列中:
$jobId = Queue::push('App\Class', $object->toArray(), $this->getQueueName());
I use to check if job was completed using is the following function:
我用来检查作业是否已完成使用以下功能:
public function find($queueName, $jobId, $recordId)
{
$phean = Queue::getPheanstalk();
try {
$phean->peek($jobId);
$data = ['status' => 'waiting'];
} catch (ServerException $e) {
$message = $e->getMessage();
if ($message == 'NOT_FOUND: Job ' . $jobId . ' does not exist.') {
$data = ... // here I get from database data for $recordId
} else {
$data = ['status' => 'error'];
}
}
return $data;
}
The question is - is that reliable method to check if job has been completed? I compare here just message I get from exception. I haven't found any other way to check if job has been completed.
问题是 - 检查工作是否已完成的可靠方法吗?我在这里比较只是从异常中得到的消息。我还没有找到任何其他方法来检查工作是否已完成。
回答by Eduardo Cruz
I haven't used Laravel 5 yet, but on Laravel 4, you have the failed jobs table. Where you can see which jobs DIDN'T complete. I'm assuming that L5 might have something or keep the same process. That wouldn't solve your problem?
我还没有使用过 Laravel 5,但在 Laravel 4 上,你有失败的工作表。在哪里可以看到哪些作业没有完成。我假设 L5 可能有一些东西或保持相同的过程。那不能解决你的问题吗?
From my point of view it seems that you are only inverting the perspective. Instead of looking for what failed, you are looking for what worked.
从我的角度来看,您似乎只是在颠倒视角。你不是在寻找失败的东西,而是寻找有效的东西。
回答by Ray
I do not use Laravel 5 yet. But with Laravel 4, when a job is finished, it needs to be manually deleted from the queue. Here is the official documentation:
我还没有使用 Laravel 5。但是在 Laravel 4 中,当一个作业完成时,它需要从队列中手动删除。以下是官方文档:
Deleting A Processed Job: Once you have processed a job, it must be deleted from the queue, which can be done via the delete method on the Job instance
Deleting A Processed Job: Once you have processed a job, it must be deleted from the queue, which can be done via the delete method on the Job instance
It simply means that if you have not use such a method, the job status will remain unfinished. In your case, you can get the queue and see if the job id is there.
这只是意味着如果您还没有使用这种方法,则作业状态将保持未完成状态。在您的情况下,您可以获取队列并查看作业 ID 是否在那里。
回答by Laurence
If you need to trigger a command based upon a job finishing - then it sounds like you should just fire an queued command when the job is complete - then your system can handle the rest?
如果您需要根据作业完成触发命令 - 那么听起来您应该在作业完成时触发一个排队的命令 - 那么您的系统可以处理其余的吗?
Otherwise 'polling' for the job to see if/when it is completed seems cumbersome and inefficient.
否则,“轮询”作业以查看它是否/何时完成似乎既麻烦又低效。
In Laravel 5 - you could event use the new Commands tool that Taylor has provided, and have the ShouldBeQueued
implementation applied - so the command is queued to run at the completion of your other task.
在 Laravel 5 中——你可以使用 Taylor 提供的新命令工具,并ShouldBeQueued
应用实现——所以命令会在你的其他任务完成时排队运行。