jced-artem/singleton-command

Symfony 单例命令。在之前的进程停止之前,不能并行运行的命令。

1.0.2 2016-12-09 14:39 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:19:55 UTC


README

Symfony 单例命令。在之前的进程停止之前,不能并行运行的命令。如果你只需要在单个进程中运行 symfony 命令,并且不想自己管理和使用锁文件,那么这个类就是你所需要的 :)

需求

  1. symfony/console
  2. jced-artem/lock-service - 使用此包替换 LockHandler,因为它不能与多个主机一起工作。在这里这是重要的,因为命令可以在多个服务器上作为 cron 在共享文件夹中启动。

安装

composer require jced-artem/singleton-command

示例

class JobCommand extends SingletonCommand implements SingletonCommandInterface
{
    protected function configure()
    {
        $this
            ->setName('cron:job')
            ->addArgument('someArgument', InputArgument::REQUIRED)
        ;
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     */
    protected function beforeLock(InputInterface $input, OutputInterface $output)
    {
        // You can create dynamic lock-names if you need. If don't - just remove this method.
        $this->setLockName($this->getName() . ':' . $input->getArgument('someArgument'));
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return bool
     */
    public function lockExecute(InputInterface $input, OutputInterface $output)
    {
        // command code here
    }
}

并且你需要将此命令用作服务,因此,你的 services.yml 应该看起来像这样

parameters:
    lock_path: '/path/to/shared/folder/locks'

services:
    lock_service:
        class: AppBundle\Service\LockService
        arguments: ['%lock_path%']
        shared: false # shouldn't be shared if you want to have commands with different lock paths.
    command.cron_job:
        class: AppBundle\Command\JobCommand
        arguments: ['@lock_service']
        tags:
            - { name: console.command }