Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exteand deletes worker #8274

Open
wants to merge 16 commits into
base: refactor-usage-sn
Choose a base branch
from
Open
29 changes: 15 additions & 14 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 40 additions & 30 deletions src/Appwrite/Platform/Workers/Deletes.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function action(Message $message, Database $dbForConsole, callable $getPr
* @throws Structure
* @throws DatabaseException
*/
private function deleteSchedules(Database $dbForConsole, callable $getProjectDB, string $datetime): void
protected function deleteSchedules(Database $dbForConsole, callable $getProjectDB, string $datetime): void
{
$this->listByGroup(
'schedules',
Expand Down Expand Up @@ -228,7 +228,7 @@ function (Document $document) use ($dbForConsole, $getProjectDB) {
* @param Document $topic
* @throws Exception
*/
private function deleteTopic(Document $project, callable $getProjectDB, Document $topic)
protected function deleteTopic(Document $project, callable $getProjectDB, Document $topic)
{
if ($topic->isEmpty()) {
Console::error('Failed to delete subscribers. Topic not found');
Expand All @@ -250,7 +250,7 @@ private function deleteTopic(Document $project, callable $getProjectDB, Document
* @param Document $target
* @throws Exception
*/
private function deleteTargetSubscribers(Document $project, callable $getProjectDB, Document $target): void
protected function deleteTargetSubscribers(Document $project, callable $getProjectDB, Document $target): void
{
/** @var Database */
$dbForProject = $getProjectDB($project);
Expand Down Expand Up @@ -291,7 +291,7 @@ function (Document $subscriber) use ($dbForProject, $target) {
* @return void
* @throws Exception
*/
private function deleteExpiredTargets(Document $project, callable $getProjectDB): void
protected function deleteExpiredTargets(Document $project, callable $getProjectDB): void
{
$this->deleteByGroup(
'targets',
Expand All @@ -305,7 +305,7 @@ function (Document $target) use ($getProjectDB, $project) {
);
}

private function deleteSessionTargets(Document $project, callable $getProjectDB, Document $session): void
protected function deleteSessionTargets(Document $project, callable $getProjectDB, Document $session): void
{
$this->deleteByGroup(
'targets',
Expand All @@ -328,7 +328,7 @@ function (Document $target) use ($getProjectDB, $project) {
* @param string|null $resourceType
* @throws Exception
*/
private function deleteCacheByResource(Document $project, callable $getProjectDB, string $resource, string $resourceType = null): void
protected function deleteCacheByResource(Document $project, callable $getProjectDB, string $resource, string $resourceType = null): void
{
$projectId = $project->getId();
$dbForProject = $getProjectDB($project);
Expand Down Expand Up @@ -366,7 +366,7 @@ function (Document $document) use ($cache, $projectId) {
* @return void
* @throws Exception
*/
private function deleteCacheByDate(Document $project, callable $getProjectDB, string $datetime): void
protected function deleteCacheByDate(Document $project, callable $getProjectDB, string $datetime): void
{
$projectId = $project->getId();
$dbForProject = $getProjectDB($project);
Expand Down Expand Up @@ -402,7 +402,7 @@ function (Document $document) use ($cache, $projectId) {
* @return void
* @throws Exception
*/
private function deleteUsageStats(Document $project, callable $getProjectDB, string $hourlyUsageRetentionDatetime): void
protected function deleteUsageStats(Document $project, callable $getProjectDB, string $hourlyUsageRetentionDatetime): void
{
$dbForProject = $getProjectDB($project);
// Delete Usage stats
Expand All @@ -419,7 +419,7 @@ private function deleteUsageStats(Document $project, callable $getProjectDB, str
* @return void
* @throws Exception
*/
private function deleteMemberships(callable $getProjectDB, Document $document, Document $project): void
protected function deleteMemberships(callable $getProjectDB, Document $document, Document $project): void
{
$dbForProject = $getProjectDB($project);
$teamInternalId = $document->getInternalId();
Expand Down Expand Up @@ -448,7 +448,7 @@ function (Document $membership) use ($dbForProject) {
* @throws Restricted
* @throws Structure
*/
private function deleteProjectsByTeam(Database $dbForConsole, callable $getProjectDB, Device $deviceForFiles, Device $deviceForFunctions, Device $deviceForBuilds, Device $deviceForCache, Document $document): void
protected function deleteProjectsByTeam(Database $dbForConsole, callable $getProjectDB, Device $deviceForFiles, Device $deviceForFunctions, Device $deviceForBuilds, Device $deviceForCache, Document $document): void
{

$projects = $dbForConsole->find('projects', [
Expand All @@ -473,7 +473,7 @@ private function deleteProjectsByTeam(Database $dbForConsole, callable $getProje
* @throws Authorization
* @throws DatabaseException
*/
private function deleteProject(Database $dbForConsole, callable $getProjectDB, Device $deviceForFiles, Device $deviceForFunctions, Device $deviceForBuilds, Device $deviceForCache, Document $document): void
protected function deleteProject(Database $dbForConsole, callable $getProjectDB, Device $deviceForFiles, Device $deviceForFunctions, Device $deviceForBuilds, Device $deviceForCache, Document $document): void
{
$projectInternalId = $document->getInternalId();

Expand Down Expand Up @@ -574,8 +574,9 @@ private function deleteProject(Database $dbForConsole, callable $getProjectDB, D
* @return void
* @throws Exception
*/
private function deleteUser(callable $getProjectDB, Document $document, Document $project): void
protected function deleteUser(callable $getProjectDB, Document $document, Document $project): void
{

$userId = $document->getId();
$userInternalId = $document->getInternalId();
$dbForProject = $getProjectDB($project);
Expand All @@ -586,15 +587,24 @@ private function deleteUser(callable $getProjectDB, Document $document, Document
], $dbForProject);

$dbForProject->purgeCachedDocument('users', $userId);

var_dump('in delete worker');
// Delete Memberships and decrement team membership counts
$this->deleteByGroup('memberships', [
Query::equal('userInternalId', [$userInternalId])
], $dbForProject, function (Document $document) use ($dbForProject) {
var_dump($document->getAttribute('confirm'));
if ($document->getAttribute('confirm')) { // Count only confirmed members
$teamId = $document->getAttribute('teamId');
$team = $dbForProject->getDocument('teams', $teamId);
if (!$team->isEmpty()) {
$total = $document->getAttribute('total');
var_dump('$total='.$total);
// Delete the team if the user is the last membership
if ($total === 1) {
$this->deleteById($team, $dbForProject);
var_dump('delete team');
return;
}
$dbForProject->decreaseDocumentAttribute('teams', $teamId, 'total', 1, 0);
}
}
Expand Down Expand Up @@ -630,7 +640,7 @@ function (Document $target) use ($getProjectDB, $project) {
* @return void
* @throws Exception
*/
private function deleteExecutionLogs(Document $project, callable $getProjectDB, string $datetime): void
protected function deleteExecutionLogs(Document $project, callable $getProjectDB, string $datetime): void
{
$dbForProject = $getProjectDB($project);
// Delete Executions
Expand All @@ -645,7 +655,7 @@ private function deleteExecutionLogs(Document $project, callable $getProjectDB,
* @return void
* @throws Exception|Throwable
*/
private function deleteExpiredSessions(Document $project, callable $getProjectDB): void
protected function deleteExpiredSessions(Document $project, callable $getProjectDB): void
{
$dbForProject = $getProjectDB($project);
$duration = $project->getAttribute('auths', [])['duration'] ?? Auth::TOKEN_EXPIRATION_LOGIN_LONG;
Expand All @@ -663,7 +673,7 @@ private function deleteExpiredSessions(Document $project, callable $getProjectDB
* @return void
* @throws Exception
*/
private function deleteRealtimeUsage(Database $dbForConsole, string $datetime): void
protected function deleteRealtimeUsage(Database $dbForConsole, string $datetime): void
{
// Delete Dead Realtime Logs
$this->deleteByGroup('realtime', [
Expand All @@ -678,7 +688,7 @@ private function deleteRealtimeUsage(Database $dbForConsole, string $datetime):
* @return void
* @throws Exception
*/
private function deleteAbuseLogs(Document $project, callable $getProjectDB, string $abuseRetention): void
protected function deleteAbuseLogs(Document $project, callable $getProjectDB, string $abuseRetention): void
{
$projectId = $project->getId();
$dbForProject = $getProjectDB($project);
Expand All @@ -699,7 +709,7 @@ private function deleteAbuseLogs(Document $project, callable $getProjectDB, stri
* @return void
* @throws Exception
*/
private function deleteAuditLogs(Document $project, callable $getProjectDB, string $auditRetention): void
protected function deleteAuditLogs(Document $project, callable $getProjectDB, string $auditRetention): void
{
$projectId = $project->getId();
$dbForProject = $getProjectDB($project);
Expand All @@ -719,7 +729,7 @@ private function deleteAuditLogs(Document $project, callable $getProjectDB, stri
* @return void
* @throws Exception
*/
private function deleteAuditLogsByResource(callable $getProjectDB, string $resource, Document $project): void
protected function deleteAuditLogsByResource(callable $getProjectDB, string $resource, Document $project): void
{
$dbForProject = $getProjectDB($project);

Expand All @@ -737,7 +747,7 @@ private function deleteAuditLogsByResource(callable $getProjectDB, string $resou
* @return void
* @throws Exception
*/
private function deleteFunction(Database $dbForConsole, callable $getProjectDB, Device $deviceForFunctions, Device $deviceForBuilds, Document $document, Document $project): void
protected function deleteFunction(Database $dbForConsole, callable $getProjectDB, Device $deviceForFunctions, Device $deviceForBuilds, Document $document, Document $project): void
{
$projectId = $project->getId();
$dbForProject = $getProjectDB($project);
Expand Down Expand Up @@ -828,7 +838,7 @@ private function deleteFunction(Database $dbForConsole, callable $getProjectDB,
* @param Document $deployment
* @return void
*/
private function deleteDeploymentFiles(Device $device, Document $deployment): void
protected function deleteDeploymentFiles(Device $device, Document $deployment): void
{
$deploymentId = $deployment->getId();
$deploymentPath = $deployment->getAttribute('path', '');
Expand Down Expand Up @@ -860,7 +870,7 @@ private function deleteDeploymentFiles(Device $device, Document $deployment): vo
* @param Document $build
* @return void
*/
private function deleteBuildFiles(Device $device, Document $build): void
protected function deleteBuildFiles(Device $device, Document $build): void
{
$buildId = $build->getId();
$buildPath = $build->getAttribute('path', '');
Expand Down Expand Up @@ -894,7 +904,7 @@ private function deleteBuildFiles(Device $device, Document $build): void
* @return void
* @throws Exception
*/
private function deleteDeployment(callable $getProjectDB, Device $deviceForFunctions, Device $deviceForBuilds, Document $document, Document $project): void
protected function deleteDeployment(callable $getProjectDB, Device $deviceForFunctions, Device $deviceForBuilds, Document $document, Document $project): void
{
$projectId = $project->getId();
$dbForProject = $getProjectDB($project);
Expand Down Expand Up @@ -930,7 +940,7 @@ private function deleteDeployment(callable $getProjectDB, Device $deviceForFunct
* @param callable|null $callback to perform after document is deleted
* @return void
*/
private function deleteById(Document $document, Database $database, callable $callback = null): void
protected function deleteById(Document $document, Database $database, callable $callback = null): void
{
if ($database->deleteDocument($document->getCollection(), $document->getId())) {
Console::success('Deleted document "' . $document->getId() . '" successfully');
Expand All @@ -951,7 +961,7 @@ private function deleteById(Document $document, Database $database, callable $ca
* @return void
* @throws Exception
*/
private function deleteByGroup(string $collection, array $queries, Database $database, callable $callback = null): void
protected function deleteByGroup(string $collection, array $queries, Database $database, callable $callback = null): void
{
$count = 0;
$chunk = 0;
Expand Down Expand Up @@ -993,7 +1003,7 @@ private function deleteByGroup(string $collection, array $queries, Database $dat
* @return void
* @throws Exception
*/
private function listByGroup(string $collection, array $queries, Database $database, callable $callback = null): void
protected function listByGroup(string $collection, array $queries, Database $database, callable $callback = null): void
{
$count = 0;
$chunk = 0;
Expand Down Expand Up @@ -1039,7 +1049,7 @@ private function listByGroup(string $collection, array $queries, Database $datab
* @param Document $document rule document
* @return void
*/
private function deleteRule(Database $dbForConsole, Document $document): void
protected function deleteRule(Database $dbForConsole, Document $document): void
{

$domain = $document->getAttribute('domain');
Expand Down Expand Up @@ -1068,7 +1078,7 @@ private function deleteRule(Database $dbForConsole, Document $document): void
* @param Document $project
* @return void
*/
private function deleteBucket(callable $getProjectDB, Device $deviceForFiles, Document $document, Document $project): void
protected function deleteBucket(callable $getProjectDB, Device $deviceForFiles, Document $document, Document $project): void
{
$dbForProject = $getProjectDB($project);

Expand All @@ -1085,7 +1095,7 @@ private function deleteBucket(callable $getProjectDB, Device $deviceForFiles, Do
* @return void
* @throws Exception
*/
private function deleteInstallation(Database $dbForConsole, callable $getProjectDB, Document $document, Document $project): void
protected function deleteInstallation(Database $dbForConsole, callable $getProjectDB, Document $document, Document $project): void
{
$dbForProject = $getProjectDB($project);

Expand Down Expand Up @@ -1114,7 +1124,7 @@ private function deleteInstallation(Database $dbForConsole, callable $getProject
* @return void
* @throws Exception
*/
private function deleteRuntimes(callable $getProjectDB, ?Document $function, Document $project): void
protected function deleteRuntimes(callable $getProjectDB, ?Document $function, Document $project): void
{
$executor = new Executor(System::getEnv('_APP_EXECUTOR_HOST'));

Expand Down