Aborting and resuming a Symfony Console command

前端 未结 3 1529
無奈伤痛
無奈伤痛 2021-02-02 17:14

I have a Symfony Console command that iterates over a potentially big collection of items and does a task with each of them. Since the collection can be big, the co

3条回答
  •  囚心锁ツ
    2021-02-02 17:34

    This is what worked for me. You need to call pcntl_signal_dispatch before the signal handlers are actually executed. Without it, all tasks will finish first.

    shouldStop = false;
    
            foreach ( $this->tasks as $task )
            {
                pcntl_signal_dispatch();
                if ( $this->shouldStop ) break; 
                $task->execute();
            }
    
            $this->showSomeStats($output);
        }
    
        public function stopCommand()
        {
            $this->shouldStop = true;
        }
    }
    

提交回复
热议问题