Skip to content

Commit

Permalink
feat:optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
bingcool committed May 12, 2024
1 parent 02e4dc6 commit 1b9b0f8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ define('IS_WORKER_SERVICE', 0);
define('IS_CLI_SCRIPT', 0);
define('IS_CRON_SERVICE', 0);
define('PHP_BIN_FILE','/usr/bin/php');
define('WORKER_START_SCRIPT_FILE', $_SERVER['PWD'].'/'.$_SERVER['SCRIPT_FILENAME']);
date_default_timezone_set('Asia/Shanghai');
Expand Down
31 changes: 12 additions & 19 deletions src/Cmd/RestartCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$appName = $input->getArgument('app_name');
$force = $input->getOption('force');
$force = $input->getOption('force');
$lineValue = "";
if (empty($force)) {
if (SystemEnv::isWorkerService()) {
Expand All @@ -49,52 +49,45 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
} else {
if (SystemEnv::isWorkerService()) {
fmtPrintInfo("\n你已放弃【重启】workerService【" . WORKER_SERVICE_NAME . "】,应用继续running中");
fmtPrintInfo(PHP_EOL."你已放弃【重启】workerService【" . WORKER_SERVICE_NAME . "】,应用继续running中");
exit(0);
} else {
fmtPrintInfo("\n你已放弃【重启】应用【{$appName}】,应用继续running中");
fmtPrintInfo(PHP_EOL."你已放弃【重启】应用【{$appName}】,应用继续running中");
exit(0);
}
}

fmtPrintInfo("-----------正在重启进程中,请等待-----------");

if (SystemEnv::isWorkerService() || SystemEnv::isCronService()) {
if (SystemEnv::isWorkerService()) {
while (true) {
if ($masterPid > 0 && \Swoole\Process::kill($masterPid, 0)) {
sleep(1);
}else {
break;
}
}
}else {
sleep(1);
}

// send restart command to main worker process
$binFile = SystemEnv::PhpBinFile();
$waitTime = 10;
$phpBinFile = SystemEnv::PhpBinFile();
$waitTime = 10;
if (SystemEnv::isWorkerService()) {
$selfFile = WORKER_START_SCRIPT_FILE;
if (SystemEnv::isCronService()) {
$selfFile = 'cron.php';
}else if (SystemEnv::isDaemonService()) {
$selfFile = 'daemon.php';
}
// sleep max 30s
$waitTime = 30;
}else {
$selfFile = 'cli.php';
}

$selfFile = WORKER_START_SCRIPT_FILE;
$scriptFile = "$selfFile start {$appName} --daemon=1";

\Swoole\Coroutine::create(function () use ($binFile, $scriptFile) {
$runner = CommandRunner::getInstance('restart');
\Swoole\Coroutine::create(function () use ($phpBinFile, $scriptFile) {
$runner = CommandRunner::getInstance('restart-'.time());
$runner->isNextHandle(false);
$runner->procOpen(function () {
}, $binFile, $scriptFile);
}, $phpBinFile, $scriptFile);
});

$phpBinFile = SystemEnv::PhpBinFile();
$time = time();
while (true) {
sleep(1);
Expand Down
2 changes: 1 addition & 1 deletion src/Cmd/SendCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
\Swoole\Event::exit();
});

// 发送数据toWorker
// send msg to Worker
$pipeMsg = serialize($pipeMsgDto);
// mainWorker Process
$workerPid = file_get_contents(WORKER_PID_FILE);
Expand Down

0 comments on commit 1b9b0f8

Please sign in to comment.