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.

Kristen Joseph-Delaffon
  • 1,261
  • 2
  • 17
  • 37

1 Answers1

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')
Community
  • 1
  • 1
po_taka
  • 1,816
  • 17
  • 27
  • 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. – Kristen Joseph-Delaffon Aug 18 '16 at 14:52
  • `$process->run()` is working but it doesn't execute command in background. – Kristen Joseph-Delaffon Aug 18 '16 at 15:04
  • 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](http://symfony.com/doc/current/components/process.html#running-processes-asynchronously) for implementation. – po_taka Aug 18 '16 at 19:39
  • 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} []` – Kristen Joseph-Delaffon Aug 18 '16 at 19:52
  • 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. – Kristen Joseph-Delaffon Aug 18 '16 at 19:57
  • It looks like some kind of [race condition](https://en.wikipedia.org/wiki/Race_condition). – po_taka Aug 18 '16 at 20:04