From 04f14816d78d98f97eee98d4b38d74fa1bc5b0a9 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Wed, 20 Jan 2021 20:58:52 +0530 Subject: [PATCH] feat: add filter to delete execution logs older than --- Dockerfile | 5 ++++- app/tasks/maintenance.php | 44 ++++++++++++++++++++++++++------------- app/workers/deletes.php | 9 ++++---- 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0017d68dcb..d8ab69bbb0 100755 --- a/Dockerfile +++ b/Dockerfile @@ -100,8 +100,11 @@ ENV _APP_SERVER=swoole \ _APP_SETUP=self-hosted \ _APP_VERSION=$VERSION \ _APP_USAGE_STATS=enabled \ + # 14 Days = 1209600 s + _APP_MAINTENANCE_EXECUTION_LOG_RETENTION=1209600 \ + _APP_MAINTENANCE_AUDIT_LOG_RETENTION=1209600 \ # 1 Day = 86400 s - _APP_MAINTENANCE_INTERVAL=86400 + _APP_MAINTENANCE_ABUSE_LOG_RETENTION=86400 \ #ENV _APP_SMTP_SECURE '' #ENV _APP_SMTP_USERNAME '' #ENV _APP_SMTP_PASSWORD '' diff --git a/app/tasks/maintenance.php b/app/tasks/maintenance.php index 34c34cfb83..cc15a4dced 100644 --- a/app/tasks/maintenance.php +++ b/app/tasks/maintenance.php @@ -12,26 +12,27 @@ Console::title('Maintenance V1'); Console::success(APP_NAME.' maintenance process v1 has started'); -function notifyDeleteExecutionLogs() +function notifyDeleteExecutionLogs(int $retention) { Resque::enqueue(Event::DELETE_QUEUE_NAME, Event::DELETE_CLASS_NAME, [ - 'type' => DELETE_TYPE_EXECUTIONS + 'type' => DELETE_TYPE_EXECUTIONS, + 'timestamp' => time() - $retention ]); } -function notifyDeleteAbuseLogs(int $interval) +function notifyDeleteAbuseLogs(int $retention) { Resque::enqueue(Event::DELETE_QUEUE_NAME, Event::DELETE_CLASS_NAME, [ 'type' => DELETE_TYPE_ABUSE, - 'timestamp' => time() - $interval + 'timestamp' => time() - $retention ]); } -function notifyDeleteAuditLogs(int $interval) +function notifyDeleteAuditLogs(int $retention) { Resque::enqueue(Event::DELETE_QUEUE_NAME, Event::DELETE_CLASS_NAME, [ 'type' => DELETE_TYPE_AUDIT, - 'timestamp' => time() - $interval + 'timestamp' => time() - $retention ]); } @@ -40,15 +41,30 @@ $cli ->desc('Schedules maintenance tasks and publishes them to resque') ->action(function () { // # of days in seconds (1 day = 86400s) - $interval = (int) App::getEnv('_APP_MAINTENANCE_INTERVAL', '86400'); + // $executionLogsRetention = (int) App::getEnv('_APP_MAINTENANCE_EXECUTION_LOG_RETENTION', '60'); + $executionLogsRetention = 120; + $abuseLogsRetention = (int) App::getEnv('_APP_MAINTENANCE_ABUSE_LOG_RETENTION', '60'); + $auditLogRetention = (int) App::getEnv('_APP_MAINTENANCE_AUDIT_LOG_RETENTION', '60'); - Console::loop(function() use ($interval){ + // Schedule delete execution logs + Console::loop(function() use ($executionLogsRetention){ $time = date('d-m-Y H:i:s', time()); - Console::info("[{$time}] Notifying deletes workers every {$interval} seconds"); - notifyDeleteExecutionLogs(); - notifyDeleteAbuseLogs($interval); - notifyDeleteAuditLogs($interval); - - }, $interval); + Console::info("[{$time}] Notifying deletes workers every {$executionLogsRetention} seconds"); + notifyDeleteExecutionLogs($executionLogsRetention); + }, $executionLogsRetention); + + // // Schedule delete abuse logs + // Console::loop(function() use ($abuseLogsRetention){ + // $time = date('d-m-Y H:i:s', time()); + // Console::info("[{$time}] Notifying deletes workers every {$abuseLogsRetention} seconds"); + // notifyDeleteAbuseLogs($abuseLogsRetention); + // }, $abuseLogsRetention); + + // // Schedule delete audit logs + // Console::loop(function() use ($auditLogRetention){ + // $time = date('d-m-Y H:i:s', time()); + // Console::info("[{$time}] Notifying deletes workers every {$auditLogRetention} seconds"); + // notifyDeleteAuditLogs($auditLogRetention); + // }, $auditLogRetention); }); \ No newline at end of file diff --git a/app/workers/deletes.php b/app/workers/deletes.php index cf9aa2c453..ac168d6bcb 100644 --- a/app/workers/deletes.php +++ b/app/workers/deletes.php @@ -59,7 +59,7 @@ class DeletesV1 break; case DELETE_TYPE_EXECUTIONS: - $this->deleteExecutionLogs(); + $this->deleteExecutionLogs($this->args['timestamp']); break; case DELETE_TYPE_AUDIT: @@ -121,16 +121,17 @@ class DeletesV1 ], $this->getProjectDB($projectId)); } - protected function deleteExecutionLogs() + protected function deleteExecutionLogs($timestamp) { - $this->deleteForProjectIds(function($projectId) { + $this->deleteForProjectIds(function($projectId) use ($timestamp) { if (!($projectDB = $this->getProjectDB($projectId))) { throw new Exception('Failed to get projectDB for project '.$projectId); } // Delete Executions $this->deleteByGroup([ - '$collection='.Database::SYSTEM_COLLECTION_EXECUTIONS + '$collection='.Database::SYSTEM_COLLECTION_EXECUTIONS, + 'dateCreated<'.$timestamp ], $projectDB); }); }