1
0
Fork 0
mirror of synced 2024-06-03 03:14:50 +12:00
appwrite/app/tasks/maintenance.php

95 lines
2.8 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
2020-12-18 23:48:00 +13:00
// TODO: Think of a better way to access consoleDB
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, [
2020-12-19 03:05:15 +13:00
'type' => DELETE_TYPE_DOCUMENT,
'document' => new Document([
'$collection' => Database::SYSTEM_COLLECTION_EXECUTIONS,
'projectIds' => $projectIds
2020-12-19 00:08:58 +13:00
])
]);
}
2020-12-19 03:05:15 +13:00
function notifyDeleteAbuseLogs(array $projectIds, int $interval)
{
Resque::enqueue(DELETE_QUEUE_NAME, DELETE_CLASS_NAME, [
2020-12-19 03:05:15 +13:00
'type' => DELETE_TYPE_ABUSE,
'document' => new Document([
'projectIds' => $projectIds,
2020-12-19 03:05:15 +13:00
'timestamp' => time() - $interval,
2020-12-19 00:08:58 +13:00
])
]);
}
2020-12-19 03:05:15 +13:00
function notifyDeleteAuditLogs(array $projectIds, int $interval)
{
Resque::enqueue(DELETE_QUEUE_NAME, DELETE_CLASS_NAME, [
2020-12-19 03:05:15 +13:00
'type' => DELETE_TYPE_AUDIT,
'document' => new Document([
'projectIds' => $projectIds,
2020-12-19 03:05:15 +13:00
'timestamp' => time() - $interval
2020-12-19 00:08:58 +13:00
])
]);
}
2020-12-15 05:39:44 +13:00
$cli
->task('maintenance')
->desc('Schedules maintenance tasks and publishes them to resque')
->action(function () {
2020-12-18 23:48:00 +13:00
// # of days in seconds (1 day = 86400s)
2020-12-15 10:26:37 +13:00
$interval = App::getEnv('_APP_MAINTENANCE_INTERVAL', '') + 0;
//Convert Seconds to microseconds
2020-12-19 03:05:15 +13:00
$intervalMicroseconds = $interval * 1000000;
2020-12-15 10:26:37 +13:00
$consoleDB = getConsoleDB();
2020-12-19 03:05:15 +13:00
Console::loop(function() use ($consoleDB, $interval){
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
2020-12-22 03:46:31 +13:00
Console::info("[ MAINTENANCE TASK ] Notifying deletes workers every {$interval} seconds");
// notifyDeleteExecutionLogs($projectIds);
2020-12-19 03:05:15 +13:00
notifyDeleteAbuseLogs($projectIds, $interval);
2020-12-22 03:46:31 +13:00
// notifyDeleteAuditLogs($projectIds, $interval);
2020-12-19 03:05:15 +13:00
}, $intervalMicroseconds);
2020-12-15 05:39:44 +13:00
});