|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class Daemon extends Module { |
|
4
|
|
|
private $tasksDirResource; |
|
5
|
|
|
private $serializer; |
|
6
|
|
|
private $workDir; |
|
7
|
|
|
public $checked = false; |
|
8
|
|
|
|
|
9
|
|
|
function check() { |
|
|
|
|
|
|
10
|
|
|
$workDir = $this->workDir(); |
|
11
|
|
|
$lock = fopen($workDir . '/daemon.lock', 'w+'); |
|
12
|
|
|
if (flock($lock, LOCK_EX | LOCK_NB)) { |
|
13
|
|
|
flock($lock, LOCK_UN); |
|
14
|
|
|
fclose($lock); |
|
15
|
|
|
if (function_exists('pcntl_fork') && $pid = pcntl_fork() !== -1) { |
|
16
|
|
|
if ($pid) { |
|
17
|
|
|
return $pid; |
|
18
|
|
|
} else { |
|
19
|
|
|
$this->start(true); |
|
20
|
|
|
} |
|
21
|
|
|
} elseif (function_exists('curl_init')) { |
|
22
|
|
|
$ch = curl_init('http://' . $_SERVER['SERVER_NAME'] . '/daemon/start'); |
|
23
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
24
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1); |
|
25
|
|
|
curl_setopt($ch, CURLOPT_NOSIGNAL, 1); |
|
26
|
|
|
$content = curl_exec($ch); |
|
|
|
|
|
|
27
|
|
|
curl_close($ch); |
|
28
|
|
|
} else { |
|
29
|
|
|
$fp = fsockopen($_SERVER['SERVER_NAME'], |
|
30
|
|
|
80, |
|
31
|
|
|
$errno, $errstr, 30); |
|
32
|
|
|
$out = "GET /daemon/start HTTP/1.1\r\n"; |
|
33
|
|
|
$out .= "Host: " . $_SERVER['SERVER_NAME'] . "\r\n"; |
|
34
|
|
|
$out .= "Connection: Close\r\n\r\n"; |
|
35
|
|
|
fwrite($fp, $out); |
|
36
|
|
|
fclose($fp); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
function start($retry = false) { |
|
|
|
|
|
|
42
|
|
|
$workDir = $this->workDir(); |
|
43
|
|
|
$lock = fopen($workDir . '/daemon.lock', 'w+'); |
|
44
|
|
|
if (flock($lock, LOCK_EX | LOCK_NB)) { |
|
45
|
|
|
set_time_limit(0); |
|
46
|
|
|
while (true) { |
|
47
|
|
|
$taskFile = $this->getNextTask(); |
|
48
|
|
|
if (!$taskFile) { |
|
|
|
|
|
|
49
|
|
|
for ($i = 0; $i < 60; $i++) { |
|
50
|
|
|
sleep(1); |
|
51
|
|
|
$taskFile = $this->getNextTask(); |
|
52
|
|
|
if ($taskFile) { |
|
|
|
|
|
|
53
|
|
|
break; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
if (!$taskFile) { |
|
|
|
|
|
|
57
|
|
|
break; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
$task = $this->unserialize(file_get_contents($taskFile)); |
|
61
|
|
|
unlink($taskFile); |
|
62
|
|
|
if ($task) { |
|
63
|
|
|
$task(); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} else { |
|
67
|
|
|
if ($retry) { |
|
68
|
|
|
sleep(1); |
|
69
|
|
|
$this->start(false); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
function getNextTask() { |
|
|
|
|
|
|
75
|
|
|
$workDir = $this->workDir(); |
|
76
|
|
|
if (!is_resource($this->tasksDirResource)) { |
|
77
|
|
|
$this->tasksDirResource = opendir($workDir . '/tasks/'); |
|
78
|
|
|
} |
|
79
|
|
|
if ($this->tasksDirResource) { |
|
80
|
|
|
while (false !== ($entry = readdir($this->tasksDirResource))) { |
|
81
|
|
|
if ($entry != "." && $entry != "..") { |
|
82
|
|
|
return $workDir . '/tasks/' . $entry; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
function task($callback) { |
|
|
|
|
|
|
90
|
|
|
$keyLog = \App::$cur->log->start('add task'); |
|
91
|
|
|
$workDir = $this->workDir(); |
|
92
|
|
|
$taskFile = $workDir . '/tasks/' . microtime(true) . '.task'; |
|
93
|
|
|
$keyLog2 = \App::$cur->log->start('serialize task'); |
|
94
|
|
|
$serialize = $this->serialize($callback); |
|
95
|
|
|
\App::$cur->log->end($keyLog2); |
|
96
|
|
|
$keyLog3 = \App::$cur->log->start('put task to file'); |
|
97
|
|
|
file_put_contents($taskFile, $serialize); |
|
98
|
|
|
\App::$cur->log->end($keyLog3); |
|
99
|
|
|
if (!$this->checked) { |
|
100
|
|
|
$this->checked = true; |
|
101
|
|
|
$this->check(); |
|
102
|
|
|
} |
|
103
|
|
|
\App::$cur->log->end($keyLog); |
|
104
|
|
|
return $taskFile; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
function workDir() { |
|
|
|
|
|
|
108
|
|
|
if ($this->workDir) { |
|
109
|
|
|
return $this->workDir; |
|
110
|
|
|
} |
|
111
|
|
|
$path = App::$primary->path . '/daemon'; |
|
112
|
|
|
Tools::createDir(App::$primary->path . '/daemon/tasks'); |
|
113
|
|
|
return $path; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
function unserialize($item) { |
|
|
|
|
|
|
117
|
|
|
try { |
|
118
|
|
|
return $this->serializer()->unserialize($item); |
|
119
|
|
|
} catch (\SuperClosure\Exception\ClosureUnserializationException $e) { |
|
|
|
|
|
|
120
|
|
|
return false; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
function serialize($item) { |
|
|
|
|
|
|
125
|
|
|
return $this->serializer()->serialize($item); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
function serializer() { |
|
|
|
|
|
|
129
|
|
|
if ($this->serializer) { |
|
130
|
|
|
return $this->serializer; |
|
131
|
|
|
} |
|
132
|
|
|
\ComposerCmd::requirePackage('jeremeamia/superclosure'); |
|
133
|
|
|
return $this->serializer = new \SuperClosure\Serializer(new SuperClosure\Analyzer\TokenAnalyzer()); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
} |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.