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 | if (!file_exists(sfConfig::get('sf_log_dir').'/solr/commands.log')) |
---|
11 | touch(sfConfig::get('sf_log_dir').'/solr/commands.log'); |
---|
12 | return sfConfig::get('sf_log_dir').'/solr/commands.log'; |
---|
13 | } |
---|
14 | |
---|
15 | protected $semaphore = null; |
---|
16 | protected $file = null; |
---|
17 | |
---|
18 | protected static $instance = null; |
---|
19 | public static function getInstance() { |
---|
20 | if (!self::$instance) { |
---|
21 | self::$instance = new SolrCommands(); |
---|
22 | } |
---|
23 | return self::$instance; |
---|
24 | } |
---|
25 | |
---|
26 | private function __construct() { |
---|
27 | $this->semaphore = null; |
---|
28 | } |
---|
29 | |
---|
30 | public function __destruct() { |
---|
31 | if ($this->semaphore) { |
---|
32 | sem_remove($this->semaphore); |
---|
33 | $this->semaphore = null; |
---|
34 | } |
---|
35 | } |
---|
36 | |
---|
37 | private static function getSemId() { |
---|
38 | self::getFileCommands(); |
---|
39 | $semfile = sfConfig::get('sf_log_dir')."/solr/SolrSem.id"; |
---|
40 | if (!file_exists($semfile)) { |
---|
41 | touch($semfile); |
---|
42 | } |
---|
43 | $id = ftok($semfile, 's'); |
---|
44 | return $id; |
---|
45 | } |
---|
46 | |
---|
47 | private function protect() { |
---|
48 | # if (! $this->semaphore) { |
---|
49 | $this->semaphore = sem_get(self::getSemId(), 1, 0666, -1); |
---|
50 | # } |
---|
51 | sem_acquire($this->semaphore); |
---|
52 | } |
---|
53 | |
---|
54 | private function unprotect() { |
---|
55 | sem_release($this->semaphore); |
---|
56 | } |
---|
57 | |
---|
58 | public function addCommand($status, $json) { |
---|
59 | $this->protect(); |
---|
60 | if (! $this->file) { |
---|
61 | $this->file = fopen($this->getFileCommands(), 'a+'); |
---|
62 | } |
---|
63 | $str = $status.' : '.json_encode($json)."\n"; |
---|
64 | fwrite($this->file, $str, strlen($str)); |
---|
65 | fclose($this->file); |
---|
66 | $this->file = null; |
---|
67 | $this->unprotect(); |
---|
68 | } |
---|
69 | |
---|
70 | public function getCommandContent() { |
---|
71 | $lockfile = $this->getFileCommands().'.lock'; |
---|
72 | if (file_exists($lockfile)) { |
---|
73 | return $lockfile; |
---|
74 | } |
---|
75 | $this->protect(); |
---|
76 | if ($this->file) { |
---|
77 | fclose($this->file); |
---|
78 | $this->file = null; |
---|
79 | } |
---|
80 | if (!file_exists($this->getFileCommands())) |
---|
81 | touch($this->getFileCommands()); |
---|
82 | rename($this->getFileCommands(), $lockfile); |
---|
83 | $this->unprotect(); |
---|
84 | return $lockfile; |
---|
85 | } |
---|
86 | public function releaseCommandContent() { |
---|
87 | $this->protect(); |
---|
88 | unlink($this->getFileCommands().'.lock'); |
---|
89 | $this->unprotect(); |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|