简体   繁体   中英

Symfony cron job, strange error

I have this symfony command that runs well locally and on the server. Locally, it works if I call it or if it goes through cron.

On the server side, it works if I call it on the shell window, but the moment cron calls it, it doesn't wanna work and throws me this strange error.

the command is php ~/mg/app/console global:insert 1 -vv --env=prod

And the error is

ContextErrorException in ArgvInput.php line 287: Warning: Invalid argument supplied for foreach()

in ArgvInput.php line 287
at ErrorHandler->handleError('2', 'Invalid argument supplied for foreach()', 'vendor/symfony/symfony/src/Symfony/Component/Console/Input/ArgvInput.php', '287', array('values' => array('--ansi'))) in ArgvInput.php line 287
at ArgvInput->hasParameterOption(array('--ansi')) in Application.php line 823
at Application->configureIO(object(ArgvInput), object(ConsoleOutput)) in Application.php line 123
at Application->run(object(ArgvInput)) in console line 26

Your help would be greatly appreciated, first time I ever see this error message.

Command code

class GlobalInsertCommand extends ContainerAwareCommand
{
protected function configure()
{
    $this
        ->setName('global:insert')
        ->addArgument('env', InputArgument::OPTIONAL, '1 | 0',0);
}

protected function execute(InputInterface $input, OutputInterface $output) {

    $rootDir = $this->getContainer()->get('kernel')->getRootDir();

    foreach (MyRepos::$SOURCES as $source):
        if($input->getArgument("env") == 1):
            $source .= " --env=prod";
        endif;
        MyRepos::runProcess("php $rootDir/console immobilier:insert $source -vv");
    endforeach;
}

}

The actual insertCommand

class InsertCommand extends ContainerAwareCommand {


protected function configure()
{
    $this
        ->setName('immobilier:insert')
        ->setDescription('Take RawData and and insert into database')
        ->addArgument('source', InputArgument::REQUIRED, 'all')
    ;
}

protected function execute(InputInterface $input, OutputInterface $output) {
    $this->pc = $this->getContainer()->get('Momoa.immobilier_cron.controller');
    try {
        $this->pc->setChoice($input->getArgument("source"));
        $this->pc->addAction();
    } catch(Exception $e){
          $this->logger->error($e->getMessage());
    }
}

}

And the Run Process method

    static function runProcess($command){
    $ps = new Process($command);
    $ps->enableOutput();
    $ps->setTimeout(null);
    $ps->run(function($type, $buffer) {
        echo "OUT > " . $buffer;
    });
}

So I found the problem, The error is not related to the actual logic of the command file but the php.ini options.

First you need to be sure that this option is set to on register_argc_argv=on in your php.ini file.

If you can't change your php.ini file, declare the command as such:

php-cli -d register_argc_argv=On /path/to/your/command

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM