Symfony call Command from controller as background task - symfony

I want to run a Symfony Console Command from a controller. How to do so is explained in the documentation.
Now my controller waits until the Command is finished. But I want the Command to be executed as a background task (like a cronjob). So the controller doesn't need to wait to complete the Command, only start it.
Is this possible with Symfony Console?

I think you have to use Process component and launch command like that:
$process = new Process('php bin/console your:command');
$process->start();

Related

How Can You Run CRaSH Commands Or A Script On Node Startup?

I need to initialise my Corda nodes by running a few flows to create certain states.
At the moment I am doing it via the CRaSH shell.
e.g.
flow start IOUFlow iouValue: 50, counterparty: Bank1
Is it possible to have the node run a script or some commands on node startup to do this automatically?
If not, how can I write a bash script to automate these CRaSH commands?
Corda 4.4 introduces a new feature to register actions to be performed on node startup.
You could register an action to be performed on node startup using a CordaService.
appServiceHub.register(
AppServiceHub.SERVICE_PRIORITY_NORMAL,
event -> {
// Your custom code to be run on startup.
}
);
You might want to check on the event type to keep it future proof, but currently the ServiceLifecycleEvent just has a single STATE_MACHINE_STARTED enum.

Symfony - background task from form setup

Would you know how to run a background task on Symfony 4, based on the setup of a form ? This would avoid that the user has to remain on the form until the task is finished.
The idea would be that when the form is validated, it starts an independant background task. Then the user can continue its navigation and come back once the task is finished to get the results.
Thanks for your help,
You need to use pattern Message Bus. Symfony has own implementation of this pattern since version 4.1 introducing Messenger Component.
You can see documentation here: https://symfony.com/doc/current/components/messenger.html
To get it work you need some external program that will implement AMQP protocol. Most popular in PHP world IMHO RabbitMQ.
A very simple solution for this could be the following procedure:
Form is valid.
A temporary file is created.
Cronjob gets executed every five minutes and starts a symfony command.
The command checks if the file exists and if it's empty.
If so, the command works of the background task. But before this, the command write it's process id in the file to prevent from beeing excuted a second time.
Remove the file when the command has finished.
As long as the file exists you can show a hint for the user that the task is running.

Calling a console command from a controller using the Process component in Symfony2

I am attempting to call a console command from a controller using the Process component in Symfony2 to have it run in the background, however all it does is hang and end up at a white screen. This is an example of what fails:
$process = new Process('php app/console list');
$process->setWorkingDirectory($this->get('kernel')->getRootDir().'/../');
$process->run();
print $process->getOutput();
I have tried interchanging $process->run() with $process->start() and it still doesn't work.
Checkout AsyncServiceCallBundle, it allows you to execute your application service's methods in background without having to wait until they are finished. Just use it like this:
$this->get('krlove.async')->call('service_id', 'method', [$arg1, $arg2, $arg3]);
It uses this approach to make such calls asynchronous.

Symfony2 calling console command in controller from vendor

I want to use a console command from this bundle within my controller: http://knpbundles.com/dizda/CloudBackupBundle
The developer proposes cronjobs, however I want to use the command to backup my database from within my controller.
How would I do that?
I am getting this error message when i simply try to register this command as a service:
You have requested a non-existent service "backupcommandservice".
Thanks for the help!
commands don't quite work that way. Per the note on http://symfony.com/doc/current/cookbook/console/console_command.html#register-commands-in-the-service-container
registering a command as a service doesn't do much other than control location and dependency injection.
if you want to call a command: http://symfony.com/doc/current/components/console/introduction.html#calling-an-existing-command
that being said you shouldn't call commands from within a controller since you're basically asking to wait for this command to finish executing before you return a response. You'd be better off just sending a request to a queue box (for example beanstalk) and have a worker perform the job.

Symfony Process run as who?

When we using Symfony\Component\Process\Process, the command run as who?
I tried the command whoami through Process, but it return void???
$return = exec('whoami');
echo $return."\n"; // return [myname]
$process = new Process('whoami'); // The symfony process
echo $process->getOutput(); // return nothing #_#
Yes, it runs as user you run this command or the user of your webserver.
Your code seems a bit incomplete. I suggest adding $process->run(); before trying to get an output.
I almost guarantee that that Process runs as whatever user your webserver is running as. If you're running apache for instance, try running:
ps aux | egrep '(apache|httpd)'
In your terminal to discover which user apache is running as. My money would be on either apache or httpd as the user which Process runs under. Hope that helps.
From the documentation is better use start() instead run() if you want to create a background process. The process_max_time could kill your process if you create it with run()
"Instead of using run() to execute a process, you can start() it: run() is blocking and waits for the process to finish, start() creates a background process."

Resources