【发布时间】:2014-12-08 09:54:05
【问题描述】:
我尝试在 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 秒运行一次,直到所有 commnets 都更改为 is_automoderated = 1
如果上一个脚本没有结束,下一个脚本会看到并且没有运行。
现在我想知道如何设置标志$flag_this_script_is_active。我想创建一些文件,并在脚本结束后将其删除。但也许有更好的方法。也许一些 symfony 函数?
【问题讨论】: