1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Hhxsv5\LaravelS\Swoole; |
4
|
|
|
|
5
|
|
|
use Hhxsv5\LaravelS\Console\Portal; |
6
|
|
|
use Swoole\Http\Server; |
|
|
|
|
7
|
|
|
use Swoole\Process; |
8
|
|
|
|
9
|
|
|
trait InotifyTrait |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
public function addInotifyProcess(Server $swoole, array $config, array $laravelConf) |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
if (empty($config['enable'])) { |
14
|
|
|
return false; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
if (!extension_loaded('inotify')) { |
18
|
|
|
$this->warning('Require extension inotify'); |
|
|
|
|
19
|
|
|
return false; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
$fileTypes = isset($config['file_types']) ? (array)$config['file_types'] : []; |
23
|
|
|
if (empty($fileTypes)) { |
24
|
|
|
$this->warning('No file types to watch by inotify'); |
25
|
|
|
return false; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$callback = function () use ($config, $laravelConf) { |
29
|
|
|
$log = !empty($config['log']); |
30
|
|
|
$this->setProcessTitle(sprintf('%s laravels: inotify process', $config['process_prefix'])); |
|
|
|
|
31
|
|
|
$inotify = new Inotify($config['watch_path'], IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVE, |
|
|
|
|
32
|
|
|
function ($event) use ($log, $laravelConf) { |
33
|
|
|
Portal::runLaravelSCommand($laravelConf['root_path'], 'reload'); |
34
|
|
|
if ($log) { |
35
|
|
|
$action = 'file:'; |
36
|
|
|
switch ($event['mask']) { |
37
|
|
|
case IN_CREATE: |
|
|
|
|
38
|
|
|
$action = 'create'; |
39
|
|
|
break; |
40
|
|
|
case IN_DELETE: |
|
|
|
|
41
|
|
|
$action = 'delete'; |
42
|
|
|
break; |
43
|
|
|
case IN_MODIFY: |
|
|
|
|
44
|
|
|
$action = 'modify'; |
45
|
|
|
break; |
46
|
|
|
case IN_MOVE: |
|
|
|
|
47
|
|
|
$action = 'move'; |
48
|
|
|
break; |
49
|
|
|
} |
50
|
|
|
$this->info(sprintf('reloaded by inotify, reason: %s %s', $action, $event['name'])); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
}); |
|
|
|
|
53
|
|
|
$inotify->addFileTypes($config['file_types']); |
54
|
|
|
if (empty($config['excluded_dirs'])) { |
55
|
|
|
$config['excluded_dirs'] = []; |
56
|
|
|
} |
57
|
|
|
$inotify->addExcludedDirs($config['excluded_dirs']); |
58
|
|
|
$inotify->watch(); |
59
|
|
|
if ($log) { |
60
|
|
|
$this->info(sprintf('[Inotify] watched files: %d; file types: %s; excluded directories: %s', |
|
|
|
|
61
|
|
|
$inotify->getWatchedFileCount(), |
|
|
|
|
62
|
|
|
implode(',', $config['file_types']), |
|
|
|
|
63
|
|
|
implode(',', $config['excluded_dirs']) |
|
|
|
|
64
|
|
|
) |
|
|
|
|
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
$inotify->start(); |
68
|
|
|
}; |
69
|
|
|
|
70
|
|
|
$process = new Process($callback, false, 0); |
71
|
|
|
$swoole->addProcess($process); |
72
|
|
|
return $process; |
73
|
|
|
} |
74
|
|
|
} |