如何使用Laravel 5.1获取IronMQ中排队作业的数量?

3
在我的项目中使用IronMQ实现Laravel 5.1中的队列和作业,我现在可以像下面图片中看到的那样将作业发送到IronMQ队列:

enter image description here

我现在想要的是在我的作业的处理函数中获取队列中当前消息数量(红框中的数字),以下是作业代码:

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队列中排队作业(消息)的数量?

1个回答

1
经过多日的搜索,我找到了答案,Laravel 5.1中没有可以给出IronMQ队列中排队作业数量的method/function
但是根据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}

例如,如果我们想要获取队列中的消息数量,我们只需要获取队列信息,然后从结果中查看size即可。
GET /queues/{Queue Name}

一个实际例子:

您可以在项目的相关队列中找到第一个基础链接,在Webhook URL案例下(见下图):

enter image description here

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
    }
  }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接