I have a message queue in PHP which I use to process tasks, I caused a bug when inserting tasks and it has just inserted a bunch of bools (I usually pass objects through it).
I killed the process which reads the queue because it hung due to items not being removed.
My typical code which has worked fine for quite some time is:
$mqst = msg_stat_queue($mqh);
while ($mqst['msg_qnum']) {
output('Queue is populated', O_DEBUG);
msg_receive($mqh, 0, $msgtype, 2048, $data, true);
$mqst = msg_stat_queue($mqh);
if ($data instanceof \Core\Image) {
$new_tasks[] = $data;
} else {
output('Got some odd data.. ' . print_r($data, true), O_ERROR);
}
}
From my terminal:
$ php -r '$mqh = msg_get_queue(12345, 0666);
$mqst = msg_stat_queue($mqh);
$i = $mqst["msg_qnum"]; while ($i--) {
msg_receive($mqh, 0, $type, 2048, $data, true);
var_dump($type, $data);
}'
Output:
int(0)
bool(false)
int(0)
bool(false)
int(0)
bool(false)
int(0)
bool(false)
int(0)
bool(false)
int(0)
bool(false)
int(0)
bool(false)
I can repeat this indefinitely, they aren’t budging. I’ve tried to just remove the queue but to no success, the documentation states:
msg_remove_queue() destroys the message queue specified by the queue. Only use this function when all processes have finished working with the message queue and you need to release the system resources held by it.
However, I’ve killed the process and it isn’t very specific on releasing the resources. I’m not sure why the messages aren’t being popped off the queue after I receive them. Perhaps I’m missing something simple
EDIT This answer was wrong.
After studying the question more thoroughly, I think the following is the problem: the first message in the queue is larger than 2048 byes, which causes
msg_receiveto return an error. Because of this, the message is never removed from the queue, so it will stay intact.On next calls to the function, it will try to get the same entry, so it will fail again.
My initial suggestion to make the
msg_receivefunction the condition of the while loop would have made this clear immediately. (if it is indeed the solution).Solution: For now, add the
MSG_NOERRORflag to themsg_receivefunction, just to see if the next message is indeed too large, and debug from there.