Jer*_*auw 10 php console symfony
我有一个Symfony Console命令,它迭代一个可能很大的项目集合,并为每个项目执行任务.由于集合可能很大,因此命令可能需要很长时间才能运行(小时).命令完成后,它会显示一些统计信息.
我想以一种很好的方式中止命令.现在,如果我中止它(即在CLI中使用ctrl + c),则没有统计摘要,也无法输出恢复命令所需的参数.另一个问题是命令可能在处理项目的过程中被终止 - 如果它只能在处理项之间终止,那就更好了.
那么有没有办法告诉命令"尽快中止",或者将ctrl + c命令解释为这样?
我尝试使用该ConsoleEvents::TERMINATE事件,虽然这个处理程序只在命令完成时被触发,而不是当我按ctrl + c时.而且我无法找到有关制作此类可恢复命令的更多信息.
Jer*_*auw 21
这对我有用.您需要pcntl_signal_dispatch在信号处理程序实际执行之前调用.没有它,所有任务都将首先完成.
<?php
use Symfony\Component\Console\Command\Command;
class YourCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output)
{
pcntl_signal(SIGTERM, [$this, 'stopCommand']);
pcntl_signal(SIGINT, [$this, 'stopCommand']);
$this->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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7457 次 |
| 最近记录: |