Two Questions: How to Run RabbitMQ as a Cron and Why has my queue stopped queuing?

1,348 views
Skip to first unread message

jhayesgo...@gmail.com

unread,
Apr 26, 2018, 12:34:25 PM4/26/18
to rabbitmq-users

Hello, I have a two questions about Rabbit MQ. The first one is an implementation of the product question, and the the original reason for joining this group. The second one is a general troubleshooting question and I thought I'd ask while I had the attention of some experts.


Question 1: What I am attempting to do is fill a queue with messages that accumulate over time. Every hour, I want to awaken the receiver via a cron job and clear out the queue, and shut the receiver back down. The reason is I do not want to be responsible for the service going down, and as a way to ensure the service keeps running. I haven't found a solution to this yet. How can I accomplish this?


Question 2: As I'm testing this morning, I send a batch of 45 messages to the queue with a script. The messages can be seen in the queue dashboard. I run a modified receive.php script from the tutorials which creates an object out of the message and saves it to a database. The messages are cleared out of the queue and the records are updated in the database. I exit the receive.php file, and run the create messages script again. I see activity in the queue but nothing is being queued. The row count in the database remains the same. What is going on?

receive.php

<?php

require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
include 'classes/activity_record.php';
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();

$channel->queue_declare('hello', false, false, false, false);

echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";

$callback = function($msg) {
  echo " [x] Received ", $msg->body, "\n";

  create_record_object($msg);

};

$channel->basic_consume('hello', '', false, true, false, false, $callback);

while(count($channel->callbacks)) {
  $channel->wait();
}

$channel->close();
$connection->close();


function create_record_object($msg){

$data = json_decode($msg->body);
$activity_record = new ActivityRecord($data->uid, $data->nid, $data->gid, 
  $data->rr, $data->type, $data->timespan, 
  $data->timestamp);

$activity_record->save();

}



example record creation:
function AMQP_message_send($json_string) {

$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare('hello', false, false, false, false);
$msg = new AMQPMessage($json_string);
$channel->basic_publish($msg, '', 'hello');

echo "Message Sent.";

$channel->close();
$connection->close();
}

Michael Klishin

unread,
Apr 26, 2018, 1:04:03 PM4/26/18
to rabbitm...@googlegroups.com
1.Your question is basically how to run a PHP program using cron. This is outside of the scope of this list. I don't think there's anything
   RabbitMQ-specific.


2. See server logs for signs of alarms, lost/abruptly closed client TCP connections and so on. We do not guess on this list, we collect data instead.
    based on the amount of information we have. Management UI provides a reasonable number of metrics that demonstrates ingress data rate on connections,
    message rates and whether there are alarms (see Overview or individual node pages) in effect that will block publishers.

--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
MK

Staff Software Engineer, Pivotal/RabbitMQ

jhayesgo...@gmail.com

unread,
Apr 26, 2018, 1:13:18 PM4/26/18
to rabbitmq-users
For Question 1: The question is how do I stop the Receiver from running. The following code on the receiver is an infinite loop. 


while(count($channel->callbacks)) {
  $channel->wait();
}

To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-user...@googlegroups.com.
To post to this group, send email to rabbitm...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Michael Klishin

unread,
Apr 26, 2018, 1:26:39 PM4/26/18
to rabbitm...@googlegroups.com
Add a condition that will check for a value e.g. set by a signal handler. Another
common approach is to send a service a message that will make it shut down (known as
"poison messages").

To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

jhayesgo...@gmail.com

unread,
Apr 26, 2018, 1:58:09 PM4/26/18
to rabbitmq-users
Thanks for the reply. I'll work with what you've described.
Reply all
Reply to author
Forward
0 new messages