Use on worker stop to properly reclaim realtime redis connections and unsubscribe

This commit is contained in:
Jake Barnby committed 2024-04-09 17:05:44 +12:00
1 parent fefb60557d
commit 4bef6125f7
3 files changed
+34 -33

No files matched your search

+21 -14
View File
@@ -38,6 +38,8 @@ require_once __DIR__ . '/init.php';
Runtime::enableCoroutine();
$redisConnections = [];
// Allows overriding
if (!function_exists('getConsoleDB')) {
/**
@@ -60,11 +62,7 @@ if (!function_exists('getConsoleDB')) {
[$cache, $reclaimCache] = getCache();
$database = new Database($dbAdapter, $cache);
$database
->setNamespace('_console')
->setMetadata('host', \gethostname())
->setMetadata('project', '_console');
$database->setNamespace('_console');
return [$database, function () use ($dbConnection, $reclaimCache) {
$dbConnection->reclaim();
@@ -100,11 +98,7 @@ if (!function_exists('getProjectDB')) {
[$cache, $reclaimCache] = getCache();
$database = new Database($dbAdapter, $cache);
$database
->setNamespace('_' . $project->getInternalId())
->setMetadata('host', \gethostname())
->setMetadata('project', $project->getId());
$database->setNamespace('_' . $project->getInternalId());
return [$database, function () use ($dbConnection, $reclaimCache) {
$dbConnection->reclaim();
@@ -325,7 +319,7 @@ $server->onStart(function () use ($stats, $register, $containerId, &$statsDocume
});
});
$server->onWorkerStart(function (int $workerId) use ($server, $register, $stats, $realtime) {
$server->onWorkerStart(function (int $workerId) use ($server, $register, $stats, $realtime, &$redisConnections) {
Console::success('Worker ' . $workerId . ' started successfully');
$attempts = 0;
@@ -436,6 +430,8 @@ $server->onWorkerStart(function (int $workerId) use ($server, $register, $stats,
*/
[$redis, $reclaimForRedis] = getPubSub();
$redisConnections[$workerId] = [$redis, $reclaimForRedis];
$redis->setOption(Redis::OPT_READ_TIMEOUT, -1);
if ($redis->ping(true)) {
@@ -514,9 +510,6 @@ $server->onWorkerStart(function (int $workerId) use ($server, $register, $stats,
sleep(DATABASE_RECONNECT_SLEEP);
continue;
} finally {
if (isset($reclaimForRedis)) {
$reclaimForRedis();
}
if (isset($reclaimForConsole)) {
$reclaimForConsole();
}
@@ -529,6 +522,20 @@ $server->onWorkerStart(function (int $workerId) use ($server, $register, $stats,
Console::error('Failed to restart pub/sub...');
});
$server->onWorkerStop(function (int $workerId) use ($redisConnections) {
/**
* @var Redis $redis
* @var callable $reclaim
*/
[$redis, $reclaim] = $redisConnections[$workerId] ?? null;
$redis?->unsubscribe(['realtime']);
if ($reclaim) {
$reclaim();
}
});
$server->onOpen(function (int $connection, SwooleRequest $request) use ($server, $register, $stats, &$realtime) {
$app = new App('UTC');
$request = new Request($request);
+1 -1
View File
@@ -69,7 +69,7 @@
"utopia-php/storage": "0.18.*",
"utopia-php/swoole": "0.5.*",
"utopia-php/vcs": "0.6.*",
"utopia-php/websocket": "0.1.*",
"utopia-php/websocket": "dev-feat-worker-stop",
"matomo/device-detector": "6.1.*",
"dragonmantank/cron-expression": "3.3.2",
"phpmailer/phpmailer": "6.8.0",
Generated
+12 -18
View File
@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "e90287b00089ad8e8059e6e157f3a6c8",
"content-hash": "ce7a7f429d68836b8667e2b02440fc3f",
"packages": [
{
"name": "adhocore/jwt",
@@ -2273,22 +2273,24 @@
},
{
"name": "utopia-php/websocket",
"version": "0.1.0",
"version": "dev-feat-worker-stop",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/websocket.git",
"reference": "51fcb86171400d8aa40d76c54593481fd273dab5"
"reference": "b7adfab69d4c48a60272fa4cb3328f9b400b0ffa"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/websocket/zipball/51fcb86171400d8aa40d76c54593481fd273dab5",
"reference": "51fcb86171400d8aa40d76c54593481fd273dab5",
"url": "https://api.github.com/repos/utopia-php/websocket/zipball/b7adfab69d4c48a60272fa4cb3328f9b400b0ffa",
"reference": "b7adfab69d4c48a60272fa4cb3328f9b400b0ffa",
"shasum": ""
},
"require": {
"php": ">=8.0"
},
"require-dev": {
"laravel/pint": "^1.15",
"phpstan/phpstan": "^1.8",
"phpunit/phpunit": "^9.5.5",
"swoole/ide-helper": "4.6.6",
"textalk/websocket": "1.5.2",
@@ -2305,16 +2307,6 @@
"license": [
"MIT"
],
"authors": [
{
"name": "Eldad Fux",
"email": "eldad@appwrite.io"
},
{
"name": "Torsten Dittmann",
"email": "torsten@appwrite.io"
}
],
"description": "A simple abstraction for WebSocket servers.",
"keywords": [
"framework",
@@ -2325,9 +2317,9 @@
],
"support": {
"issues": "https://github.com/utopia-php/websocket/issues",
"source": "https://github.com/utopia-php/websocket/tree/0.1.0"
"source": "https://github.com/utopia-php/websocket/tree/feat-worker-stop"
},
"time": "2021-12-20T10:50:09+00:00"
"time": "2024-04-09T04:48:45+00:00"
},
{
"name": "webmozart/assert",
@@ -5145,7 +5137,9 @@
],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"stability-flags": {
"utopia-php/websocket": 20
},
"prefer-stable": false,
"prefer-lowest": false,
"platform": {