Merge pull request #2427 from appwrite/feat-improve-deletes-worker
fix(deletes): add realtime logs and improve code
This commit is contained in:
commit
446827732d
|
@ -34,8 +34,7 @@ class DeletesV1 extends Worker
|
|||
|
||||
switch (strval($type)) {
|
||||
case DELETE_TYPE_DOCUMENT:
|
||||
$document = $this->args['document'] ?? [];
|
||||
$document = new Document($document);
|
||||
$document = new Document($this->args['document'] ?? []);
|
||||
|
||||
switch ($document->getCollection()) {
|
||||
case DELETE_TYPE_COLLECTIONS:
|
||||
|
@ -72,8 +71,7 @@ class DeletesV1 extends Worker
|
|||
break;
|
||||
|
||||
case DELETE_TYPE_REALTIME:
|
||||
//$this->deleteRealtimeUsage($this->args['timestamp']);
|
||||
//TODO: implement this
|
||||
$this->deleteRealtimeUsage($this->args['timestamp']);
|
||||
break;
|
||||
|
||||
case DELETE_TYPE_CERTIFICATES:
|
||||
|
@ -120,12 +118,10 @@ class DeletesV1 extends Worker
|
|||
* @param int $timestamp1d
|
||||
* @param int $timestamp30m
|
||||
*/
|
||||
protected function deleteUsageStats(int $timestamp1d, int $timestamp30m) {
|
||||
$this->deleteForProjectIds(function($projectId) use ($timestamp1d, $timestamp30m) {
|
||||
if (!($dbForInternal = $this->getInternalDB($projectId))) {
|
||||
throw new Exception('Failed to get projectDB for project '.$projectId);
|
||||
}
|
||||
|
||||
protected function deleteUsageStats(int $timestamp1d, int $timestamp30m)
|
||||
{
|
||||
$this->deleteForProjectIds(function (string $projectId) use ($timestamp1d, $timestamp30m) {
|
||||
$dbForInternal = $this->getInternalDB($projectId);
|
||||
// Delete Usage stats
|
||||
$this->deleteByGroup('stats', [
|
||||
new Query('time', Query::TYPE_LESSER, [$timestamp1d]),
|
||||
|
@ -159,6 +155,7 @@ class DeletesV1 extends Worker
|
|||
protected function deleteProject(Document $document): void
|
||||
{
|
||||
$projectId = $document->getId();
|
||||
|
||||
// Delete all DBs
|
||||
$this->getExternalDB($projectId)->delete();
|
||||
$this->getInternalDB($projectId)->delete();
|
||||
|
@ -183,7 +180,7 @@ class DeletesV1 extends Worker
|
|||
// Delete Memberships and decrement team membership counts
|
||||
$this->deleteByGroup('memberships', [
|
||||
new Query('userId', Query::TYPE_EQUAL, [$userId])
|
||||
], $this->getInternalDB($projectId), function(Document $document) use ($projectId, $userId) {
|
||||
], $this->getInternalDB($projectId), function (Document $document) use ($projectId) {
|
||||
|
||||
if ($document->getAttribute('confirm')) { // Count only confirmed members
|
||||
$teamId = $document->getAttribute('teamId');
|
||||
|
@ -203,10 +200,7 @@ class DeletesV1 extends Worker
|
|||
protected function deleteExecutionLogs(int $timestamp): void
|
||||
{
|
||||
$this->deleteForProjectIds(function (string $projectId) use ($timestamp) {
|
||||
if (!($dbForInternal = $this->getInternalDB($projectId))) {
|
||||
throw new Exception('Failed to get projectDB for project '.$projectId);
|
||||
}
|
||||
|
||||
$dbForInternal = $this->getInternalDB($projectId);
|
||||
// Delete Executions
|
||||
$this->deleteByGroup('executions', [
|
||||
new Query('dateCreated', Query::TYPE_LESSER, [$timestamp])
|
||||
|
@ -214,6 +208,20 @@ class DeletesV1 extends Worker
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $timestamp
|
||||
*/
|
||||
protected function deleteRealtimeUsage(int $timestamp): void
|
||||
{
|
||||
$this->deleteForProjectIds(function (string $projectId) use ($timestamp) {
|
||||
$dbForInternal = $this->getInternalDB($projectId);
|
||||
// Delete Dead Realtime Logs
|
||||
$this->deleteByGroup('realtime', [
|
||||
new Query('timestamp', Query::TYPE_LESSER, [$timestamp])
|
||||
], $dbForInternal);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $timestamp
|
||||
*/
|
||||
|
@ -223,8 +231,9 @@ class DeletesV1 extends Worker
|
|||
throw new Exception('Failed to delete audit logs. No timestamp provided');
|
||||
}
|
||||
|
||||
$this->deleteForProjectIds(function($projectId) use ($timestamp){
|
||||
$timeLimit = new TimeLimit("", 0, 1, $this->getInternalDB($projectId));
|
||||
$this->deleteForProjectIds(function (string $projectId) use ($timestamp) {
|
||||
$dbForInternal = $this->getInternalDB($projectId);
|
||||
$timeLimit = new TimeLimit("", 0, 1, $dbForInternal);
|
||||
$abuse = new Abuse($timeLimit);
|
||||
|
||||
$status = $abuse->cleanup($timestamp);
|
||||
|
@ -242,8 +251,9 @@ class DeletesV1 extends Worker
|
|||
if ($timestamp == 0) {
|
||||
throw new Exception('Failed to delete audit logs. No timestamp provided');
|
||||
}
|
||||
$this->deleteForProjectIds(function($projectId) use ($timestamp){
|
||||
$audit = new Audit($this->getInternalDB($projectId));
|
||||
$this->deleteForProjectIds(function (string $projectId) use ($timestamp) {
|
||||
$dbForInternal = $this->getInternalDB($projectId);
|
||||
$audit = new Audit($dbForInternal);
|
||||
$status = $audit->cleanup($timestamp);
|
||||
if (!$status) {
|
||||
throw new Exception('Failed to delete Audit logs for project' . $projectId);
|
||||
|
@ -326,9 +336,8 @@ class DeletesV1 extends Worker
|
|||
|
||||
$chunk++;
|
||||
|
||||
$projectIds = array_map (function ($project) {
|
||||
return $project->getId();
|
||||
}, $projects);
|
||||
/** @var string[] $projectIds */
|
||||
$projectIds = array_map(fn(Document $project) => $project->getId(), $projects);
|
||||
|
||||
$sum = count($projects);
|
||||
|
||||
|
|
Loading…
Reference in a new issue