【问题标题】:How to get number of queued jobs in IronMQ using Laravel 5.1?如何使用 Laravel 5.1 在 IronMQ 中获取排队作业的数量?
【发布时间】:2015-10-03 08:13:03
【问题描述】:

在我的项目中使用 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 队列中排队作业(消息)的数量?

【问题讨论】:

    标签: php laravel queue laravel-5.1 ironmq


    【解决方案1】:

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

    但是针对 IronMQ On-Premise API Reference 给我们一个解决方案,它是一个 REST/HTTP API 允许我们使用 javascript 来查询不同的请求来设置/get all what we want from/to queue (Get Queue, Update Queue, List Queues ...) 和从/到每个队列中的消息(Get Message by Id, Get all Messages, Clear Messages ...)。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-21
      • 1970-01-01
      • 2013-12-28
      • 1970-01-01
      相关资源
      最近更新 更多