大佬教程收集整理的这篇文章主要介绍了php-Symfony锁定组件未锁定-如何解决?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我最近升级到了Symfony 3.4.x,由于弃用警告而重构LockHandler并陷入奇怪的行为.
重构前命令中的代码:
class FooCommand
{
protected function configure() { /* ... does not matter ... */ }
protected function lock() : bool
{
$resource = $this->getName();
$lock = new \Symfony\Component\Filesystem\LockHandler($resource);
return $lock->lock();
}
protected function execute()
{
if (!$this->lock()) return 0;
// Execute some task
}
}
而且它可以防止同时运行两个命令-秒只是完成而不做任何工作.那很好.
但是在建议重构之后,它允许同时运行许多命令.这是失败的.如何防止执行?新代码:
class FooCommand
{
protected function configure() { /* ... does not matter ... */ }
protected function lock() : bool
{
$resource = $this->getName();
$store = new \Symfony\Component\Lock\FlockStore(sys_get_temp_dir());
$factory = new \Symfony\Component\Lock\Factory($store);
$lock = $factory->createLock($resource);
return $lock->acquire();
}
protected function execute()
{
if (!$this->lock()) return 0;
// Execute some task
}
}
注意#1:我不在乎很多服务器,仅在一个应用程序实例上.
注意2:如果进程被杀死,则新命令必须解锁并运行.
解决方法:
您必须使用LockableTrait特性
use Symfony\Component\Console\Command\LockableTrait;
use Symfony\Component\Console\Command\Command
class FooCommand extends Command
{
use LockableTrait;
.....
protected function execute(InputInterface $input, OutputInterface $output)
{
if (!$this->lock()) {
$output->writeln('The command is already running in another process.');
return 0;
}
// If you prefer to wait until the lock is released, use this:
// $this->lock(null, true);
// ...
// if not released explicitly, Symfony releases the lock
// automatically when the execution of the command ends
$this->release();
}
以上是大佬教程为你收集整理的php-Symfony锁定组件未锁定-如何解决?全部内容,希望文章能够帮你解决php-Symfony锁定组件未锁定-如何解决?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。