aboutsummaryrefslogtreecommitdiff
path: root/lib/task/isisBatchTask.class.php
blob: 95be44c3a213365650abbf8e945dfcc57bfe9213 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php

class isisBatchTask extends mySfTask
{
  protected function configure()
  {
    $this->addArgument('rows', sfCommandArgument::OPTIONAL, 'Number of rows to process for each task');
    $this->addOptions(array(
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
      new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
      new sfCommandOption('loglevel', 'l', sfCommandOption::PARAMETER_REQUIRED, 'Log level', 'info'),
      new sfCommandOption('database', 'd', sfCommandOption::PARAMETER_REQUIRED, 'Database', null),
    ));

    $this->namespace           = 'isis';
    $this->name                = 'batch';
    $this->briefDescription    = 'Imports an ISIS database into the application, batch version';
    $this->detailedDescription = <<<EOF
The [isis:import|INFO] task imports an ISIS database into the application.
This is the batch version and uses PNCTL to avoid memory exhaustion.
Call it with:

  [php symfony isis:batch|INFO]
EOF;
  }

  protected function execute($arguments = array(), $options = array())
  {
    // initialize the database connection
    $databaseManager = new sfDatabaseManager($this->configuration);
    $connection      = $databaseManager->getDatabase($options['connection'])->getConnection();

    // Initialize an IsisConnector.
    $class = (class_exists('IsisImporterManager')) ? 'IsisImporterManager' : 'sfIsisImporterManager';
    $isis  = new $class($options['loglevel']);

    // Error handling.
    if ($isis == FALSE)
    {
      $this->logSection('isis', 'Error opening ISIS database.');
      return FALSE;
    }

    $databases = $isis->databases($options['database']);

    foreach ($databases as $database)
    {
      $rows = (isset($arguments['rows'])) ? $argument['rows'] : 20;
      $max  = 1000; // TODO

      for ($offset = 0; $offset <= $max; $offset = $offset + $rows)
      {
        $pid = pcntl_fork();
        if ($pid == -1)
        {
          die('Could not fork');
        }
        else if ($pid)
        {
          // We are the parent.
          // Protect against Zombie children.
          pcntl_wait($status);
        }
        else
        {
          // We are the child.
          // Mass ISIS import.
          $isis->massImport($this, 'task', $rows, $offset, $database);
        }
      }
    }
  }
}