在另一个命令中运行的Symfony命令不起作用

时间:2018-02-13 00:06:23

标签: php bash symfony cron symfony-console

Hellow,我需要一些关于在另一个命令中执行symfony命令的帮助。我并不陌生,我创建了许多命令,并从命令,控制器内部运行它们,它始终有效。但是这个我不明白为什么它不像其他人一样工作。我一直在运行一个命令,并且当有很多工作要为这个工人提供一些帮助时,我不时创建了一些额外的工作人员(一次检查选项)。

我创建了使用此类运行beanstalk worker的命令:

<?php

namespace AppBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Command\LockableTrait;

/**
 * Class BeanstalkWorkerCommand 
 * Command start the Beanstalk worker. Get the job from queue and preform job
 *
 * @method configure()
 * @method execute(InputInterface $input, OutputInterface $output)
 * @method authenticateUser(InputInterface $input, OutputInterface $output)
 * @method checkAuthentication($username, $password)
 */
class BeanstalkWorkerCommand extends ContainerAwareCommand
{
use LockableTrait;

protected function configure()
{
    $this
        ->setName('beanstalk:worker:start')
        ->setDescription('Start the Beanstalk infinitive worker. Get the job from queue and preform job')
        ->addOption(
            'one-check',
            'o',
            InputOption::VALUE_NONE,
            'If set, the worker will check tubes only once and died if there is no jobs in queue'
        )
    ;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
    $output->writeln("\n<info>Beanstalk worker service started</info>");
    $tubes = $this->getContainer()->get('app.job_manager.power_plant')->getTubes();

    if ($input->getOption('one-check')) {
        // run once
        foreach ($tubes as $tubeName) {
            if ($tubeName != "default") {
                $this->getContainer()->get('app.queue_manager')->fetchQueue($tubeName);
            }
        }
        $output->writeln("\n<info>Beanstalk worker completed check and stoped</info>");
    } else {
        // run forever
        set_time_limit(0);
        ini_set('xdebug.max_nesting_level', 1000);
        if (!$this->lock()) {
            $output->writeln('The command is already running in another process.');

            return 0;
        }
        while (1) {
            foreach ($tubes as $tubeName) {
                if ($tubeName != "default") {
                    $this->getContainer()->get('app.queue_manager')->fetchQueue($tubeName);
                }
                sleep(0.1);
            }
        }
        $output->writeln("\n<error>Beanstalk worker service has stoped</error>");
    }
}
}

我运行另一个命令来创建一些具有此功能的额外工作人员:

public function startExtraWorkers(OutputInterface $output)
{
    $numOfExtraWorkers = $this->getContainer()->getParameter('num_of_extra_beanstalk_workers');

    for ($i=0; $i < $numOfExtraWorkers; $i++) {
        $payload = [
            "--one-check"   =>   TRUE
        ];
        $this->getContainer()->get('app.job_manager.queue')->createAndSendToQueue('beanstalk:worker:start', $payload, 10);
    }
    $output->writeln("\n<question>".$numOfExtraWorkers." extra benastalk workers started!</question>");

    return TRUE;
}

public function createAndSendToQueue($command, $payload, $priority = 65536)
{
    $jobData = $this->createJob($command, $payload);

    return $this->job->enqueue($command, $jobData, $priority);
}

public function enqueue($job, array $args, $priority = 65536, $delay = 0, $ttr = 120)
{
    if (!preg_match('/[a-z0-9\.]+/i', $job)) {
        throw new InvalidArgumentException("Invalid job name");
    }
    $args = json_encode($args);

    return $this->pheanstalk->put($args, $priority, $delay, $ttr);
}

问题是,如果我从终端运行此命令或使用cron作业运行它,但是如果我使用此函数运行它,则它不起作用。我看到该命令已被执行但由于某些未知原因它无效。 如果我执行了这个命令,我可以看到所有命令都已被执行,如果我从终端或cron作业运行相同的命令,他们就不会执行工作:

ps ax |grep "beanstalk:worker:start --one-check"

输出(第一个是从这个函数运行的,第二个是用cron作业运行的。只有第二个有效):

31934 ?        Ss     0:00 /bin/sh -c /usr/bin/php /var/www/mose-base/bin/console beanstalk:worker:start --one-check
31935 ?        S      0:00 /usr/bin/php /var/www/mose-base/bin/console beanstalk:worker:start --one-check

任何人都可以给我一些建议,为什么这不像其他命令那样工作?如果我用cron作业或终端内部运行它,为什么同样的命令运行正常?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

尝试使用:

/bin/sh -c '/usr/bin/php /var/www/mose-base/bin/console beanstalk:worker:start --one-check'

记住引号。