1
0
Fork 0
mirror of synced 2024-07-10 08:56:25 +12:00

Add try/catch/reclaim around sending stats

This commit is contained in:
Jake Barnby 2024-04-08 14:55:41 +12:00
parent fc4cbb8174
commit 899bba64d1
No known key found for this signature in database
GPG key ID: C437A8CC85B96E9C

View file

@ -252,15 +252,16 @@ $server->onStart(function () use ($stats, $register, $containerId, &$statsDocume
go(function () use ($register, $containerId, &$statsDocument) {
$attempts = 0;
do {
try {
/**
* @var Database $database
* @var callable $reclaim
*/
[$database, $reclaim] = getConsoleDB();
do {
try {
$attempts++;
$document = new Document([
'$id' => ID::unique(),
'$collection' => ID::custom('realtime'),
@ -270,7 +271,9 @@ $server->onStart(function () use ($stats, $register, $containerId, &$statsDocume
'value' => '{}'
]);
$statsDocument = Authorization::skip(fn() => $database->createDocument('realtime', $document));
$statsDocument = Authorization::skip(function () use ($database, $document) {
return $database->createDocument('realtime', $document);
});
break;
} catch (Throwable) {
@ -279,7 +282,9 @@ $server->onStart(function () use ($stats, $register, $containerId, &$statsDocume
}
} while (true);
if (isset($reclaim)) {
$reclaim();
}
});
/**
@ -331,6 +336,7 @@ $server->onWorkerStart(function (int $workerId) use ($server, $register, $stats,
* Sending current connections to project channels on the console project every 5 seconds.
*/
if ($realtime->hasSubscriber('console', Role::users()->toString(), 'project')) {
try {
/**
* @var Database $database
* @var callable $reclaim
@ -339,9 +345,11 @@ $server->onWorkerStart(function (int $workerId) use ($server, $register, $stats,
$payload = [];
$list = Authorization::skip(fn() => $database->find('realtime', [
$list = Authorization::skip(function () use ($database) {
return $database->find('realtime', [
Query::greaterThan('timestamp', DateTime::addSeconds(new \DateTime(), -15)),
]));
]);
});
/**
* Aggregate stats across containers.
@ -379,9 +387,14 @@ $server->onWorkerStart(function (int $workerId) use ($server, $register, $stats,
'data' => $event['data']
]));
}
} catch (Throwable $th) {
logError($th, 'sendStats');
} finally {
if (isset($reclaim)) {
$reclaim();
}
}
}
/**
* Sending test message for SDK E2E tests every 5 seconds.
@ -450,7 +463,9 @@ $server->onWorkerStart(function (int $workerId) use ($server, $register, $stats,
*/
[$dbForConsole, $reclaimForConsole] = getConsoleDB();
$project = Authorization::skip(fn() => $dbForConsole->getDocument('projects', $projectId));
$project = Authorization::skip(function () use ($dbForConsole, $projectId) {
return $dbForConsole->getDocument('projects', $projectId);
});
[$dbForProject, $reclaimForProject] = getProjectDB($project);
@ -521,9 +536,15 @@ $server->onOpen(function (int $connection, SwooleRequest $request) use ($server,
Console::info("Connection open (user: {$connection})");
App::setResource('pools', fn() => $register->get('pools'));
App::setResource('request', fn() => $request);
App::setResource('response', fn() => $response);
App::setResource('pools', function () use ($register) {
return $register->get('pools');
});
App::setResource('request', function () use ($request) {
return $request;
});
App::setResource('response', function () use ($response) {
return $response;
});
try {
/** @var Document $project */
@ -634,7 +655,10 @@ $server->onMessage(function (int $connection, string $message) use ($server, $re
[$database, $reclaimForConsole] = getConsoleDB();
if ($projectId !== 'console') {
$project = Authorization::skip(fn() => $database->getDocument('projects', $projectId));
$project = Authorization::skip(function () use ($database, $projectId) {
return $database->getDocument('projects', $projectId);
});
[$database, $reclaimForProject] = getProjectDB($project);
} else {
$project = null;