0

Using a queue system for handling jobs. We have a cron run once every morning at 1am and it publishes all of our required API calls. We have another consumer cron that runs every five minutes that addresses the requests in the queue.

We are in a shared environment and scripts called via cron are terminated after 15 minutes. Therefore to protect ourselves we gracefully terminate our script after 9 minutes.

Nearly all of our API calls are running with the exception of (2), which we are trying to figure out why. When we run them manually via the same script they work perfectly. Strange. Only fail when run via cron with all of the other requests.

In any case the nightly logs show the following Fatal Error occurring multiple times. Maybe this is the culprit. Unable to determine what is causing this. If I manually kill the AMQP connections I get a different error so I know $ch and $conn are not being overridden somewhere in one of the API calls.

Fatal error: Call to a member function basic_get() on resource in /........../consume_requests.php on line 116

Below is our initialization of phpamqplib. Any help would be appreciated.

require '/vendors/php-amqplib/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

try {
    $queue = 'my_queue';
    $url = parse_url(URL);
    $conn = new AMQPStreamConnection(HOST, 5555, USER, PASS, PATH, 1));
    $ch = $conn->channel();
    $ch->queue_declare($queue, false, true, false, false);
    $ch->exchange_declare(EXCHANGE, 'direct', true, true, false);
    $ch->queue_bind($queue, EXCHANGE,$queue);
} catch(Exception $e) {
    echo $e->getMessage();
}


while($message_receipt = $ch->basic_get($queue)) {  //line 116

1 Answer 1

0

Since the code you show is not in any function, the variables are all global. Any other global code can easily overwrite these variables, particularly with an abbreviated name like $ch.

My guess is that inside the code, you are setting up a Curl connection, and calling the "curl handle" $ch, which accidentally overwrites the existing variable. (It could be anything else, but this is a common variable name, and would give you a resource.) Then next time the while loop checks its condition, it sees this new value of $ch, rather than the "channel" that you intended.

The fix is basically to pay more attention to code style:

  • Use more descriptive variable names which are less likely to collide.
  • Break long blocks of code into functions so you can see them all in one go, and take advantage of local variables.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.