PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, February 6, 2022

[FIXED] Running a background task using Symfony Process without having to wait for the process to finish

 February 06, 2022     php, symfony     No comments   

Issue

After a user submits a form, I want to render a view file, and right after that I want to initiate a background task to process five MS Excel files (each may have up to 2000 rows) , but in way so that the user don't have to wait for the process to finish in order to view the page. After the task is finished I will inform the user through an email.

I am using Symfony Framework 3. I have included my code below. Its not doing what I am trying to achieve. After submitting the form the page loads only when the entire background task is complete.

I am not sure but after googling a lot, I think The kernel.terminate Event could be useful here. But I can't seem to understand how to work with it.

Could you please tell me how to solve this problem ?

Here's my code:

I have created a Console Command:

class GreetCommand extends ContainerAwareCommand {

  protected function configure()
  {
    $this->setName('hello:world');
  }

  protected function execute(InputInterface $input, OutputInterface $output)
  {
   // My code to execute
  }

}

And in my controller I have

$process = new Process('ls -lsa');
$process->disableOutput();
$that = $this;

$process->start(function ($type, $buffer) use ($that) {
        $command = new GreetCommand();
        $command->setContainer($this->container);
        $input = new ArrayInput(array());
        $output = new NullOutput;
        $resultCode = $command->run($input, $output);

 });

 return $this->render('success.html.php',  array('msg' => 'Registraion Successful'));

Update

I have solved the problem using the connection-handling feature of PHP.

Thanks to this post


Solution

Running Processes Asynchronously

You can also start the subprocess and then let it run asynchronously, retrieving output and the status in your main process whenever you need it. Use the start() method to start an asynchronous process

documentation

so, to start your command asynchronously you should create new process with command and start it

$process = new Process('php bin/console hello:word');
$process->start();

Consider to change this to full paths like \usr\bin\php \var\www\html\bin\console hello:word

Also there is good bundle cocur/background-process you may use it, or at least read the docs to find out how it works.



Answered By - Denis Alimov
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing