YGGo/crontab/cleaner.php

63 lines
1.3 KiB
PHP
Raw Normal View History

2023-04-08 21:06:28 +00:00
<?php
2023-08-01 14:53:14 +00:00
// Stop cleaner on cli running
$semaphore = sem_get(crc32('cli.yggo'), 1);
2023-04-08 21:06:28 +00:00
if (false === sem_acquire($semaphore, true)) {
2023-08-01 14:53:14 +00:00
echo 'cli.yggo process running in another thread.' . PHP_EOL;
exit;
}
2023-08-01 14:53:14 +00:00
// Lock multi-thread execution
$semaphore = sem_get(crc32('crontab.cleaner'), 1);
if (false === sem_acquire($semaphore, true)) {
2023-08-01 14:53:14 +00:00
echo 'process locked by another thread.' . PHP_EOL;
2023-04-08 21:06:28 +00:00
exit;
}
// Define variables
$timeStart = microtime(true);
2023-08-01 14:53:14 +00:00
2023-04-08 21:06:28 +00:00
// Load system dependencies
2023-06-30 11:38:29 +00:00
require_once(__DIR__ . '/../config/app.php');
require_once(__DIR__ . '/../library/mysql.php');
2023-04-08 21:06:28 +00:00
// Connect database
2023-08-05 16:39:49 +00:00
try {
$db = new MySQL(DB_HOST, DB_PORT, DB_NAME, DB_USERNAME, DB_PASSWORD);
} catch(Exception $e) {
var_dump($e);
exit;
}
2023-04-08 21:06:28 +00:00
// Reset banned hosts
$hostsBansRemoved = $db->resetBannedHostPages(time() - CLEAN_HOST_PAGE_BAN_SECONDS_OFFSET);
// Reset banned pages
$hostPagesBansRemoved = $db->resetBannedHosts(time() - CLEAN_HOST_BAN_SECONDS_OFFSET);
// Optimize tables
if (CLEAN_DB_TABLES_OPTIMIZATION) {
try {
2023-05-29 19:13:41 +00:00
$db->optimize();
} catch (Exception $e) {
var_dump($e);
}
2023-04-08 21:06:28 +00:00
}
// Debug
echo 'Host bans removed: ' . $hostsBansRemoved . PHP_EOL;
echo 'Host page bans removed: ' . $hostPagesBansRemoved . PHP_EOL;
2023-05-08 05:27:21 +00:00
echo 'Total time: ' . microtime(true) - $timeStart . PHP_EOL . PHP_EOL;