0

I want to import data from LinkedIn and save them in my database. I want this run at background.

So I create a console command. But how could I call a controller/action in a console command so that the import and save transaction could run at the console command at background?

luchaninov
  • 6,792
  • 6
  • 60
  • 75
史京迪
  • 481
  • 2
  • 6
  • 19

2 Answers2

4

I don't know what the structure of your application is, but if you want to allow a command to run something that a controller does, then the typical way to do that is by having the intended code in a service that both controller and command has access to.

By using a ContainerAwareCommand, you give the command instance access to the service container, and thusly the service. Controllers by default have access to the service container.

Try to avoid jury rigging parts of your application together when they are designed to be separate. Give them access to the same services, but keep them apart.

Flosculus
  • 6,880
  • 3
  • 18
  • 42
  • Thx. But how could i make the console command run in background? For example, when a user login, it start running the console command but it will not affect user to see other pages. – 史京迪 Aug 05 '13 at 16:33
  • You want it to run repeatedly? Normally such tasks are added to a cron job. I think you are approaching this the wrong way, if it is meant to be trigger via controller action, then it shouldn't need to be a command. – Flosculus Aug 05 '13 at 16:51
  • 1
    I confirm with Flosculus by doing this task per cron job. There you can easily call your command line script at any time you need it. You can also use an Event which get's triggered after login and that call the necessary function you need. – sensi Aug 05 '13 at 16:54
  • Maybe the SonataNotificationBundle is useful – Emii Khaos Aug 05 '13 at 17:07
  • Thx for your answer. You understand me in a right way. What you mean is i shouldn't use a console command? How could i make a cron job with symfony2? Or i just use the basic php cron job? I'm a rookie in php and symfony2. Thank you for your patience @sensi – 史京迪 Aug 06 '13 at 08:43
  • The cron job is a bit of a cruel solution, because not all hosting providers give you that feature. However, if something needs to be triggered when a user logs in, then as sensi said, events are a good place. – Flosculus Aug 06 '13 at 08:47
  • Here is a good example: http://dev.dbl-a.com/symfony-2-0/how-to-add-a-symfony2-login-event-listener/ – Flosculus Aug 06 '13 at 08:48
  • SonataNoficaionBundle I would not recommend, because it's a bit complicated to implement it. But as Flosculus said not every hosting provider let you create cronjobs. Do you have a Root or Vserver? Then it's pretty simple to add a crontab with the crontab -e command. See http://www.openjs.com/scripts/jslibrary/demos/crontab.php . Then you have to ways do execute the script: 1. Clean and preffered solution: call a local php script like php app/console your:command:execute 2. Or you do an request with curl do an given url (but notice the authentication can be a tricky :)) – sensi Aug 06 '13 at 09:33
0

Thanks to Flosculus and sensi for helping me a lot. In order to reach my goal, I first add a event listener when login.http://dev.dbl-a.com/symfony-2-0/how-to-add-a-symfony2-login-event-listener/ Then I add a process to make it work in background http://symfony.com/doc/master/components/process.html

史京迪
  • 481
  • 2
  • 6
  • 19