Skip to content

Commit f06d45b

Browse files
committed
update some for run comand
1 parent 697f1ca commit f06d45b

2 files changed

Lines changed: 32 additions & 28 deletions

File tree

app/Command/ServeCommand.php

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use function file_exists;
2222
use function getmypid;
2323
use function md5;
24-
use function output;
2524
use function realpath;
2625
use function sleep;
2726
use 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);

app/Helper/CliHelper.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,28 @@
33
namespace Swoft\Cli\Helper;
44

55
use Swoft\Console\Helper\Show;
6+
use function date;
67

78
/**
89
* Class CliHelper
910
* @since 2.0
1011
*/
1112
class CliHelper
1213
{
13-
public const PREFIX = '<cyan>[SWOFTCLI]</cyan>';
14+
public const PREFIX = ' <cyan>[SWOFTCLI]</cyan>';
1415

1516
public static function info(string $msg): void
1617
{
17-
Show::writeln(self::PREFIX . " <info>$msg</info>");
18+
Show::writeln(date('Y/m/d-H:i:s') . self::PREFIX . " <info>$msg</info>");
1819
}
1920

2021
public static function warn(string $msg): void
2122
{
22-
Show::writeln(self::PREFIX . " <warning>$msg</warning>");
23+
Show::writeln(date('Y/m/d-H:i:s') . self::PREFIX . " <warning>$msg</warning>");
2324
}
2425

2526
public static function error(string $msg): void
2627
{
27-
Show::writeln(self::PREFIX . " <error>$msg</error>");
28+
Show::writeln(date('Y/m/d-H:i:s') . self::PREFIX . " <error>$msg</error>");
2829
}
2930
}

0 commit comments

Comments
 (0)