From fea58c2bc9a4f35d8796a97755760a4415f9461f Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Thu, 8 May 2025 07:49:27 +0000 Subject: [PATCH 1/2] refactor: maintenance task to run at a specific time --- .env | 1 + app/config/variables.php | 9 +++++++++ app/views/install/compose.phtml | 1 + docker-compose.yml | 1 + src/Appwrite/Platform/Tasks/Maintenance.php | 22 ++++++++++++++++++--- 5 files changed, 31 insertions(+), 3 deletions(-) diff --git a/.env b/.env index 5ea2ba2852..a1f8cfafc6 100644 --- a/.env +++ b/.env @@ -83,6 +83,7 @@ _APP_EXECUTOR_HOST=http://proxy/v1 _APP_FUNCTIONS_RUNTIMES=php-8.0,node-18.0,python-3.9,ruby-3.1 _APP_MAINTENANCE_INTERVAL=86400 _APP_MAINTENANCE_DELAY= +_APP_MAINTENANCE_START_TIME= _APP_MAINTENANCE_RETENTION_CACHE=2592000 _APP_MAINTENANCE_RETENTION_EXECUTION=1209600 _APP_MAINTENANCE_RETENTION_ABUSE=86400 diff --git a/app/config/variables.php b/app/config/variables.php index bfdceee027..297f11bd89 100644 --- a/app/config/variables.php +++ b/app/config/variables.php @@ -1037,6 +1037,15 @@ return [ 'question' => '', 'filter' => '' ], + [ + 'name' => '_APP_MAINTENANCE_START_TIME', + 'description' => 'The time of day (in 24-hour format) when the maintenance process should start. The default value is 00:00.', + 'introduction' => '1.6.2', + 'default' => '00:00', + 'required' => false, + 'question' => '', + 'filter' => '' + ], [ 'name' => '_APP_MAINTENANCE_RETENTION_CACHE', 'description' => 'The maximum duration (in seconds) upto which to retain cached files. The default value is 2592000 seconds (30 days).', diff --git a/app/views/install/compose.phtml b/app/views/install/compose.phtml index 25c8c30742..816f88299b 100644 --- a/app/views/install/compose.phtml +++ b/app/views/install/compose.phtml @@ -144,6 +144,7 @@ $image = $this->getParam('image', ''); - _APP_LOGGING_CONFIG - _APP_MAINTENANCE_INTERVAL - _APP_MAINTENANCE_DELAY + - _APP_MAINTENANCE_START_TIME - _APP_MAINTENANCE_RETENTION_EXECUTION - _APP_MAINTENANCE_RETENTION_CACHE - _APP_MAINTENANCE_RETENTION_ABUSE diff --git a/docker-compose.yml b/docker-compose.yml index e8e51805d2..ac6530fd5b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -733,6 +733,7 @@ services: - _APP_MAINTENANCE_RETENTION_USAGE_HOURLY - _APP_MAINTENANCE_RETENTION_SCHEDULES - _APP_MAINTENANCE_DELAY + - _APP_MAINTENANCE_START_TIME - _APP_DATABASE_SHARED_TABLES appwrite-task-stats-resources: diff --git a/src/Appwrite/Platform/Tasks/Maintenance.php b/src/Appwrite/Platform/Tasks/Maintenance.php index 2df75b22a8..83533a7698 100644 --- a/src/Appwrite/Platform/Tasks/Maintenance.php +++ b/src/Appwrite/Platform/Tasks/Maintenance.php @@ -35,12 +35,28 @@ class Maintenance extends Action Console::title('Maintenance V1'); Console::success(APP_NAME . ' maintenance process v1 has started'); - // # of days in seconds (1 day = 86400s) - $interval = (int) System::getEnv('_APP_MAINTENANCE_INTERVAL', '86400'); - $delay = (int) System::getEnv('_APP_MAINTENANCE_DELAY', '0'); + $interval = (int) System::getEnv('_APP_MAINTENANCE_INTERVAL', '86400'); // 1 day + $delay = (int) System::getEnv('_APP_MAINTENANCE_DELAY', '0'); // 0 seconds $usageStatsRetentionHourly = (int) System::getEnv('_APP_MAINTENANCE_RETENTION_USAGE_HOURLY', '8640000'); //100 days $cacheRetention = (int) System::getEnv('_APP_MAINTENANCE_RETENTION_CACHE', '2592000'); // 30 days $schedulesDeletionRetention = (int) System::getEnv('_APP_MAINTENANCE_RETENTION_SCHEDULES', '86400'); // 1 Day + $jobInitTime = System::getEnv('_APP_MAINTENANCE_START_TIME', '00:00'); // (hour:minutes) + + $now = new \DateTime(); + $now->setTimezone(new \DateTimeZone(date_default_timezone_get())); + $next = new \DateTime($now->format("Y-m-d $jobInitTime")); + $next->setTimezone(new \DateTimeZone(date_default_timezone_get())); + $delay = $next->getTimestamp() - $now->getTimestamp(); + + /** + * If time passed for the target day. + */ + if ($delay <= 0) { + $next->add(\DateInterval::createFromDateString('1 days')); + $delay = $next->getTimestamp() - $now->getTimestamp(); + } + + Console::info('Setting loop start time to ' . $next->format("Y-m-d H:i:s.v") . '. Delaying for ' . $delay . ' seconds.'); Console::loop(function () use ($interval, $cacheRetention, $schedulesDeletionRetention, $usageStatsRetentionHourly, $dbForPlatform, $console, $queueForDeletes, $queueForCertificates) { $time = DateTime::now(); From 2c8e1e1731533ad75447c17e982290b0ec0b4e8e Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Thu, 8 May 2025 13:44:58 +0000 Subject: [PATCH 2/2] chore: deprecate delay --- .env | 3 +-- app/config/variables.php | 2 +- docker-compose.yml | 1 - src/Appwrite/Platform/Tasks/Maintenance.php | 1 - 4 files changed, 2 insertions(+), 5 deletions(-) diff --git a/.env b/.env index a1f8cfafc6..89b76cb740 100644 --- a/.env +++ b/.env @@ -82,8 +82,7 @@ _APP_EXECUTOR_SECRET=your-secret-key _APP_EXECUTOR_HOST=http://proxy/v1 _APP_FUNCTIONS_RUNTIMES=php-8.0,node-18.0,python-3.9,ruby-3.1 _APP_MAINTENANCE_INTERVAL=86400 -_APP_MAINTENANCE_DELAY= -_APP_MAINTENANCE_START_TIME= +_APP_MAINTENANCE_START_TIME=12:00 _APP_MAINTENANCE_RETENTION_CACHE=2592000 _APP_MAINTENANCE_RETENTION_EXECUTION=1209600 _APP_MAINTENANCE_RETENTION_ABUSE=86400 diff --git a/app/config/variables.php b/app/config/variables.php index 297f11bd89..f79d4cb517 100644 --- a/app/config/variables.php +++ b/app/config/variables.php @@ -1030,7 +1030,7 @@ return [ ], [ 'name' => '_APP_MAINTENANCE_DELAY', - 'description' => 'Delay value containing the number of seconds that the Appwrite maintenance process should wait before executing system cleanups and optimizations. The default value is 0 seconds.', + 'description' => 'Deprecated with 1.6.2 use _APP_MAINTENANCE_START_TIME instead to run the maintenance at a specific time per day.', 'introduction' => '1.5.0', 'default' => '0', 'required' => false, diff --git a/docker-compose.yml b/docker-compose.yml index ac6530fd5b..7ea2fceadc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -732,7 +732,6 @@ services: - _APP_MAINTENANCE_RETENTION_AUDIT_CONSOLE - _APP_MAINTENANCE_RETENTION_USAGE_HOURLY - _APP_MAINTENANCE_RETENTION_SCHEDULES - - _APP_MAINTENANCE_DELAY - _APP_MAINTENANCE_START_TIME - _APP_DATABASE_SHARED_TABLES diff --git a/src/Appwrite/Platform/Tasks/Maintenance.php b/src/Appwrite/Platform/Tasks/Maintenance.php index 83533a7698..bb815e50bd 100644 --- a/src/Appwrite/Platform/Tasks/Maintenance.php +++ b/src/Appwrite/Platform/Tasks/Maintenance.php @@ -36,7 +36,6 @@ class Maintenance extends Action Console::success(APP_NAME . ' maintenance process v1 has started'); $interval = (int) System::getEnv('_APP_MAINTENANCE_INTERVAL', '86400'); // 1 day - $delay = (int) System::getEnv('_APP_MAINTENANCE_DELAY', '0'); // 0 seconds $usageStatsRetentionHourly = (int) System::getEnv('_APP_MAINTENANCE_RETENTION_USAGE_HOURLY', '8640000'); //100 days $cacheRetention = (int) System::getEnv('_APP_MAINTENANCE_RETENTION_CACHE', '2592000'); // 30 days $schedulesDeletionRetention = (int) System::getEnv('_APP_MAINTENANCE_RETENTION_SCHEDULES', '86400'); // 1 Day