Skip to content

Commit a80945b

Browse files
committed
refactor: clean up signal handling and remove unused code
- Remove SIGKILL hack, use graceful coroutine wait - Simplify SignalHandler exit logic - Add ShutdownHelper to consolidate shutdown code - Remove unused LogWorker/CoroutineFileTarget parameters - Update all configs and docs to remove deprecated options
1 parent e1acdcc commit a80945b

File tree

13 files changed

+335
-509
lines changed

13 files changed

+335
-509
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ return [
106106
'class' => \Dacheng\Yii2\Swoole\Log\CoroutineFileTarget::class,
107107
'levels' => ['error', 'warning'],
108108
'logFile' => '@runtime/logs/app.log',
109-
'channelSize' => 10000,
109+
'maxFileSize' => 10240, // KB
110+
'maxLogFiles' => 5,
111+
'enableRotation' => true,
110112
],
111113
],
112114
],
@@ -230,11 +232,11 @@ curl http://127.0.0.1:9501/
230232
'targets' => [
231233
[
232234
'class' => \Dacheng\Yii2\Swoole\Log\CoroutineFileTarget::class,
233-
'channelSize' => 10000, // Channel buffer size
234-
'pushTimeout' => 0.5, // Timeout for pushing to channel
235-
'batchSize' => 1000, // Messages per batch write
235+
'levels' => ['error', 'warning'],
236+
'logFile' => '@runtime/logs/app.log',
236237
'maxFileSize' => 10240, // Max file size before rotation (KB)
237238
'maxLogFiles' => 5, // Number of rotated files to keep
239+
'enableRotation' => true, // Enable log rotation
238240
],
239241
],
240242
],

examples/app-api/config/console.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
'levels' => ['error', 'warning', 'info'],
2121
'exportInterval' => 1,
2222
'logFile' => '@runtime/logs/console.log',
23-
'channelSize' => (int)(getenv('YII_LOG_CHANNEL_SIZE') ?: 10000),
24-
'pushTimeout' => 0.5,
25-
'batchSize' => 1000, // Packets per batch write
2623
'maxFileSize' => 10240, // 10MB
2724
'maxLogFiles' => 5,
2825
'enableRotation' => true,

examples/app-api/config/web.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@
3737
'levels' => ['error', 'warning'],
3838
'exportInterval' => 1,
3939
'logFile' => '@runtime/logs/app.log',
40-
'channelSize' => (int)(getenv('YII_LOG_CHANNEL_SIZE') ?: 10000),
41-
'pushTimeout' => 0.5,
42-
'batchSize' => 1000,
4340
'maxFileSize' => 10240,
4441
'maxLogFiles' => 5,
4542
'enableRotation' => true,

examples/app-api/controllers/LogController.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,6 @@ public function actionStats()
199199
$lineCount = count(file($logFile));
200200
}
201201

202-
$channelStats = $logTarget->getChannelStats();
203-
204202
$result = [
205203
'success' => true,
206204
'log_file' => $logFile,
@@ -210,15 +208,10 @@ public function actionStats()
210208
'line_count' => $lineCount,
211209
'max_file_size_kb' => $logTarget->maxFileSize,
212210
'max_log_files' => $logTarget->maxLogFiles,
213-
'channel_size' => $logTarget->channelSize,
214-
'push_timeout' => $logTarget->pushTimeout,
215211
'rotation_enabled' => $logTarget->enableRotation,
212+
'worker_initialized' => $logTarget->getWorker() !== null,
216213
];
217214

218-
if ($channelStats !== null) {
219-
$result['channel'] = $channelStats;
220-
}
221-
222215
return $result;
223216
}
224217
}

examples/app-basic/config/console.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@
3535
'levels' => ['error', 'warning', 'info'],
3636
'exportInterval' => 1,
3737
'logFile' => '@runtime/logs/console.log',
38-
'channelSize' => (int)(getenv('YII_LOG_CHANNEL_SIZE') ?: 10000),
39-
'pushTimeout' => 0.5,
40-
'batchSize' => 1000, // Packets per batch write
4138
'maxFileSize' => 10240, // 10MB
4239
'maxLogFiles' => 5,
4340
'enableRotation' => true,

examples/app-basic/config/web.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@
5353
'levels' => ['error', 'warning'],
5454
'exportInterval' => 1,
5555
'logFile' => '@runtime/logs/app.log',
56-
'channelSize' => (int)(getenv('YII_LOG_CHANNEL_SIZE') ?: 10000),
57-
'pushTimeout' => 0.5,
58-
'batchSize' => 1000,
5956
'maxFileSize' => 10240,
6057
'maxLogFiles' => 5,
6158
'enableRotation' => true,

src/Console/SwooleController.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,20 @@ public function actionStart(?string $host = null, ?string $port = null): int
6464

6565
$this->stdout(sprintf("Swoole HTTP server listening on %s:%d\n", $server->host, $server->port));
6666

67-
$server->start();
67+
try {
68+
$server->start();
69+
} catch (\Swoole\ExitException $e) {
70+
// ExitException is thrown during graceful shutdown, this is expected
71+
$exitCode = method_exists($e, 'getStatus') ? $e->getStatus() : 0;
72+
73+
if ($exitCode === 0) {
74+
$this->stdout("Swoole HTTP server stopped gracefully.\n");
75+
return ExitCode::OK;
76+
}
77+
78+
$this->stderr(sprintf("Swoole HTTP server exited with code %d: %s\n", $exitCode, $e->getMessage()));
79+
return $exitCode;
80+
}
6881

6982
return ExitCode::OK;
7083
}

0 commit comments

Comments
 (0)