How to get the number of jobs in the queue in IronMQ using Laravel 5.1?

Implementing queues and jobs in Laravel 5.1 in my project using IronMQ , now I can submit jobs to the IronMQ queue as you can see in the image below:

enter image description here

Now I want to get the current number of messages in the queue (number in red field) in the handle function in my work, find the following job code:

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

      

Q: How can I get the number of queued jobs (messages) in an IronMQ queue using Laravel?

+3


source to share


1 answer


After days of searching, I found an answer, not method/function

in Laravel 5.1 , that can give us the number of jobs in the queue in IronMQ .

But vs. IronMQ On-Premise API Reference gives us a solution, it is a REST / HTTP API that allows us to request different requests using javascript to set / get whatever we want from a queue / queue (Get queue, update queue, queue queues ...) and from / to messages in each 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}

For example, if we need the number of messages in the queue, we only have Get queue information and view from the result. size

GET /queues/{Queue Name}

      


Practical example:



You can find your first base link inside the corresponding queue in your project at the Webhook URL (see picture below):

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

      

+1


source







All Articles