1 | <?php |
---|
2 | |
---|
3 | class SolrCommands |
---|
4 | { |
---|
5 | public static function getFileCommands() { |
---|
6 | return sfConfig::get('sf_log_dir').'/solr/commands.log'; |
---|
7 | } |
---|
8 | |
---|
9 | protected static $semaphore = null; |
---|
10 | protected static $file = null; |
---|
11 | |
---|
12 | protected static function getSemaphore() { |
---|
13 | if (! self::$semaphore) { |
---|
14 | self::$semaphore = sem_get(rand()); |
---|
15 | } |
---|
16 | return self::$semaphore; |
---|
17 | } |
---|
18 | |
---|
19 | public static function addCommand($status, $json) { |
---|
20 | sem_acquire(self::getSemaphore()); |
---|
21 | if (! self::$file) { |
---|
22 | self::$file = fopen(self::getFileCommands(), 'w'); |
---|
23 | } |
---|
24 | $str = $status.' : '.json_encode($json); |
---|
25 | fwrite(self::$file, $str, strlen($str)); |
---|
26 | sem_release(self::getSemaphore()); |
---|
27 | } |
---|
28 | |
---|
29 | public static function getCommandContent() { |
---|
30 | $lockfile = self::getFileCommands().'.lock'; |
---|
31 | if (file_exists($lockfile)) { |
---|
32 | return $lockfile; |
---|
33 | } |
---|
34 | sem_acquire(self::getSemaphore()); |
---|
35 | if (!self::$file) { |
---|
36 | touch($lockfile); |
---|
37 | sem_release(self::getSemaphore()); |
---|
38 | return $lockfile; |
---|
39 | } |
---|
40 | fclose(self::$file); |
---|
41 | self::$file = null; |
---|
42 | rename(self::getFileCommands(), self::getFileCommands().'.lock'); |
---|
43 | sem_release(self::getSemaphore()); |
---|
44 | return $lockfile; |
---|
45 | } |
---|
46 | public static function releaseCommandContent() { |
---|
47 | sem_acquire(self::getSemaphore()); |
---|
48 | unlink(self::getFileCommands().'.lock'); |
---|
49 | sem_release(self::getSemaphore()); |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|