Skip to content

Commit b617bf8

Browse files
committed
feature:add file_monitor
1 parent dddc7aa commit b617bf8

File tree

3 files changed

+67
-17
lines changed

3 files changed

+67
-17
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
"php": ">=7.2.5",
88
"topthink/framework": "^6.0.0",
99
"topthink/think-worker": "^3.0",
10-
"ext-json": "*"
10+
"ext-json": "*",
11+
"ext-posix": "*",
12+
"ext-pcntl": "*"
1113
},
1214
"autoload": {
1315
"psr-4": {

src/command/AsyncTaskService.php

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@
22
namespace cathy\AsyncTask\command;
33
use cathy\AsyncTask\main\AsyncTaskSynchronizer;
44
use Exception;
5+
use RecursiveDirectoryIterator;
6+
use RecursiveIteratorIterator;
57
use think\console\Command;
68
use think\console\Input;
79
use think\console\input\Argument;
810
use think\console\Output;
11+
use think\facade\App;
912
use think\facade\Config;
13+
use Workerman\Lib\Timer;
1014
use Workerman\Worker;
1115
use function app;
1216

1317
class AsyncTaskService extends Command
1418
{
19+
protected $lastMtime;
20+
1521
public function configure()
1622
{
1723
$this->setName('worker:task')
@@ -53,24 +59,39 @@ public function execute(Input $input, Output $output)
5359
} else {
5460
$port = !empty($option['port']) ? $option['port'] : $configs['port'];
5561
}
56-
if(empty($option['process_number'])){
57-
$option['process_number'] = $configs['process_number'];
62+
63+
if($input->hasOption('daemon')){
64+
$option['daemon'] = true;
65+
} else {
66+
$option['daemon'] = !empty($option['daemon']) ? $option['daemon'] : $configs['daemon'];
67+
}
68+
69+
if(empty($option['count'])){
70+
$option['count'] = $configs['count'];
5871
}
72+
5973
if(empty($option['name'])){
6074
$option['name'] = $configs['name'];
6175
}
76+
6277
if(empty($option['reuse_port'])){
63-
if(isset($option['reuse_port'])){
64-
$option['reuse_port'] = false;
65-
}else{
66-
$option['reuse_port'] = $configs['reuse_port'];
67-
}
78+
$option['reuse_port'] = $configs['reuse_port'];
6879
}
80+
6981
if(empty($option['synchronizer_ttl'])){
7082
$option['synchronizer_ttl'] = $configs['synchronizer_ttl'];
7183
}
72-
if($input->hasOption('daemon')){
73-
$option['daemon'] = true;
84+
85+
if(empty($option['file_monitor'])){
86+
$option['file_monitor'] = $configs['file_monitor'];
87+
}
88+
89+
if(empty($option['file_monitor_interval'])){
90+
$option['file_monitor_interval'] = $configs['file_monitor_interval'];
91+
}
92+
93+
if(empty($option['file_monitor_path'])){
94+
$option['file_monitor_path'] = $configs['file_monitor_path'];
7495
}
7596

7697
$this->start($host, (int) $port, $option);
@@ -81,9 +102,32 @@ public function start(string $host, int $port, array $option = []){
81102
Worker::$daemonize = true;
82103
}
83104
$task_worker = new Worker('tcp://'.$host.':'.$port);
84-
$task_worker->count = $option['process_number']; // 进程数
105+
$task_worker->count = $option['count']; // 进程数
85106
$task_worker->name = $option['name']; // 名称
86107
$task_worker->reusePort = $option['reuse_port']; // 根据配置开启端口复用,让每一个任务进程平衡异步任务,仅php7支持
108+
// 设置文件监控
109+
if (DIRECTORY_SEPARATOR !== '\\' && App::isDebug() && $option['file_monitor'] && 0 == $task_worker->id) {
110+
$timer = $option['file_monitor_interval'] ?: 2;
111+
$paths = !empty($option['file_monitor_path']) ? $option['file_monitor_path'] : [App::getAppPath(), App::getConfigPath()];
112+
Timer::add($timer, function () use ($paths) {
113+
foreach ($paths as $path) {
114+
$dir = new RecursiveDirectoryIterator($path);
115+
$iterator = new RecursiveIteratorIterator($dir);
116+
foreach ($iterator as $file) {
117+
if (pathinfo($file, PATHINFO_EXTENSION) != 'php') {
118+
continue;
119+
}
120+
121+
if ($this->lastMtime < $file->getMTime()) {
122+
echo '[update]' . $file . "\n";
123+
posix_kill(posix_getppid(), SIGUSR1);
124+
$this->lastMtime = $file->getMTime();
125+
return;
126+
}
127+
}
128+
}
129+
});
130+
}
87131
$task_worker->onMessage = function($connection, $data) use ($option) {
88132
$data = json_decode($data,true);
89133
$taskClass = $data['taskClass'];

src/config/config.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
<?php
22
return [
3-
'host' => '0.0.0.0',
4-
'port' => '19345',
5-
'process_number' => 4,
6-
'name' => 'AsyncTask',
7-
'reuse_port' => true,
8-
'synchronizer_ttl' => 3600
3+
'host' => '0.0.0.0',
4+
'port' => 19345,
5+
'count' => 4,
6+
'name' => 'AsyncTask',
7+
'daemon' => true, // 是否开启守护进程
8+
'reuse_port' => true, // 是否开启端口复用
9+
'synchronizer_ttl' => 3600, // 任务同步器taskKey缓存时间
10+
'file_monitor' => true, // 是否开启PHP文件更改监控(调试模式下自动开启)
11+
'file_monitor_interval' => 2, // 文件监控检测时间间隔(秒)
12+
'file_monitor_path' => [], // 文件监控目录 默认监控application和config目录
913
];

0 commit comments

Comments
 (0)