1
0
Fork 0
mirror of synced 2024-09-09 22:27:06 +12:00
appwrite/app/tasks/maintenance.php

127 lines
4.7 KiB
PHP
Raw Normal View History

2020-12-15 05:39:44 +13:00
<?php
global $cli;
use Appwrite\Auth\Auth;
2022-08-25 04:28:04 +12:00
use Appwrite\Database\DatabasePool;
use Appwrite\Event\Certificate;
2022-04-20 01:13:55 +12:00
use Appwrite\Event\Delete;
2020-12-15 05:39:44 +13:00
use Utopia\App;
use Utopia\CLI\Console;
use Utopia\Database\Document;
2022-04-10 21:38:22 +12:00
use Utopia\Database\Query;
2020-12-15 05:39:44 +13:00
$cli
->task('maintenance')
->desc('Schedules maintenance tasks and publishes them to resque')
->action(function () {
2022-07-16 08:58:15 +12:00
global $register;
2021-01-27 09:49:13 +13:00
Console::title('Maintenance V1');
Console::success(APP_NAME . ' maintenance process v1 has started');
2021-01-27 09:49:13 +13:00
function notifyDeleteExecutionLogs(int $interval)
{
2022-04-20 01:13:55 +12:00
(new Delete())
->setType(DELETE_TYPE_EXECUTIONS)
->setTimestamp(time() - $interval)
->trigger();
2021-01-27 09:49:13 +13:00
}
function notifyDeleteAbuseLogs(int $interval)
2021-01-27 09:49:13 +13:00
{
2022-04-20 01:13:55 +12:00
(new Delete())
->setType(DELETE_TYPE_ABUSE)
->setTimestamp(time() - $interval)
->trigger();
2021-01-27 09:49:13 +13:00
}
function notifyDeleteAuditLogs(int $interval)
2021-01-27 09:49:13 +13:00
{
2022-04-20 01:13:55 +12:00
(new Delete())
->setType(DELETE_TYPE_AUDIT)
->setTimestamp(time() - $interval)
->trigger();
2021-01-27 09:49:13 +13:00
}
function notifyDeleteUsageStats(int $interval30m, int $interval1d)
2021-08-27 23:37:52 +12:00
{
2022-04-20 01:13:55 +12:00
(new Delete())
->setType(DELETE_TYPE_USAGE)
->setTimestamp1d(time() - $interval1d)
->setTimestamp30m(time() - $interval30m)
->trigger();
2021-08-27 23:37:52 +12:00
}
function notifyDeleteConnections()
{
2022-04-20 01:13:55 +12:00
(new Delete())
->setType(DELETE_TYPE_REALTIME)
->setTimestamp(time() - 60)
->trigger();
}
function notifyDeleteExpiredSessions()
{
(new Delete())
->setType(DELETE_TYPE_SESSIONS)
->setTimestamp(time() - Auth::TOKEN_EXPIRATION_LOGIN_LONG)
->trigger();
}
2022-04-10 21:38:22 +12:00
function renewCertificates($dbForConsole)
{
$time = date('d-m-Y H:i:s', time());
$certificates = $dbForConsole->find('certificates', [
2022-04-14 00:26:07 +12:00
new Query('attempts', Query::TYPE_LESSEREQUAL, [5]), // Maximum 5 attempts
2022-04-10 21:38:22 +12:00
new Query('renewDate', Query::TYPE_LESSEREQUAL, [\time()]) // includes 60 days cooldown (we have 30 days to renew)
2022-04-11 19:56:58 +12:00
], 200); // Limit 200 comes from LetsEncrypt (300 orders per 3 hours, keeping some for new domains)
2022-04-10 21:38:22 +12:00
if (\count($certificates) > 0) {
2022-04-10 21:38:22 +12:00
Console::info("[{$time}] Found " . \count($certificates) . " certificates for renewal, scheduling jobs.");
$event = new Certificate();
2022-04-10 21:38:22 +12:00
foreach ($certificates as $certificate) {
$event
->setDomain(new Document([
'domain' => $certificate->getAttribute('domain')
]))
->trigger();
2022-04-10 21:38:22 +12:00
}
} else {
Console::info("[{$time}] No certificates for renewal.");
}
}
2020-12-18 23:48:00 +13:00
// # of days in seconds (1 day = 86400s)
2021-01-21 06:57:49 +13:00
$interval = (int) App::getEnv('_APP_MAINTENANCE_INTERVAL', '86400');
2021-01-21 07:23:48 +13:00
$executionLogsRetention = (int) App::getEnv('_APP_MAINTENANCE_RETENTION_EXECUTION', '1209600');
$auditLogRetention = (int) App::getEnv('_APP_MAINTENANCE_RETENTION_AUDIT', '1209600');
$abuseLogsRetention = (int) App::getEnv('_APP_MAINTENANCE_RETENTION_ABUSE', '86400');
$usageStatsRetention30m = (int) App::getEnv('_APP_MAINTENANCE_RETENTION_USAGE_30M', '129600'); //36 hours
2021-08-27 23:37:52 +12:00
$usageStatsRetention1d = (int) App::getEnv('_APP_MAINTENANCE_RETENTION_USAGE_1D', '8640000'); // 100 days
2020-12-15 10:26:37 +13:00
2022-07-16 08:58:15 +12:00
Console::loop(function () use ($register, $interval, $executionLogsRetention, $abuseLogsRetention, $auditLogRetention, $usageStatsRetention30m, $usageStatsRetention1d) {
$redis = $register->get('cache');
2022-08-25 04:28:04 +12:00
$dbPool = $register->get('dbPool');
2022-08-25 05:35:10 +12:00
2022-08-25 04:28:04 +12:00
$database = $dbPool->getConsoleDB();
$pdo = $dbPool->getPDO($database);
$database = DatabasePool::wait(
DatabasePool::getDatabase($pdo, $redis, '_console'),
'certificates',
);
2022-05-12 01:11:58 +12:00
2021-01-17 19:17:19 +13:00
$time = date('d-m-Y H:i:s', time());
2022-05-12 01:11:58 +12:00
Console::info("[{$time}] Notifying workers with maintenance tasks every {$interval} seconds");
notifyDeleteExecutionLogs($executionLogsRetention);
2021-01-21 06:57:49 +13:00
notifyDeleteAbuseLogs($abuseLogsRetention);
notifyDeleteAuditLogs($auditLogRetention);
2021-09-02 18:32:32 +12:00
notifyDeleteUsageStats($usageStatsRetention30m, $usageStatsRetention1d);
2021-06-18 22:00:27 +12:00
notifyDeleteConnections();
notifyDeleteExpiredSessions();
2022-05-12 01:11:58 +12:00
renewCertificates($database);
2021-01-21 06:57:49 +13:00
}, $interval);
});