1
|
|
|
<?php |
2
|
|
|
/**************************************************************************** |
3
|
|
|
* For license information see doc/license.txt |
4
|
|
|
* |
5
|
|
|
* Unicode Reminder メモ |
6
|
|
|
* |
7
|
|
|
* synchronization of processes which must not run concurrently; |
8
|
|
|
****************************************************************************/ |
9
|
|
|
|
10
|
|
|
namespace Oc\Util; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ProcessSync |
14
|
|
|
*/ |
15
|
|
|
class ProcessSync |
16
|
|
|
{ |
17
|
|
|
/** @var string $pidFilePath */ |
18
|
|
|
private $pidFilePath; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* ProcessSync constructor. |
23
|
|
|
* |
24
|
|
|
* @param $name |
25
|
|
|
*/ |
26
|
|
|
public function __construct($name) |
27
|
|
|
{ |
28
|
|
|
$this->pidFilePath = __DIR__ . '/../../../cache2/' . $name . '.pid'; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Enter code section which must not run concurrently |
34
|
|
|
* |
35
|
|
|
* @return bool |
36
|
|
|
*/ |
37
|
|
|
public function enter() |
38
|
|
|
{ |
39
|
|
|
if (!$this->checkDaemon()) { |
40
|
|
|
return false; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (file_exists($this->pidFilePath)) { |
44
|
|
|
echo 'Error: PidFile (' . $this->pidFilePath . ") already present\n"; |
45
|
|
|
|
46
|
|
|
return false; |
47
|
|
|
} else { |
48
|
|
|
if ($pidFile = @fopen($this->pidFilePath, 'w')) { |
49
|
|
|
fwrite($pidFile, posix_getpid()); |
50
|
|
|
fclose($pidFile); |
51
|
|
|
|
52
|
|
|
return true; |
53
|
|
|
} else { |
54
|
|
|
echo "can't create PidFile " . $this->pidFilePath . "\n"; |
55
|
|
|
|
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* checks if other instance of process is running |
63
|
|
|
* |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
private function checkDaemon() |
67
|
|
|
{ |
68
|
|
|
if ($pidFile = @fopen($this->pidFilePath, 'r')) { |
69
|
|
|
$pidDaemon = fgets($pidFile, 20); |
70
|
|
|
fclose($pidFile); |
71
|
|
|
|
72
|
|
|
$pidDaemon = (int)$pidDaemon; |
73
|
|
|
|
74
|
|
|
// bad PID file, e.g. due to system malfunction while creating the file? |
75
|
|
|
if ($pidDaemon <= 0) { |
76
|
|
|
echo 'removing bad PidFile (' . $this->pidFilePath . ")\n"; |
77
|
|
|
unlink($this->pidFilePath); |
78
|
|
|
|
79
|
|
|
return false; |
80
|
|
|
} // process running? |
81
|
|
|
elseif (posix_kill($pidDaemon, 0)) { |
82
|
|
|
// yes, good bye |
83
|
|
|
echo 'Error: process for ' . $this->pidFilePath . " is already running with pid=$pidDaemon\n"; |
84
|
|
|
|
85
|
|
|
return false; |
86
|
|
|
} else { |
87
|
|
|
// no, remove pid_file |
88
|
|
|
echo 'process not running, removing old PidFile (' . $this->pidFilePath . ")\n"; |
89
|
|
|
unlink($this->pidFilePath); |
90
|
|
|
|
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
} else { |
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Leave code section which must not run concurrently |
101
|
|
|
* |
102
|
|
|
* @param bool $message |
103
|
|
|
*/ |
104
|
|
|
public function leave($message = false) |
105
|
|
|
{ |
106
|
|
|
if ($pidFile = @fopen($this->pidFilePath, 'r')) { |
107
|
|
|
$pid = fgets($pidFile, 20); |
108
|
|
|
fclose($pidFile); |
109
|
|
|
if ($pid === posix_getpid()) { |
|
|
|
|
110
|
|
|
unlink($this->pidFilePath); |
111
|
|
|
} |
112
|
|
|
} else { |
113
|
|
|
echo "Error: can't delete own PidFile (" . $this->pidFilePath . ")\n"; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if ($message) { |
117
|
|
|
echo $message . "\n"; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|