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

67 lines
1.9 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\Adapter\MySQL as MySQLAdapter;
use Appwrite\Database\Adapter\Redis as RedisAdapter;
use Appwrite\Event\Event;
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(Event::DELETE_QUEUE_NAME, Event::DELETE_CLASS_NAME, [
2020-12-28 06:57:35 +13:00
'type' => DELETE_TYPE_EXECUTIONS
]);
}
2020-12-22 07:15:52 +13:00
function notifyDeleteAbuseLogs(int $interval)
{
Resque::enqueue(Event::DELETE_QUEUE_NAME, Event::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(Event::DELETE_QUEUE_NAME, Event::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-30 08:20:08 +13:00
$interval = (int) App::getEnv('_APP_MAINTENANCE_INTERVAL', '86400');
2020-12-15 10:26:37 +13:00
//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
});