Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Quourtige's avatar

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)

0 likes
4 replies
Quourtige's avatar

Yes, I know that but I have been looking for a solution to a problem for 7 days because I have an indefinitely running process and I can't use queues (I tried they didn't work for my case) nor using the async symphony process, nothing work at the end I understand that Laravel is not good at handling indefinitely running process but I can't switch now, if this didn't work then I will just use the solution which is to run a counter and when the consumer Timedout for a value like 10 times it will close the consumer.

Snapey's avatar
Snapey
Best Answer
Level 122

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.

1 like
Quourtige's avatar

Thank you, at least I know where is the problem now.

Please or to participate in this conversation.