Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best practice how to schedule symfony2 action [closed]

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.

From my point of view :

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.

My questions are :

  • Is there a cleaner way to achive that ?
  • Inside the docronaction how do I launch others action ?
  • How do I manage to set the time limit to 0 for an entire controller ?
  • Does a bundle doing this allready exist ?
like image 799
0x1gene Avatar asked May 13 '13 09:05

0x1gene


Video Answer


3 Answers

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.

like image 159
Emii Khaos Avatar answered Oct 18 '22 20:10

Emii Khaos


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
like image 35
Rodolfo Velasco Avatar answered Oct 18 '22 20:10

Rodolfo Velasco


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

like image 3
j-guyon Avatar answered Oct 18 '22 18:10

j-guyon