API should be stateless. I think you shouldn't use session here.
Updating the session value didn't allows me to break from a while loop
I have this code below of a while loop that will run indefinitely:
try
{
$this->kafkaConsumer->subscribe(['topic-'.$this->topicKey]);
while (!session('closeConsumer'))
{
$message = $this->kafkaConsumer->consume(10000);
switch ($message->err)
{
case RD_KAFKA_RESP_ERR_NO_ERROR:
$this->sendMessage($message);
// Commit offsets asynchronously
$this->kafkaConsumer->commitAsync($message);
break;
case RD_KAFKA_RESP_ERR__TIMED_OUT:
if (!$this->timedout)
{
$this->processTimedout();
}
break;
default:
$this->processError($message->err, $message->errstr());
break 2;
}
}
}
finally
{
$this->shutDown();
}
I have set the session variable 'closeConsumer' to false then I make a REST API request which will run the first loop, then if I want to break from the loop I will make another REST API which will change the session value of 'closeConsumer' to true:
session(['closeConsumer', true]);
At first, the loop started but when I change the session variable to true it didn't break from the loop, it's like the session variable stays the same.
What is the problem exactly?
NOTE: the first REST API is a blocking one it means it will block in the controller function (I tried different solutions like queues but nothing work so I have no choice)
Besides, both requests would need to be using the same session
besides, session is loaded at the start of the request cycle and stored at the end of the request cycle.
You only have one request cycle. The state cannot change in the middle of it.
Please or to participate in this conversation.