appwrite/src/Appwrite/CLI/Tasks/Maintenance.php

145 lines
5.4 KiB
PHP
Raw Normal View History

2022-07-13 07:02:55 +00:00
<?php
2022-07-14 02:04:31 +00:00
2022-08-02 01:32:46 +00:00
namespace Appwrite\CLI\Tasks;
2022-07-13 07:02:55 +00:00
use Appwrite\Auth\Auth;
use Appwrite\Event\Certificate;
use Appwrite\Event\Delete;
use Utopia\App;
use Utopia\CLI\Console;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\DateTime;
2022-07-13 07:02:55 +00:00
use Utopia\Database\Query;
use Utopia\Platform\Action;
2022-07-14 02:04:31 +00:00
class Maintenance extends Action
{
2022-08-02 01:58:36 +00:00
public static function getName(): string
{
return 'maintenance';
}
2022-07-13 07:02:55 +00:00
public function __construct()
{
$this
->desc('Schedules maintenance tasks and publishes them to resque')
2022-10-28 03:57:59 +00:00
->inject('dbForConsole')
->callback(fn (Database $dbForConsole) => $this->action($dbForConsole));
2022-07-13 07:02:55 +00:00
}
2022-10-28 03:57:59 +00:00
public function action(Database $dbForConsole): void
2022-07-13 07:02:55 +00:00
{
Console::title('Maintenance V1');
Console::success(APP_NAME . ' maintenance process v1 has started');
function notifyDeleteExecutionLogs(int $interval)
{
(new Delete())
->setType(DELETE_TYPE_EXECUTIONS)
2022-07-13 14:02:49 +00:00
->setDatetime(DateTime::addSeconds(new \DateTime(), -1 * $interval))
2022-07-13 07:02:55 +00:00
->trigger();
}
function notifyDeleteAbuseLogs(int $interval)
{
(new Delete())
->setType(DELETE_TYPE_ABUSE)
2022-07-13 14:02:49 +00:00
->setDatetime(DateTime::addSeconds(new \DateTime(), -1 * $interval))
2022-07-13 07:02:55 +00:00
->trigger();
}
function notifyDeleteAuditLogs(int $interval)
{
(new Delete())
->setType(DELETE_TYPE_AUDIT)
2022-07-13 14:02:49 +00:00
->setDatetime(DateTime::addSeconds(new \DateTime(), -1 * $interval))
2022-07-13 07:02:55 +00:00
->trigger();
}
function notifyDeleteUsageStats(int $interval30m, int $interval1d)
{
(new Delete())
->setType(DELETE_TYPE_USAGE)
2022-07-13 14:02:49 +00:00
->setDateTime1d(DateTime::addSeconds(new \DateTime(), -1 * $interval1d))
->setDateTime30m(DateTime::addSeconds(new \DateTime(), -1 * $interval30m))
2022-07-13 07:02:55 +00:00
->trigger();
}
function notifyDeleteConnections()
{
(new Delete())
->setType(DELETE_TYPE_REALTIME)
2022-07-13 14:02:49 +00:00
->setDatetime(DateTime::addSeconds(new \DateTime(), -60))
2022-07-13 07:02:55 +00:00
->trigger();
}
function notifyDeleteExpiredSessions()
{
(new Delete())
->setType(DELETE_TYPE_SESSIONS)
2022-07-13 14:02:49 +00:00
->setDatetime(DateTime::addSeconds(new \DateTime(), -1 * Auth::TOKEN_EXPIRATION_LOGIN_LONG))
2022-07-13 07:02:55 +00:00
->trigger();
}
function renewCertificates($dbForConsole)
{
2022-07-13 14:02:49 +00:00
$time = DateTime::now();
2022-07-11 15:12:41 +00:00
2022-07-13 07:02:55 +00:00
$certificates = $dbForConsole->find('certificates', [
2022-09-07 11:57:08 +00:00
Query::lessThan('attempts', 5), // Maximum 5 attempts
2022-09-02 14:19:36 +00:00
Query::lessThanEqual('renewDate', $time), // includes 60 days cooldown (we have 30 days to renew)
Query::limit(200), // Limit 200 comes from LetsEncrypt (300 orders per 3 hours, keeping some for new domains)
]);
2022-07-13 07:02:55 +00:00
if (\count($certificates) > 0) {
Console::info("[{$time}] Found " . \count($certificates) . " certificates for renewal, scheduling jobs.");
$event = new Certificate();
foreach ($certificates as $certificate) {
$event
->setDomain(new Document([
'domain' => $certificate->getAttribute('domain')
]))
->trigger();
}
} else {
Console::info("[{$time}] No certificates for renewal.");
}
}
2022-07-03 09:36:59 +00:00
function notifyDeleteCache($interval)
{
(new Delete())
2022-08-15 09:05:41 +00:00
->setType(DELETE_TYPE_CACHE_BY_TIMESTAMP)
2022-08-23 05:23:01 +00:00
->setDatetime(DateTime::addSeconds(new \DateTime(), -1 * $interval))
2022-07-03 09:36:59 +00:00
->trigger();
}
2022-07-13 07:02:55 +00:00
// # of days in seconds (1 day = 86400s)
$interval = (int) App::getEnv('_APP_MAINTENANCE_INTERVAL', '86400');
$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
$usageStatsRetention1d = (int) App::getEnv('_APP_MAINTENANCE_RETENTION_USAGE_1D', '8640000'); // 100 days
2022-07-03 09:36:59 +00:00
$cacheRetention = (int) App::getEnv('_APP_MAINTENANCE_RETENTION_CACHE', '2592000'); // 30 days
2022-07-13 07:02:55 +00:00
2022-10-28 03:57:59 +00:00
Console::loop(function () use ($interval, $executionLogsRetention, $abuseLogsRetention, $auditLogRetention, $usageStatsRetention30m, $usageStatsRetention1d, $cacheRetention, $dbForConsole) {
2022-07-13 14:02:49 +00:00
$time = DateTime::now();
2022-07-11 15:12:41 +00:00
2022-07-13 07:02:55 +00:00
Console::info("[{$time}] Notifying workers with maintenance tasks every {$interval} seconds");
notifyDeleteExecutionLogs($executionLogsRetention);
notifyDeleteAbuseLogs($abuseLogsRetention);
notifyDeleteAuditLogs($auditLogRetention);
notifyDeleteUsageStats($usageStatsRetention30m, $usageStatsRetention1d);
notifyDeleteConnections();
notifyDeleteExpiredSessions();
2022-10-28 03:57:59 +00:00
renewCertificates($dbForConsole);
2022-07-03 09:36:59 +00:00
notifyDeleteCache($cacheRetention);
2022-07-13 07:02:55 +00:00
}, $interval);
}
2022-07-14 02:04:31 +00:00
}