1
0
Fork 0
mirror of synced 2024-05-20 20:52:36 +12:00
appwrite/app/tasks/maintenance.php

89 lines
2.5 KiB
PHP
Raw Normal View History

2020-12-15 05:39:44 +13:00
<?php
global $cli;
2020-12-15 10:26:37 +13:00
require_once __DIR__.'/../init.php';
use Appwrite\Database\Database;
use Appwrite\Database\Document;
use Appwrite\Database\Adapter\MySQL as MySQLAdapter;
use Appwrite\Database\Adapter\Redis as RedisAdapter;
use Appwrite\Database\Validator\Authorization;
2020-12-15 05:39:44 +13:00
use Utopia\App;
use Utopia\CLI\Console;
use Utopia\Config\Config;
2020-12-15 05:39:44 +13:00
define("DELETE_QUEUE_NAME", "v1-deletes");
define("DELETE_CLASS_NAME", "DeletesV1");
2020-12-15 10:26:37 +13:00
function getConsoleDB() {
global $register;
$consoleDB = new Database();
$consoleDB->setAdapter(new RedisAdapter(new MySQLAdapter($register), $register));
$consoleDB->setNamespace('app_console'); // Main DB
$consoleDB->setMocks(Config::getParam('collections', []));
return $consoleDB;
}
function notifyDeleteExecutionLogs(array $projectIds)
{
Resque::enqueue(DELETE_QUEUE_NAME, DELETE_CLASS_NAME, [
'document' => new Document([
'$collection' => Database::SYSTEM_COLLECTION_EXECUTIONS,
'projectIds' => $projectIds
]),
]);
}
function notifyDeleteAbuseLogs(array $projectIds)
{
Resque::enqueue(DELETE_QUEUE_NAME, DELETE_CLASS_NAME, [
'document' => new Document([
'$collection' => Database::SYSTEM_COLLECTION_EXECUTIONS,
'projectIds' => $projectIds
]),
]);
}
function notifyDeleteAuditLogs(array $projectIds)
{
Resque::enqueue(DELETE_QUEUE_NAME, DELETE_CLASS_NAME, [
'document' => new Document([
'$collection' => Database::SYSTEM_COLLECTION_EXECUTIONS,
'projectIds' => $projectIds
]),
]);
}
2020-12-15 05:39:44 +13:00
$cli
->task('maintenance')
->desc('Schedules maintenance tasks and publishes them to resque')
->action(function () {
2020-12-15 10:26:37 +13:00
// Convert string to integer
$interval = App::getEnv('_APP_MAINTENANCE_INTERVAL', '') + 0;
//Convert Seconds to microseconds
$interval = $interval * 1000000;
$consoleDB = getConsoleDB();
Console::loop(function() use ($consoleDB){
Authorization::disable();
2020-12-15 10:26:37 +13:00
$projects = $consoleDB->getCollection([
'filters' => [
'$collection='.Database::SYSTEM_COLLECTION_PROJECTS,
],
]);
Authorization::reset();
$projectIds = array_map (function ($project) {
return $project->getId();
}, $projects);
2020-12-15 10:26:37 +13:00
notifyDeleteExecutionLogs($projectIds);
notifyDeleteAbuseLogs($projectIds);
notifyDeleteAuditLogs($projectIds);
2020-12-15 10:26:37 +13:00
}, $interval);
2020-12-15 05:39:44 +13:00
});