22namespace cathy \AsyncTask \command ;
33use cathy \AsyncTask \main \AsyncTaskSynchronizer ;
44use Exception ;
5+ use RecursiveDirectoryIterator ;
6+ use RecursiveIteratorIterator ;
57use think \console \Command ;
68use think \console \Input ;
79use think \console \input \Argument ;
810use think \console \Output ;
11+ use think \facade \App ;
912use think \facade \Config ;
13+ use Workerman \Lib \Timer ;
1014use Workerman \Worker ;
1115use function app ;
1216
1317class 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 ' ];
0 commit comments