My application based on symfony2 has to do some standalone jobs at differents moments and differents frequencies. For exemple, sending newsletter, editing report ...
I want to be able to add/edit each task from the application My task would be very close to cron jobs and will consist to call a specific url.
I think about creating a cron job that launch a script every half hour, or ten minutes. this script only do a curl command witch get a docronjob action. This docronjobaction is inside symfony and is responsible to get all the task and launch the one it has to.
It sounds you are storing your schedule of tasks in the database. No problem. For every different type of task (eg. sending newsletter, save reports) create a service, which does the task. Then add to this services a tag (like twig.extension, but your own), so you have something like a TaskChain, which knows all the tasks.
For executing create a console command, which retrieves the schedule from the database, loads the TaskChain, and executes the tasks. This console command can be simply called from a cronjob without exposing it to the web. In fact your are calling this command via the php-cli and not from a browser, the standard time limit is unlimited. No controllers should be involved for executing.
This should be all organized in an extra TaskBundle.
You can use this blog in order to do what you want. You could then use cron or someother program to trigger the action.
In summary:
First of all, you need to create a symfony command that executes the functionality that you wish. For that, create a folder named "Command" in our Bundle at the same level that we have "Controller or Resources" and put a name in our class. The name has to end with "Command" or it won't work. Let's call it "MyCommand".
<?php
namespace Devvness\DevvnessBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class MyCommand extends ContainerAwareCommand
{
protected function configure()
{
$this->setName('devvness:my_command')
->setDescription('Descripción de lo que hace el comando')
->addArgument('my_argument', InputArgument::OPTIONAL, 'Explicamos el significado del argumento');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getContainer()->get('doctrine')->getManager();
// Hacemos lo que sea
$output->writeln('Hola mundo');
$em->flush();
}
}
Then you can call it by executing on cmd or console:
php app/console devness:mycommand
or
php app/console devness:mycommand --my_argument=valor
Then you need to scheduled the task with cron for Unix or something like that by editing the cron file
* * * * * php /root/absolute/for/symfony/project/app/console devness:mycommand --myargument=valor
I had very similar needs (that's why i found this old topic) while working on an application with a lot of commands to manage.
I finally made my own bundle to manage execution and queuing of mutiple symfony command (scheduled tasks stored via Doctrine).
Here is the Git repository : CommandSchedulerBundle
I hope it will help someone.
Best regards
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With