Search code examples
laraveldockermqtt

laravel command with mqtt client in background stop without error


I have an laravel application in docker environment. In my application, I have a console command which will connect to a MQTT broker and subscribe to a certain topic, then command will handle message recieved from MQTT broker.

This is my handle() method in command class:

    public function handle()
    {
        $this->logger->info('Start connecting to broker.');
        $subscribeTopic = '#';
        $mqtt = MQTT::connection(MqttClient::MQTT_SUBSCRIBE_CONNECTION);

        // Using a infinite loop for re-creating new connection in case current connection is broken
        while (true) {
            try {
                if (!$mqtt->isConnected()) {
                    $this->logger->info('Client is disconnected. Re-connecting.');
                    $mqtt->connect();
                }
                $mqtt->subscribe($subscribeTopic, $this->handleSubscriberCallback());
                $mqtt->loop(true);
            } catch (Exception $exception) {
                $this->logger->error($exception);
            }
        }
    }

When container started, this process is automatically started in background by command

php artisan mqtt:start > /dev/null &

Checking process is running by command ps aux

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
www          1  0.0  0.0   3832  3016 ?        Ss   02:12   0:00 bash /usr/local/bin/cmd
www         64  0.1  1.0 126336 61000 ?        S    02:12   0:02 php artisan mqtt:start
www         65  0.0  0.5 230460 35488 ?        Ss   02:12   0:00 php-fpm: master process (/usr/local/etc/php-fpm.conf)
www         66  0.3  0.9 308336 57684 ?        S    02:12   0:05 php-fpm: pool www
www         67  0.3  0.7 233172 46008 ?        S    02:12   0:04 php-fpm: pool www
www         75  0.1  0.0   4096  3360 pts/0    Ss   02:34   0:00 bash
www         83  0.0  0.0   6696  2820 pts/0    R+   02:35   0:00 ps aux

This command worked fine, but problem comes when MQTT client (my artisan command) receive messages continuously, process stop without any log, error or warning.

I've tried to publish messages with differences delay each time publish message:

  • With 0.01 second -> process stop after ~4k5 messages
  • With 0.1 second -> process stop after ~9k5 messages

My system:

  • WSL2 in window 11
  • WSL OS: Ubuntu 20.04
  • RAM: 6GB
  • CPU: 4

Can someone know where problem come from? Or how can I automatically restart my process when it stopped. Thanks


Solution

  • Incidentally, review my question and update some information for someone needed.

    Thanks to @Namoshek, using supervisor solved the problem by auto restart command when it stopped.

    Additionally, the supervisor log file for the process helped me debug my error. It's due to an exhausted memory error caused by Laravel Telescope. When I turn off Telescope, this error doesn't happen anymore. Besides, when I start the command when starting the docker container, I use the command php artisan mqtt:start > /dev/null &, which /dev/null removes all output errors. Thus, I could not track error thrown in the docker container log.