Changeset 1528


Ignore:
Timestamp:
Oct 2, 2010, 8:47:30 PM (10 years ago)
Author:
teymour
Message:

Meilleure gestion des sémaphores

File:
1 edited

Legend:

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

    r1457 r1528  
    88      mkdir (sfConfig::get('sf_log_dir').'/solr/');
    99    }
     10    if (!file_exists(sfConfig::get('sf_log_dir').'/solr/commands.log'))
     11      touch(sfConfig::get('sf_log_dir').'/solr/commands.log');
    1012    return sfConfig::get('sf_log_dir').'/solr/commands.log';
    1113  }
     
    1517
    1618  protected static $instance = null;
    17   protected static $semaphore_id = "99999910823498202340982340982340981678";
    1819  public static function getInstance() {
    1920    if (!self::$instance) {
     
    2324  }
    2425
    25   public function __construct() {
    26     $this->semaphore = sem_get(self::$semaphore_id);
     26  public function __destruct() {
     27    if ($this->semaphore)
     28      $this->unprotect();
    2729  }
    2830
    29   public function __destruct() {
    30     sem_remove($this->semaphore); //semble supprimer non seulement notre instance mais le semaphore global :(
     31  private function protect() {
     32    if (! $this->semaphore) {
     33      $id = ftok(self::getFileCommands(), 's');
     34      $this->semaphore = sem_get($id);
     35    }
     36    sem_acquire($this->semaphore);
     37  }
     38
     39  private function unprotect() {
     40    sem_release($this->semaphore);
     41    sem_remove($this->semaphore);
    3142    $this->semaphore = null;
    3243  }
    3344
    3445  public function addCommand($status, $json) {
    35     sem_acquire($this->semaphore);
     46    $this->protect();
    3647    if (! $this->file) {
    3748      $this->file = fopen($this->getFileCommands(), 'a+');
     
    3950    $str = $status.' : '.json_encode($json)."\n";
    4051    fwrite($this->file, $str, strlen($str));
    41     sem_release($this->semaphore);
     52    $this->unprotect();
    4253  }
    4354
     
    4758      return $lockfile;
    4859    }
    49     sem_acquire($this->semaphore);
     60    $this->protect();
    5061    if ($this->file) {
    5162      fclose($this->file);
     
    5566      touch($this->getFileCommands());
    5667    rename($this->getFileCommands(), $lockfile);
    57     sem_release($this->semaphore);
     68    $this->unprotect();
    5869    return $lockfile;
    5970  }
    6071  public  function releaseCommandContent() {
    61     sem_acquire($this->semaphore);
     72    $this->protect();
    6273    unlink($this->getFileCommands().'.lock');
    63     sem_release($this->semaphore);
     74    $this->unprotect();
    6475  }
    6576}
Note: See TracChangeset for help on using the changeset viewer.