Run Symfony2 command in background - symfony

I've a queue of emails for send to customers. I'm spooling emails: http://symfony.com/doc/2.0/cookbook/email/spool.html
Using this command:
php app/console swiftmailer:spool:send --env=prod
The problem is, how to run this command in background?. I mean don't have to execute this command from console whenever I want to send email queues
I solved this using a crontab like explained here: http://blog.servergrove.com/2012/04/27/spooling-emails-with-symfony2-on-vps-and-shared-hosting/
But for me, using crontab not appears the best solution. I read also about RabbitMQ and her bundle for Symfony2 but with this I have to run another command to consume the queue:
./app/console rabbitmq:consumer -m 50 queue_email
What is the best solution for this?

Related

Run Symfony command with Cronjob Kubernetes

Each minute, I have a K8S Cronjob who run Symfony command. My problem is the huge time my pod warmup Symfony cache before execute the command : 56s ...
I search a solution for stock the cache in docker container but i can't execute cache:warmup during docker build, because commande need database and redis up.
Do you have a solution for me ?
Thanks !

How to avoid duplicating workers with Symfony local server?

I found out that I have quite many Symfony local web server workers registered (around ~35), and the number keeps growing. I usually just start server with symfony serve and then kill it (Ctrl + \) when no longer needed. Apparently killing it leaves a worker behind, as seen in symfony server:status. Running symfony serve again just creates a new worker.
symfony server:status output:
Local Web Server
Not Running
Workers
PID 6327: /usr/bin/php7.2 -S 127.0.0.1:43653 -d variables_order=EGPCS /home/mindaugas/.symfony/php/83247c3521c3ac3990bf3f823ef473db0a9445e1-router.php
PID 24596: /usr/bin/php7.2 -S 127.0.0.1:37789 -d variables_order=EGPCS /home/mindaugas/.symfony/php/83247c3521c3ac3990bf3f823ef473db0a9445e1-router.php
PID 6575: /usr/bin/php7.4 -S 127.0.0.1:42505 -d variables_order=EGPCS /home/mindaugas/.symfony/php/83247c3521c3ac3990bf3f823ef473db0a9445e1-router.php
PID 41550: /usr/bin/php7.4 -S 127.0.0.1:36313 -d variables_order=EGPCS /home/mindaugas/.symfony/php/83247c3521c3ac3990bf3f823ef473db0a9445e1-router.php
...
Environment Variables
None
So my questions regarding this:
#1: is it possible to quickly kill the server? I assume symfony server:stop is more correct way, but that requires additional console window and entering the command.
#2: how to kill those workers that are registered from previous sessions? Trying e.g. kill 6327 says that there's no such process. Also they're not gone after system restart.
Those extra workers are bothering me because for each one of them the server log output in the console is duplicated. So right now on each request to the server I get around 3k lines of log output in the console. Which makes it pretty useless.
I have the same problem after upgrading to Symfony CLI version v4.19.0...
My (very) bad workaround:
rm /home/myusername/.symfony/var/83247c3521c3ac3990bf3f823ef473db0a9445e1/*
Edit: this answer is not accurate, as hinted a by #CrSrr's answer above.
The symfony command adds data to both the ./log and ./var directories. Deleting entries in only one of those does not remove the appearance of non-existent workers in the project directory. I was fooled by checking status in a directory where the server:start had never been run.
A bug report is on file with symfony here.
Just faced with a similar issue. The PIDs were not to be found.
PS G:\workspace\joined> symfony server:status
Local Web Server
Not Running
Workers
PID 7732: C:\php\php-cgi.exe -b 63801 -d error_log=C:\Users\George\.symfony\log\e79ad2f4b30a2f0a35c3b5ab08772770b382a3d6.log
PID 19324: C:\php\php-cgi.exe -b 62927 -d error_log=C:\Users\George\.symfony\log\e79ad2f4b30a2f0a35c3b5ab08772770b382a3d6.log
PID 17968: C:\php\php-cgi.exe -b 50197 -d error_log=C:\Users\George\.symfony\log\e79ad2f4b30a2f0a35c3b5ab08772770b382a3d6.log
PID 14040: C:\php\php-cgi.exe -b 55075 -d error_log=C:\Users\George\.symfony\log\e79ad2f4b30a2f0a35c3b5ab08772770b382a3d6.log
Environment Variables
None
In the Windows OS, the log files are kept in %USERPROFILE%.symfony. There's most likely a similar location in your home directory. Deleting all the contents of that directory allowed a new Windows Terminal app to show:
PS G:\workspace> symfony server:status
Local Web Server
Not Running
Workers
No Workers
Environment Variables
None
do symfony serve:stopto stopped the server and do against symfony serve to run the server good luck

How to set timers for console commands in symfony 2.x?

In my app I've got one task that needs to be done every 48 hours on server side. I've created a console command in order to automatize my job. However I don't know how can I set timer to keep invoking that command. Can you point my a way to do that?
You should see on a cron commands.
Cron will run your command every X (frequency) times.
TO create a cron, (on unix) use: crontab -e
For example
0 0 */2 * * bin/console app:command >/dev/null 2>&1
will run every odd days, bin/console app:command
to help you generating a cron
https://crontab-generator.org/

Kill process using Symfony

I am trying to kill a process with pid 38456 using Symfony with this code:
$process1 = new Process('kill -9 38456');
$process1->run();
Pitifully this doesn't work. I think this is due to permissions (Symfony only can kill its own process) but I am not sure about it.
Try to understand which user runs your code:
$process = new Process('whoami');
$process->run();
echo $process->getOutput();
If you are apache or www-data then most probably you have very limited rights.
Your options:
run your script from CLI (command-line) as root. This is the way I recommend
run the process you want to kill from your process
run web-server (apache or php-fpm) as root - very insecure
strange way - web-server can kill process not directly but for example create some file with process id to kill. Also there will be CLI process running by rout that will read this file and kill that process.

Cannot CTRL-C out of rabbitmq consumer console command

We recently upgraded from php 5.4 to 5.5 and we can no longer ctrl-c out of the consumer command. We start/stop the consumer every hour and this no longer works unless we send a SIGKILL which is not ideal.
app/console rabbitmq:consumer ld_sync --env=dev
The command runs fine, it will consume messages, but will end up spiking one of our CPUs to 100%. It cannot be killed with ctrl-c. You must kill -9 it. I'm not even sure where to start debugging this issue. Even if you start it and try to ctrl-c immediately you cannot kill it.
I'd appreciate even a nudge in the right direction for troubleshooting.
The problem can be solved with:
app/console rabbitmq:consumer -w ld_sync --env=dev
For -w option you need to have have enabled pcntl extension.

Resources