diff --git a/.env b/.env index 5ea2ba2852..89b76cb740 100644 --- a/.env +++ b/.env @@ -82,7 +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=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 bfdceee027..f79d4cb517 100644 --- a/app/config/variables.php +++ b/app/config/variables.php @@ -1030,13 +1030,22 @@ 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, '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..7ea2fceadc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -732,7 +732,7 @@ 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 appwrite-task-stats-resources: diff --git a/src/Appwrite/Platform/Tasks/Maintenance.php b/src/Appwrite/Platform/Tasks/Maintenance.php index 2df75b22a8..bb815e50bd 100644 --- a/src/Appwrite/Platform/Tasks/Maintenance.php +++ b/src/Appwrite/Platform/Tasks/Maintenance.php @@ -35,12 +35,27 @@ 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 $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();