How to keep Laravel Queue system running on server

后端 未结 17 1036
小鲜肉
小鲜肉 2020-11-28 02:23

I recently setup a Laravel Queue system. The basics are a cronjob calls a command which adds jobs to a queue and calls a second command which sends an email.

The sy

17条回答
  •  野性不改
    2020-11-28 03:18

    From https://gist.github.com/ivanvermeyen/b72061c5d70c61e86875

    isQueueListenerRunning()) {
                $this->comment('Queue listener is being started.');
                $pid = $this->startQueueListener();
                $this->saveQueueListenerPID($pid);
            }
    
            $this->comment('Queue listener is running.');
        }
    
        /**
         * Check if the queue listener is running.
         *
         * @return bool
         */
        private function isQueueListenerRunning()
        {
            if ( ! $pid = $this->getLastQueueListenerPID()) {
                return false;
            }
    
            $process = exec("ps -p $pid -opid=,cmd=");
            //$processIsQueueListener = str_contains($process, 'queue:listen'); // 5.1
            $processIsQueueListener = ! empty($process); // 5.6 - see comments
    
            return $processIsQueueListener;
        }
    
        /**
         * Get any existing queue listener PID.
         *
         * @return bool|string
         */
        private function getLastQueueListenerPID()
        {
            if ( ! file_exists(__DIR__ . '/queue.pid')) {
                return false;
            }
    
            return file_get_contents(__DIR__ . '/queue.pid');
        }
    
        /**
         * Save the queue listener PID to a file.
         *
         * @param $pid
         *
         * @return void
         */
        private function saveQueueListenerPID($pid)
        {
            file_put_contents(__DIR__ . '/queue.pid', $pid);
        }
    
        /**
         * Start the queue listener.
         *
         * @return int
         */
        private function startQueueListener()
        {
            //$command = 'php-cli ' . base_path() . '/artisan queue:listen --timeout=60 --sleep=5 --tries=3 > /dev/null & echo $!'; // 5.1
            $command = 'php-cli ' . base_path() . '/artisan queue:work --timeout=60 --sleep=5 --tries=3 > /dev/null & echo $!'; // 5.6 - see comments
            $pid = exec($command);
    
            return $pid;
        }
    }
    

提交回复
热议问题