0

Let's imagine this:

  1. I have to download an XML document from an URL;
  2. I have to elaborate this document and persist its information in the database, creating or updating a lot of entites.

I think the best way is to use queues. Or maybe I can also use cronjobs.

My problem is this: if I use the same app to do the heavy tasks and also to show to the end user the results of those heavy tasks, it may happen that the heavy tasks slow down the main website.

Take a more concrete example from real life: Google Search Console (or whatever other app that does heavy tasks and shows results to the end user).

Google Search Console gets the XML map, then starts downloading each webpage, on each webpage performs a lot of analysis, then saves the results to a database and so the end user can see the errors of his website and other useful information.

So, put as hypothesis I want to build again Google Search Console as a Symfony app, which are the possible approaches?

I think that for sure I have to use queues, but the app that downloads the webpages and the app that processes these webpages and the public frontend that shows the result of these operations are the same app or are two or three separate apps?

That is, have I to create a unique application that does all these things, or I create an app to download webpages, one other to process these webpages and one other to show to the user the results?

I'm thinking a lot at this, and I'm not able to find a good design to follow.

Because my istinct is to create multiple apps for each of those tasks to make the workings but it seems that create multiple Symfony apps isn't a good choice: Symfony 2 multiple apps?

So I really don't know which path to follow: multiple apps or one big app? And if I use one big app, should have I to use cronjobs or I have anyway use queues?

Community
  • 1
  • 1
Aerendir
  • 6,152
  • 9
  • 55
  • 108
  • I have made a similar app on my job. Use cron-jobs ! And just one app to display results. You can use symfony command https://symfony.com/doc/current/console.html. This commands will run in separate thread from cli (console). – Weenesta - Mathieu Dormeval Nov 22 '16 at 15:30
  • Separate thread? What do you mean? I'm not so good at low level concepts: can you explain me better? How can I manage threads? I'm n Heroku... – Aerendir Nov 22 '16 at 15:33
  • Symfony commands use PHP cli, with command line (shell) your script may be launched like another basic PHP script (like you launch php bin/console cache:clear or other standards commands). I'm not sure that "heroku" letting you access to SSH ? – Weenesta - Mathieu Dormeval Nov 22 '16 at 15:40
  • Yes, but why does I need SSH? My concern is that performing too many background tasks, and also so intensive llike downloading, parsing, analyzing, etc, will make the whole application slower, the frontend too and my users will navigate a very slow website. So I have to increase resources, but if I can separate the resource for the frontend and for the backend it may be better. Independently by the fact I'll do this using two separate application or by using some other sort of separation. For this I'm interested in the thread discourse: may be this a solution? – Aerendir Nov 22 '16 at 15:43
  • SSH will let you have separate threads from your front end application. It will not be impacted by doing some other jobs. This is the best way to achieve this goal. I have made a similar app. I have used symfony commands like the symfony default commands, and cronjobs. You don't need to have an SSH account if you can plan cronjob. You can make symfony command application : http://symfony.com/doc/current/components/console/single_command_tool.html – Weenesta - Mathieu Dormeval Nov 22 '16 at 15:53
  • I know how to build Symfony commands: I currently use them for some simpler background tasks. I'm more interested in better understanding the SSH solution: can you give me some resources? Or can you write a more detailed answer? Also If you don't refere to Heroku directly, but I really like to understand the "behind the scenes" concepts. – Aerendir Nov 22 '16 at 15:55
  • I will send you some detailled answer as soon as possible... – Weenesta - Mathieu Dormeval Nov 22 '16 at 15:58

1 Answers1

0
  1. I will first detail the architecture :

Architecture

  1. You can do an Service like this :

namespace SomeBundle\Utils;

use SomeBundle\Entity\MyEntity;
use SomeBundle\myException;

class SomeService 
{
  private $var;

  public function __construct()
  {

  }

  public function doStuff()
  {
    // Do stuff
  }
}

To debug you service, you can call it from a testController or basic Controller.

  1. You can make Command like this :

namespace SomeBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use SomeBundle\Utils\SomeService;

class SomeCommand extends ContainerAwareCommand 
{
  protected function configure()
  {
    $this->setName('some:command')
         ->setDescription('My awesome command');
  }

  protected function execute(InputInterface $input, OutputInterface $output)
  {
    $container = $this->getContainer();
    $sevice = new SomeService($container);
    $results = $service->doStuff();
  }
}
  1. You can do an Command Application like this :

require __DIR__.'/vendor/autoload.php';
require_once __DIR__.'/app/AppKernel.php';

$kernel = new AppKernel('dev', true);

use SomeBundle\Command\SomeCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;

$application = new Application($kernel);
$application->add(new SomeCommand());
$application->run();

Hope this helps !

  • After several unsuccessful attempts, I abandon the idea of well format the source code ! – Weenesta - Mathieu Dormeval Nov 22 '16 at 16:36
  • Added some `` for formating, tricky with lists ;) – Veve Nov 22 '16 at 16:46
  • First of all many thanks for the answer! For what I understand, you suggest to build a Symfony application that is only a command line application... I'm understanding well? – Aerendir Nov 22 '16 at 17:31
  • No you can build a front end with controllers. – Weenesta - Mathieu Dormeval Nov 22 '16 at 18:22
  • Is a "Symfony Command Line Application" is a file in fact inside an Symfony Application, where you can code a Front End with controllers. – Weenesta - Mathieu Dormeval Nov 23 '16 at 09:57
  • I found the solution with Heroku: using `worker dynos`. The provided example shows how to do it and the answer you gave me here will be very helpful! Maybe you also know how to solve the current problem I'm facing with: http://stackoverflow.com/questions/40796567/heroku-worker-can-i-use-the-database-as-communication-mechanism-between-the-fro :D – Aerendir Nov 25 '16 at 01:42