在我的项目中使用 Laravel 5.1 实现队列和作业 IronMQ ,我现在可以将作业发送到 IronMQ 队列,如下图所示:
我现在想要的是在我的工作中的句柄函数中获取队列中的当前消息数(红色框中的数字),找到下面的工作代码:
class GetWords extends Job implements SelfHandling, ShouldQueue{
use InteractsWithQueue, SerializesModels;
/**
* Create a new job instance.
*/
public function __construct(Url $url)
{
}
/**
* Execute the job.
*/
public function handle()
{
//getting the name of queue
dd($this->job->getName()); //return 'words'
$currentNumberMsgsInQueue = ?????; //i can't find how
//Condition
if($currentNumberMsgsInQueue == 10){
//Do something
}
}
}
问题是:如何使用 Laravel 获取 IronMQ 队列中排队作业(消息)的数量?
最佳答案
经过几天的搜索,我找到了答案,没有 method/function
在 Laravel 5.1 中,它可以为我们提供 IronMQ 中排队作业的数量。
但反对 IronMQ On-Premise API Reference 给我们一个解决方案,它是一个 REST/HTTP API 允许我们使用 javascript 查询不同的请求,以设置/获取我们想要从/到队列(获取队列、更新队列、列表队列...)和从/到消息的所有内容在每个队列中(通过 Id 获取消息、获取所有消息、清除消息...)。
Base URL :
https://{Host}/{API Version}/projects/{Project_ID}/queues/{Queue_Name}/messages/webhook?oauth={Token}
例如,如果我们想要队列中的消息数量,我们只需 Get Queue Info 并查看 size
从结果。
GET /queues/{Queue Name}
一个实际例子:
您可以在Webhook URL 案例下的相关队列中找到您的第一个基本链接(见下图):
JS代码:
//To get queue info we have url : GET /queues/{Queue Name}
var url = "https://{Host}/{API Version}/projects/{Project_ID}/queues/{Queue_Name}?oauth={Token}";
//Using ajax $.get
$.get( url ,
function( result ) {
alert( "Queue size is :" + result["queue"]["size"]);
});
结果:
{
"queue": {
"project_id": 123,
"name": "my_queue",
"size": 0,
"total_messages": 0,
"message_timeout": 60,
"message_expiration": 604800,
"type": "pull/unicast/multicast",
"push": {
"subscribers": [
{
"name": "subscriber_name",
"url": "http://mysterious-brook-1807.herokuapp.com/ironmq_push_1",
"headers": {
"Content-Type": "application/json"
}
}
],
"retries": 3,
"retries_delay": 60,
"error_queue": "error_queue_name",
"rate_limit": 10
}
}
}
关于php - 如何使用 Laravel 5.1 在 IronMQ 中获取排队作业的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31410897/