3

在我的项目中使用IronMQ在 Laravel 5.1 中实现队列和作业,我现在可以将作业发送到 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 队列中获取排队作业(消息)的数量?

4

1 回答 1

1

经过几天的搜索,我找到了答案,method/functionLaravel 5.1中没有可以为我们提供IronMQ中排队作业的数量。

但是IronMQ On-Premise API Reference给了我们一个解决方案,它是一个REST/HTTP API,它允许我们使用javascript查询不同的请求,以设置/获取我们想要的所有内容(获取队列、更新队列、列出队列。 ..) 以及从/到每个队列中的消息(按 ID 获取消息、获取所有消息、清除消息......)。

基本网址

https://{Host}/{API 版本}/projects/{Project_ID}/queues/{Queue_Name}/messages/webhook?oauth={Token}

例如,如果我们想要队列中的消息数量,我们只需获取队列信息并查看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
    }
  }
}
于 2015-08-11T10:00:44.907 回答