Changeset 1332


Ignore:
Timestamp:
Sep 7, 2010, 1:53:52 PM (10 years ago)
Author:
teymour
Message:

Résolution du pb de semaphore pointé par Roux

Location:
cpc/trunk/project
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • cpc/trunk/project/lib/model/solr/SolrCommands.class.php

    r1323 r1332  
    77  }
    88
    9   protected static $semaphore = null;
    10   protected static $file = null;
     9  protected $semaphore = null;
     10  protected $file = null;
    1111
    12   protected static function getSemaphore() {
    13     if (! self::$semaphore) {
    14       self::$semaphore = sem_get(rand());
     12  protected static $instance = null;
     13  protected static $semaphore_id = "99999910823498202340982340982340981678";
     14  public static function getInstance() {
     15    if (!self::$instance) {
     16      self::$instance = new SolrCommands();
    1517    }
    16     return self::$semaphore;
     18    return self::$instance;
    1719  }
    1820
    19   public static function addCommand($status, $json) {
    20     sem_acquire(self::getSemaphore());
    21     if (! self::$file) {
    22       self::$file = fopen(self::getFileCommands(), 'a+');
     21  public function __construct() {
     22    $this->semaphore = sem_get(self::$semaphore_id);
     23  }
     24
     25  public function __destruct() {
     26    sem_remove($this->semaphore);
     27    $this->semaphore = null;
     28  }
     29
     30  public function addCommand($status, $json) {
     31    sem_acquire($this->semaphore);
     32    if (! $this->file) {
     33      $this->file = fopen($this->getFileCommands(), 'a+');
    2334    }
    2435    $str = $status.' : '.json_encode($json)."\n";
    25     fwrite(self::$file, $str, strlen($str));
    26     sem_release(self::getSemaphore());
     36    fwrite($this->file, $str, strlen($str));
     37    sem_release($this->semaphore);
    2738  }
    2839
    29   public static function getCommandContent() {
    30     $lockfile = self::getFileCommands().'.lock';
     40  public function getCommandContent() {
     41    $lockfile = $this->getFileCommands().'.lock';
    3142    if (file_exists($lockfile)) {
    3243      return $lockfile;
    3344    }
    34     sem_acquire(self::getSemaphore());
    35     if (!self::$file) {
     45    sem_acquire($this->semaphore);
     46    if (!$this->file) {
    3647      touch($lockfile);
    37       sem_release(self::getSemaphore());
     48      sem_release($this->semaphore);
    3849      return $lockfile;
    3950    }
    40     fclose(self::$file);
    41     self::$file = null;
    42     rename(self::getFileCommands(), self::getFileCommands().'.lock');
    43     sem_release(self::getSemaphore());
     51    fclose($this->file);
     52    $this->file = null;
     53    rename($this->getFileCommands(), $this->getFileCommands().'.lock');
     54    sem_release($this->semaphore);
    4455    return $lockfile;
    4556  }
    46   public static function releaseCommandContent() {
    47     sem_acquire(self::getSemaphore());
    48     unlink(self::getFileCommands().'.lock');
    49     sem_release(self::getSemaphore());
     57  public function releaseCommandContent() {
     58    sem_acquire($this->semaphore);
     59    unlink($this->getFileCommands().'.lock');
     60    sem_release($this->semaphore);
    5061  }
    5162}
  • cpc/trunk/project/lib/model/solr/SolrConnector.class.php

    r1330 r1332  
    2929
    3030  public function updateFromCommands() {
    31     $file = SolrCommands::getCommandContent();
     31    $file = SolrCommands::getInstance()->getCommandContent();
    3232    foreach(file($file) as $line) {
    3333      if (preg_match('/(UPDATE|DELETE) : (.+)/', $line, $matches)) {
     
    4040      }
    4141    }
    42     SolrCommands::releaseCommandContent();
     42    SolrCommands::getInstance()->releaseCommandContent();
    4343  }
    4444
  • cpc/trunk/project/lib/model/solr/SolrListener.php

    r1324 r1332  
    99     */
    1010    protected $_options = array();
    11 
    12     protected static $fileCommand = null;
    13     protected static $fileDispatcher = null;
    14 
    15     protected static function getFileCommand() {
    16       if (!self::$fileDispatcher) {
    17         self::$fileDispatcher = new sfEventDispatcher();
    18       }
    19       if (!self::$fileCommand) {
    20         self::$fileCommand = new sfFileLogger(self::$fileDispatcher, array('file' => SolrConnector::getFileCommands()));
    21       }
    22       return self::$fileCommand;
    23     }
    2411
    2512    private $command = null;
     
    145132    $json['tags']['weight'] = $extra_weight;
    146133   
    147     SolrCommands::addCommand('UPDATE', $json);
     134    SolrCommands::getInstance()->addCommand('UPDATE', $json);
    148135  }
    149136 
     
    154141    $json = new stdClass();
    155142    $json->id = $this->getLuceneObjId($obj);
    156     SolrCommands::addCommand('DELETE', $json);
     143    SolrCommands::getInstance()->addCommand('DELETE', $json);
    157144  }
    158145 
  • cpc/trunk/project/test/unit/solrableTest.php

    r1323 r1332  
    5454$q->save();
    5555
    56 $i = Doctrine::getTable('Intervention')->find(392980);
    57 $i->save();
     56$iexists = Doctrine::getTable('Intervention')->find(3);
     57$iexists->save();
    5858
    5959$s->updateFromCommands();
Note: See TracChangeset for help on using the changeset viewer.