2121use function file_exists ;
2222use function getmypid ;
2323use function md5 ;
24- use function output ;
2524use function realpath ;
2625use function sleep ;
2726use function sprintf ;
@@ -45,6 +44,11 @@ class ServeCommand
4544 */
4645 private $ pid = 0 ;
4746
47+ /**
48+ * @var int
49+ */
50+ private $ retry = 0 ;
51+
4852 /**
4953 * @var bool
5054 */
@@ -98,10 +102,10 @@ private function collectInfo(Input $input): bool
98102 $ this ->phpBin = $ input ->getOpt ('php-bin ' );
99103
100104 if ($ this ->phpBin === 'php ' ) {
101- [$ ok , $ ret , ] = Sys::run ('which php ' );
105+ [$ ok , $ ret ,] = Sys::run ('which php ' );
102106
103107 if ($ ok === 0 ) {
104- $ this ->phpBin = \ trim ($ ret );
108+ $ this ->phpBin = trim ($ ret );
105109 }
106110 }
107111
@@ -155,7 +159,7 @@ private function collectInfo(Input $input): bool
155159 return $ path ;
156160 }, $ this ->watchDir );
157161
158- if (!$ this ->watchDir = array_filter ($ watchDirs )){
162+ if (!$ this ->watchDir = array_filter ($ watchDirs )) {
159163 Show::error ('Did not enter any valid monitoring directory ' );
160164 return false ;
161165 }
@@ -185,7 +189,7 @@ private function collectInfo(Input $input): bool
185189 * "watch", short="w", default="app,config", type="directories",
186190 * desc="List of directories you want to watch, relative the <cyan>targetPath</cyan>"
187191 * )
188- * @param Input $input
192+ * @param Input $input
189193 * @param Output $output
190194 *
191195 * @example
@@ -197,7 +201,7 @@ public function run(Input $input, Output $output): void
197201 return ;
198202 }
199203
200- $ fileName = 'server- ' . md5 ($ this ->entryFile ) . '.id ' ;
204+ $ fileName = 'server- ' . md5 ($ this ->entryFile ) . '.id ' ;
201205 // $mw = new ModifyWatcher(Sys::getTempDir() . '/' . $fileName));
202206 $ mw = new ModifyWatcher (Swoft::getAlias ('@runtime/ ' . $ fileName ));
203207 $ mw ->watchDir ($ this ->watchDir );
@@ -211,32 +215,31 @@ public function run(Input $input, Output $output): void
211215 if ($ ret = Process::wait (false )) {
212216 $ exitPid = $ ret ['pid ' ];
213217 $ exitCode = $ ret ['code ' ];
214- CliHelper::warn ("Server(pid $ exitPid) exited (signal {$ ret ['signal ' ]}, code $ exitCode) " );
215-
216- // Exit with error
218+ CliHelper::warn ("Target server(pid $ exitPid) exited (signal {$ ret ['signal ' ]}, code $ exitCode) " );
217219 if ($ exitCode !== 0 ) {
218220 CliHelper::error ('Server non-zero status exit ' );
219-
220- $ msg = $ output ->read ('restart?(y/n) > ' );
221- if ($ msg && 0 === stripos ($ msg , 'n ' )) {
222- CliHelper::info ('Exit ' );
223- return ;
224- }
225-
226- CliHelper::info ('will restart server ' );
227- $ pid = $ this ->startServer ();
228- continue ;
229221 }
230222
231- if ($ exitPid === $ pid ) {
232- $ pid = $ this ->startServer ();
233- }
223+ // if ($retry > 2) {
224+ // $msg = $output->read("Have auto try start server {$retry} times, restart?(y/n) >");
225+ // if ($msg && 0 === stripos($msg, 'n')) {
226+ // CliHelper::info('Exit');
227+ // return;
228+ // }
229+ //
230+ // $retry = 0;
231+ // }
232+
233+ CliHelper::info ('Will try restart server ... after 3 seconds ' );
234+ sleep (3 );
235+ $ pid = $ this ->startServer ();
236+ continue ;
234237 }
235238
236239 if ($ mw ->isChanged ()) {
237240 CliHelper::info (date ('Y/m/d H:i:s ' ) . ': file changed! ' );
238241 Show::aList ($ mw ->getChangedInfo (), 'modify info ' );
239- CliHelper::info ('will restart server ' );
242+ CliHelper::info ('Will restart server ' );
240243
241244 if (false === $ this ->stopServer ($ pid )) {
242245 CliHelper::info ('Exit ' );
@@ -245,7 +248,7 @@ public function run(Input $input, Output $output): void
245248
246249 $ pid = $ this ->startServer ();
247250 } elseif ($ this ->debug ) {
248- CliHelper::info (date ('Y/m/d H:i:s ' ) . ': no change! ' );
251+ CliHelper::info (date ('Y/m/d H:i:s ' ) . ': files no change! ' );
249252 }
250253
251254 sleep ($ this ->interval );
0 commit comments