我尝试在Symfony2:中制作控制台命令
class UpdateCommentsCommand extends ContainerAwareCommand
{
protected function configure()
{
$this->setName('comments:update')
->setDescription('Update comments');
->addArgument('force', InputArgument::OPTIONAL, 'force', false)
}
protected function execute(InputInterface $input, OutputInterface $output)
{
if (!$input->getArgument('force') && if $flag_this_script_is_active) {
return;
}
$flag_this_script_is_active = True
$em = $this->getContainer()->get('doctrine')->getEntityManager();
// Bad comments
$update_commang = $em->createQuery("
UPDATE MyBundle:CommentsAll AS c
SET
c.is_automoderated = 1
WHERE
bla_bla_bla LIMIT 100
");
$result = $update_commang->getResult();
if ($result > 0) {
//Re-run this script with --force argument
} else {
$flag_this_script_is_active = false
}
}
}
该脚本将由crone每60秒运行一次,并继续运行,直到所有通信网络都将更改为is_automoded=1
若上一个脚本并没有结束,下一个脚本就会看到并没有运行。
现在我想知道如何设置标志$flag_this_script_is_active
。我想创建一些文件,并在脚本结束后将其删除。但也许还有更好的方法。也许是一些符号函数?
您可以为此目的使用bash脚本。使用ps aux | grep 'comments:update'
查找已在运行的命令,如果未找到,则运行该命令。我们在以前的项目中使用了这种方法来检测守护进程是否正在执行。