|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class Daemon extends Module { |
|
4
|
|
|
private $tasksDirResource; |
|
5
|
|
|
private $serializer; |
|
6
|
|
|
private $workDir; |
|
7
|
|
|
public $needCheck = 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
|
|
|
echo 'started'; |
|
14
|
|
|
flock($lock, LOCK_UN); |
|
15
|
|
|
fclose($lock); |
|
16
|
|
|
$fp = fsockopen($_SERVER['SERVER_NAME'], |
|
17
|
|
|
80, |
|
18
|
|
|
$errno, $errstr, 30); |
|
19
|
|
|
|
|
20
|
|
|
$out = "GET /daemon/start HTTP/1.1\r\n"; |
|
21
|
|
|
$out .= "Host: " . $_SERVER['SERVER_NAME'] . "\r\n"; |
|
22
|
|
|
$out .= "Connection: Close\r\n\r\n"; |
|
23
|
|
|
fwrite($fp, $out); |
|
24
|
|
|
fclose($fp); |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
function start() { |
|
|
|
|
|
|
29
|
|
|
$workDir = $this->workDir(); |
|
30
|
|
|
$lock = fopen($workDir . '/daemon.lock', 'w+'); |
|
31
|
|
|
if (flock($lock, LOCK_EX | LOCK_NB)) { |
|
32
|
|
|
while ($taskFile = $this->getNextTask()) { |
|
33
|
|
|
$task = $this->unserialize(file_get_contents($taskFile)); |
|
34
|
|
|
$task(); |
|
35
|
|
|
unlink($taskFile); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
function getNextTask() { |
|
|
|
|
|
|
41
|
|
|
$workDir = $this->workDir(); |
|
42
|
|
|
if (!is_resource($this->tasksDirResource)) { |
|
43
|
|
|
$this->tasksDirResource = opendir($workDir . '/tasks/'); |
|
44
|
|
|
} |
|
45
|
|
|
if ($this->tasksDirResource) { |
|
46
|
|
|
while (false !== ($entry = readdir($this->tasksDirResource))) { |
|
47
|
|
|
if ($entry != "." && $entry != "..") { |
|
48
|
|
|
return $workDir . '/tasks/' . $entry; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
return false; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
function task($callback) { |
|
|
|
|
|
|
56
|
|
|
$workDir = $this->workDir(); |
|
57
|
|
|
$taskFile = $workDir . '/tasks/' . microtime(true) . '.task'; |
|
58
|
|
|
file_put_contents($taskFile, $this->serialize($callback)); |
|
59
|
|
|
$this->needCheck = true; |
|
60
|
|
|
return $taskFile; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
function workDir() { |
|
|
|
|
|
|
64
|
|
|
if ($this->workDir) { |
|
65
|
|
|
return $this->workDir; |
|
66
|
|
|
} |
|
67
|
|
|
$path = App::$primary->path . '/daemon'; |
|
68
|
|
|
Tools::createDir(App::$primary->path . '/daemon/tasks'); |
|
69
|
|
|
return $path; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
function unserialize($item) { |
|
|
|
|
|
|
73
|
|
|
return $this->serializer()->unserialize($item); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
function serialize($item) { |
|
|
|
|
|
|
77
|
|
|
return $this->serializer()->serialize($item); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
function serializer() { |
|
|
|
|
|
|
81
|
|
|
if ($this->serializer) { |
|
82
|
|
|
return $this->serializer; |
|
83
|
|
|
} |
|
84
|
|
|
\ComposerCmd::requirePackage('jeremeamia/superclosure'); |
|
85
|
|
|
return $this->serializer = new \SuperClosure\Serializer(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
function __destruct() { |
|
|
|
|
|
|
89
|
|
|
if ($this->needCheck) { |
|
90
|
|
|
$this->check(); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
} |
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.