mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-22 08:44:01 +00:00
AsyncPool: be less dependent on Server in the code
The goal is to remove the Server things from here completely.
This commit is contained in:
parent
af69418a55
commit
c327b3d2c4
@ -1527,7 +1527,7 @@ class Server{
|
||||
$poolSize = (int) $poolSize;
|
||||
}
|
||||
|
||||
$this->asyncPool = new AsyncPool($this, $poolSize, (int) max(-1, (int) $this->getProperty("memory.async-worker-hard-limit", 256)));
|
||||
$this->asyncPool = new AsyncPool($this, $poolSize, (int) max(-1, (int) $this->getProperty("memory.async-worker-hard-limit", 256)), $this->autoloader, $this->logger);
|
||||
|
||||
if($this->getProperty("network.batch-threshold", 256) >= 0){
|
||||
Network::$BATCH_THRESHOLD = (int) $this->getProperty("network.batch-threshold", 256);
|
||||
|
@ -30,6 +30,11 @@ class AsyncPool{
|
||||
/** @var Server */
|
||||
private $server;
|
||||
|
||||
/** @var \ClassLoader */
|
||||
private $classLoader;
|
||||
/** @var \ThreadedLogger */
|
||||
private $logger;
|
||||
|
||||
protected $size;
|
||||
/** @var int */
|
||||
private $workerMemoryLimit;
|
||||
@ -46,15 +51,17 @@ class AsyncPool{
|
||||
/** @var int[] */
|
||||
private $workerUsage = [];
|
||||
|
||||
public function __construct(Server $server, int $size, int $workerMemoryLimit){
|
||||
public function __construct(Server $server, int $size, int $workerMemoryLimit, \ClassLoader $classLoader, \ThreadedLogger $logger){
|
||||
$this->server = $server;
|
||||
$this->size = $size;
|
||||
$this->workerMemoryLimit = $workerMemoryLimit;
|
||||
$this->classLoader = $classLoader;
|
||||
$this->logger = $logger;
|
||||
|
||||
for($i = 0; $i < $this->size; ++$i){
|
||||
$this->workerUsage[$i] = 0;
|
||||
$this->workers[$i] = new AsyncWorker($this->server->getLogger(), $i + 1, $this->workerMemoryLimit);
|
||||
$this->workers[$i]->setClassLoader($this->server->getLoader());
|
||||
$this->workers[$i] = new AsyncWorker($this->logger, $i + 1, $this->workerMemoryLimit);
|
||||
$this->workers[$i]->setClassLoader($this->classLoader);
|
||||
$this->workers[$i]->start();
|
||||
}
|
||||
}
|
||||
@ -67,8 +74,8 @@ class AsyncPool{
|
||||
if($newSize > $this->size){
|
||||
for($i = $this->size; $i < $newSize; ++$i){
|
||||
$this->workerUsage[$i] = 0;
|
||||
$this->workers[$i] = new AsyncWorker($this->server->getLogger(), $i + 1, $this->workerMemoryLimit);
|
||||
$this->workers[$i]->setClassLoader($this->server->getLoader());
|
||||
$this->workers[$i] = new AsyncWorker($this->logger, $i + 1, $this->workerMemoryLimit);
|
||||
$this->workers[$i]->setClassLoader($this->classLoader);
|
||||
$this->workers[$i]->start();
|
||||
}
|
||||
$this->size = $newSize;
|
||||
@ -170,11 +177,11 @@ class AsyncPool{
|
||||
try{
|
||||
$task->onCompletion($this->server);
|
||||
if($task->removeDanglingStoredObjects()){
|
||||
$this->server->getLogger()->notice("AsyncTask " . get_class($task) . " stored local complex data but did not remove them after completion");
|
||||
$this->logger->notice("AsyncTask " . get_class($task) . " stored local complex data but did not remove them after completion");
|
||||
}
|
||||
}catch(\Throwable $e){
|
||||
$this->server->getLogger()->critical("Could not execute completion of asynchronous task " . (new \ReflectionClass($task))->getShortName() . ": " . $e->getMessage());
|
||||
$this->server->getLogger()->logException($e);
|
||||
$this->logger->critical("Could not execute completion of asynchronous task " . (new \ReflectionClass($task))->getShortName() . ": " . $e->getMessage());
|
||||
$this->logger->logException($e);
|
||||
|
||||
$task->removeDanglingStoredObjects(); //silent
|
||||
}
|
||||
@ -182,7 +189,7 @@ class AsyncPool{
|
||||
|
||||
$this->removeTask($task);
|
||||
}elseif($task->isTerminated() or $task->isCrashed()){
|
||||
$this->server->getLogger()->critical("Could not execute asynchronous task " . (new \ReflectionClass($task))->getShortName() . ": Task crashed");
|
||||
$this->logger->critical("Could not execute asynchronous task " . (new \ReflectionClass($task))->getShortName() . ": Task crashed");
|
||||
$this->removeTask($task, true);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user