Updated Cleanup to utilize PECL parallel processing. Uses CPU cores effectively across all threads / workers.
https://www.php.net/manual/en/intro.parallel.php
NOTE: You can't autoload classes prior to the block scope of the run() method.
Because Cleanup extends Extensions, I have to explicitly define both for autoloading to avoid the unnecessary overhead of autoloading every single class file. Whether that only matters upon instance is of question, but I feel better with this way.
https://www.php.net/manual/en/intro.parallel.php
// Cleanup V2 - PECL Parallel support
$tasks = [
'cleancache' => [1],
'seedbonus' => [],
'comments' => [],
'checkexpiredwarns' => [],
'cleanlogs' => [3],
'autopromote' => [],
'autofreeleech' => [],
'autodemote' => [],
'pendingusers' => [],
'privseeders' => [],
'startsync' => [],
'cleanshoutbox' => [10],
'checkdead' => [],
'ratiowarn' => [],
];
$classFiles = [
'Extensions',
'Cleanup'
];
$results = [];
$runtime = new \parallel\Runtime;
$channel = new \parallel\Channel;
foreach ($tasks as $taskName => $taskParams) {
$future = $runtime->run(function () use ($taskName, $taskParams, $classFiles, $channel) {
spl_autoload_register(function ($className) use ($classFiles) {
if (!empty($classFiles) && in_array($className, $classFiles)) {
include_once "classes/$className.php";
}
});
$result = call_user_func_array([(new \Cleanup), $taskName], $taskParams);
$channel->send([$taskName, $result]);
});
}
foreach ($tasks as $taskName => $taskParams) {
[$taskName, $result] = $channel->recv();
$results[$taskName] = $result;
}
$channel->close();
foreach ($results as $taskname => $result) {
echo "Task $taskname completed.\n";
}
NOTE: You can't autoload classes prior to the block scope of the run() method.
Because Cleanup extends Extensions, I have to explicitly define both for autoloading to avoid the unnecessary overhead of autoloading every single class file. Whether that only matters upon instance is of question, but I feel better with this way.
Last edited by STorrents2020 on 2023-03-06 05:08:48