1 | <?php |
---|
2 | |
---|
3 | class SolrCommands |
---|
4 | { |
---|
5 | public static function getFileCommands() { |
---|
6 | umask(0000); |
---|
7 | if (!file_exists(sfConfig::get('sf_log_dir').'/solr/')) { |
---|
8 | mkdir (sfConfig::get('sf_log_dir').'/solr/'); |
---|
9 | } |
---|
10 | return sfConfig::get('sf_log_dir').'/solr/commands.log'; |
---|
11 | } |
---|
12 | |
---|
13 | protected $semaphore = null; |
---|
14 | protected $file = null; |
---|
15 | |
---|
16 | protected static $instance = null; |
---|
17 | protected static $semaphore_id = "99999910823498202340982340982340981678"; |
---|
18 | public static function getInstance() { |
---|
19 | if (!self::$instance) { |
---|
20 | self::$instance = new SolrCommands(); |
---|
21 | } |
---|
22 | return self::$instance; |
---|
23 | } |
---|
24 | |
---|
25 | public function __construct() { |
---|
26 | $this->semaphore = sem_get(self::$semaphore_id); |
---|
27 | } |
---|
28 | |
---|
29 | public function __destruct() { |
---|
30 | sem_remove($this->semaphore); |
---|
31 | $this->semaphore = null; |
---|
32 | } |
---|
33 | |
---|
34 | public function addCommand($status, $json) { |
---|
35 | sem_acquire($this->semaphore); |
---|
36 | if (! $this->file) { |
---|
37 | $this->file = fopen($this->getFileCommands(), 'a+'); |
---|
38 | } |
---|
39 | $str = $status.' : '.json_encode($json)."\n"; |
---|
40 | fwrite($this->file, $str, strlen($str)); |
---|
41 | sem_release($this->semaphore); |
---|
42 | } |
---|
43 | |
---|
44 | public function getCommandContent() { |
---|
45 | $lockfile = $this->getFileCommands().'.lock'; |
---|
46 | if (file_exists($lockfile)) { |
---|
47 | return $lockfile; |
---|
48 | } |
---|
49 | sem_acquire($this->semaphore); |
---|
50 | if ($this->file) { |
---|
51 | fclose($this->file); |
---|
52 | $this->file = null; |
---|
53 | } |
---|
54 | if (!file_exists($this->getFileCommands())) |
---|
55 | touch($this->getFileCommands()); |
---|
56 | rename($this->getFileCommands(), $lockfile); |
---|
57 | sem_release($this->semaphore); |
---|
58 | return $lockfile; |
---|
59 | } |
---|
60 | public function releaseCommandContent() { |
---|
61 | sem_acquire($this->semaphore); |
---|
62 | unlink($this->getFileCommands().'.lock'); |
---|
63 | sem_release($this->semaphore); |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|