2

I need to run a command asynchronously. To do this, I'm trying to use Process Component.

The command I'm trying to start is calling a function that needs somes parameters. These parameters are given by Controller that launches the Process.

Problem is I don't know how to pass parameters to my command with Process Component.

Command :

protected function configure()
{
    $this
        ->setName('generationpdf:classement')
        ->setDescription('Génère le PDF d\'un classement.');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
    ini_set('memory_limit','4000M');
    $pdf = $this->imprimerAction();
    $this->sendClassement($pdf);
    $output->writeln('PDF envoyé.');
}

Controller :

        $command = new PDFClassementCommand();
        $input = new ArrayInput(array('id' => $id, 'errata' => $errata, 'precision' => $precision, 'group' => $group, 'logoAnnee' => $logoAnnee));
        $output = new NullOutput();

        $process = new Process($command);
        $process->disableOutput();
        $process->start();

Parameters I need to use are in ArrayInput but Process doesn't take array argument.

1 Answer 1

2

The process is expecting first argument to be string.

Example:

$process = new Process('ls');

To run command you will need to execute symfony console command

$process = new Process('/var/www/my-project/bin/console generationpdf:classement')

You don't have to hardcode the path, see how-to-get-the-root-dir.

You can pass parameters normally, like running command from cli

$process = new Process('/var/www/my-project/bin/console generationpdf:classement --id=5 --errata=someValue')
Sign up to request clarification or add additional context in comments.

6 Comments

I did what you said and my command is running as intended in terminal. I tried with Process but it doesn't work. $rootDir = $this->get('kernel')->getRootDir(); $process = new Process('php '.$rootDir.'/bin/console generationpdf:classement '.$id.' '.$group.' '.$logoAnnee.' '.$errata.' '.$precision.''); $process->start(); Process is started but is not successful.
$process->run() is working but it doesn't execute command in background.
Could you wait the process to finish, using start() and then check the output and error output. There should be error output for failed process, right ? See docs for implementation.
I've just seen in logs these lines : [2016-08-18 19:45:50] php.DEBUG: unlink(C:\WINDOWS\TEMP\sf_proc_00.out): Permission denied {"type":2,"file":"\\vendor\\symfony\\symfony\\src\\Symfony\\Component\\Process\\Pipes\\WindowsPipes.php","line":190,"level":28928} [] [2016-08-18 19:45:50] php.DEBUG: unlink(C:\WINDOWS\TEMP\sf_proc_00.err): Permission denied {"type":2,"file":"\\vendor\\symfony\\symfony\\src\\Symfony\\Component\\Process\\Pipes\\WindowsPipes.php","line":190,"level":28928} []
When I add these lines : $process->start(); $process->wait() It's also working but it's blocking main process. Look like it doesn't want to get to background due to some permission but I don't know how to solve this.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.