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

73 lines
2.1 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
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;
}
2020-12-22 07:15:52 +13:00
function notifyDeleteExecutionLogs()
{
Resque::enqueue(DELETE_QUEUE_NAME, DELETE_CLASS_NAME, [
2020-12-22 07:15:52 +13:00
'type' => DELETE_TYPE_EXECUTION_LOGS,
'document' => new Document([
2020-12-22 07:15:52 +13:00
'$collection' => Database::SYSTEM_COLLECTION_EXECUTIONS
2020-12-19 00:08:58 +13:00
])
]);
}
2020-12-22 07:15:52 +13:00
function notifyDeleteAbuseLogs(int $interval)
{
Resque::enqueue(DELETE_QUEUE_NAME, DELETE_CLASS_NAME, [
2020-12-19 03:05:15 +13:00
'type' => DELETE_TYPE_ABUSE,
2020-12-22 07:15:52 +13:00
'timestamp' => time() - $interval
]);
}
2020-12-22 07:15:52 +13:00
function notifyDeleteAuditLogs(int $interval)
{
Resque::enqueue(DELETE_QUEUE_NAME, DELETE_CLASS_NAME, [
2020-12-19 03:05:15 +13:00
'type' => DELETE_TYPE_AUDIT,
2020-12-22 07:15:52 +13:00
'timestamp' => time() - $interval
]);
}
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){
2020-12-22 03:46:31 +13:00
Console::info("[ MAINTENANCE TASK ] Notifying deletes workers every {$interval} seconds");
2020-12-22 07:15:52 +13:00
notifyDeleteExecutionLogs();
notifyDeleteAbuseLogs($interval);
notifyDeleteAuditLogs($interval);
2020-12-19 03:05:15 +13:00
}, $intervalMicroseconds);
2020-12-15 05:39:44 +13:00
});