dongxianghuan3587 2015-07-14 15:26
浏览 95
已采纳

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

Implementing queues & jobs in Laravel 5.1 in my project using IronMQ, I can now send jobs to the IronMQ queue like you see in image bellow :

enter image description here

What I want now is to get the current number of messages in queue (number in red box) in the handle function in my job, find job code bellow :

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

Question is : How to get number of queued jobs (messages) in IronMQ queue using Laravel ?

  • 写回答

1条回答 默认 最新

  • doujie1917 2015-08-11 10:00
    关注

    After days of searching I found the answer, there's no method/function in Laravel 5.1 that can give us the number of queued jobs in IronMQ.

    But against IronMQ On-Premise API Reference give us a solution, it's a REST/HTTP API that allow us to query different requests using javascript to set/get all what we want from/to queue (Get Queue, Update Queue, List Queues ...) and from/to messages in every queue (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}

    Example, if we want the number of messages in queue, we have just to Get Queue Info and peek the size from result.

    GET /queues/{Queue Name}
    

    A Practical Example :

    You can find your first base link inside the concerned queue in your project under Webhook URL case (see picture bellow) :

    enter image description here

    JS code :

    //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"]);
    });
    

    Result :

    {
      "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
        }
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)