AsyncPool: Remove Server dependency (API breaks included)

This brings two plugin-breaking changes: AsyncTask->onCompletion() and AsyncTask->onProgressUpdate() no longer accept Server parameters.

This now allows for the functionality of AsyncPool and AsyncTask to be tested outside of a Server.
This commit is contained in:
Dylan K. Taylor 2018-09-10 15:47:12 +01:00
parent 5ce2d5e072
commit d62e00cc74
11 changed files with 19 additions and 40 deletions

View File

@ -1528,7 +1528,7 @@ class Server{
$poolSize = max(1, (int) $poolSize);
}
$this->asyncPool = new AsyncPool($this, $poolSize, (int) max(-1, (int) $this->getProperty("memory.async-worker-hard-limit", 256)), $this->autoloader, $this->logger);
$this->asyncPool = new AsyncPool($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){
NetworkCompression::$THRESHOLD = (int) $this->getProperty("network.batch-threshold", 256);

View File

@ -28,7 +28,6 @@ use pocketmine\command\utils\InvalidCommandSyntaxException;
use pocketmine\lang\TranslationContainer;
use pocketmine\Player;
use pocketmine\scheduler\BulkCurlTask;
use pocketmine\Server;
use pocketmine\timings\TimingsHandler;
use pocketmine\utils\InternetException;
@ -127,7 +126,7 @@ class TimingsCommand extends VanillaCommand{
$this->storeLocal($sender);
}
public function onCompletion(Server $server){
public function onCompletion(){
$sender = $this->fetchLocal();
if($sender instanceof Player and !$sender->isOnline()){ // TODO replace with a more generic API method for checking availability of CommandSender
return;

View File

@ -27,7 +27,6 @@ use pocketmine\level\format\Chunk;
use pocketmine\level\Level;
use pocketmine\level\SimpleChunkManager;
use pocketmine\scheduler\AsyncTask;
use pocketmine\Server;
class PopulationTask extends AsyncTask{
@ -137,7 +136,7 @@ class PopulationTask extends AsyncTask{
}
}
public function onCompletion(Server $server) : void{
public function onCompletion() : void{
/** @var Level $level */
$level = $this->fetchLocal();
if(!$level->isClosed()){

View File

@ -26,7 +26,6 @@ namespace pocketmine\level\light;
use pocketmine\level\format\Chunk;
use pocketmine\level\Level;
use pocketmine\scheduler\AsyncTask;
use pocketmine\Server;
class LightPopulationTask extends AsyncTask{
@ -48,7 +47,7 @@ class LightPopulationTask extends AsyncTask{
$this->chunk = $chunk->fastSerialize();
}
public function onCompletion(Server $server){
public function onCompletion() : void{
/** @var Level $level */
$level = $this->fetchLocal();
if(!$level->isClosed()){

View File

@ -26,7 +26,6 @@ namespace pocketmine\network\mcpe;
use pocketmine\level\format\Chunk;
use pocketmine\network\mcpe\protocol\FullChunkDataPacket;
use pocketmine\scheduler\AsyncTask;
use pocketmine\Server;
use pocketmine\tile\Spawnable;
class ChunkRequestTask extends AsyncTask{
@ -75,7 +74,7 @@ class ChunkRequestTask extends AsyncTask{
$this->setResult(NetworkCompression::compress($stream->buffer, $this->compressionLevel), false);
}
public function onCompletion(Server $server) : void{
public function onCompletion() : void{
/** @var CompressBatchPromise $promise */
$promise = $this->fetchLocal();
$promise->resolve($this->getResult());

View File

@ -24,7 +24,6 @@ declare(strict_types=1);
namespace pocketmine\network\mcpe;
use pocketmine\scheduler\AsyncTask;
use pocketmine\Server;
class CompressBatchTask extends AsyncTask{
@ -46,7 +45,7 @@ class CompressBatchTask extends AsyncTask{
$this->setResult(NetworkCompression::compress($this->data, $this->level), false);
}
public function onCompletion(Server $server) : void{
public function onCompletion() : void{
/** @var CompressBatchPromise $promise */
$promise = $this->fetchLocal();
$promise->resolve($this->getResult());

View File

@ -35,7 +35,6 @@ use Mdanter\Ecc\Serializer\Signature\DerSignatureSerializer;
use pocketmine\network\mcpe\protocol\LoginPacket;
use pocketmine\Player;
use pocketmine\scheduler\AsyncTask;
use pocketmine\Server;
class ProcessLoginTask extends AsyncTask{
@ -216,11 +215,11 @@ class ProcessLoginTask extends AsyncTask{
return rtrim(strtr(base64_encode($str), '+/', '-_'), '=');
}
public function onCompletion(Server $server) : void{
public function onCompletion() : void{
/** @var Player $player */
$player = $this->fetchLocal();
if(!$player->isConnected()){
$server->getLogger()->error("Player " . $player->getName() . " was disconnected before their login could be verified");
$this->worker->getLogger()->error("Player " . $player->getName() . " was disconnected before their login could be verified");
}elseif($player->setAuthenticationStatus($this->authenticated, $this->error)){
if(!$this->useEncryption){
$player->getNetworkSession()->onLoginSuccess();

View File

@ -23,8 +23,6 @@ declare(strict_types=1);
namespace pocketmine\scheduler;
use pocketmine\Server;
/**
* Manages general-purpose worker threads used for processing asynchronous tasks, and the tasks submitted to those
* workers.
@ -32,9 +30,6 @@ use pocketmine\Server;
class AsyncPool{
private const WORKER_START_OPTIONS = PTHREADS_INHERIT_INI | PTHREADS_INHERIT_CONSTANTS;
/** @var Server */
private $server;
/** @var \ClassLoader */
private $classLoader;
/** @var \ThreadedLogger */
@ -59,8 +54,7 @@ class AsyncPool{
/** @var \Closure[] */
private $workerStartHooks = [];
public function __construct(Server $server, int $size, int $workerMemoryLimit, \ClassLoader $classLoader, \ThreadedLogger $logger){
$this->server = $server;
public function __construct(int $size, int $workerMemoryLimit, \ClassLoader $classLoader, \ThreadedLogger $logger){
$this->size = $size;
$this->workerMemoryLimit = $workerMemoryLimit;
$this->classLoader = $classLoader;
@ -259,7 +253,7 @@ class AsyncPool{
}
if(count($this->tasks) > 0){
Server::microSleep(25000);
usleep(25000);
}
}while(count($this->tasks) > 0);
@ -290,12 +284,12 @@ class AsyncPool{
public function collectTasks() : void{
foreach($this->tasks as $task){
if(!$task->isGarbage()){
$task->checkProgressUpdates($this->server);
$task->checkProgressUpdates();
}
if($task->isGarbage() and !$task->isRunning() and !$task->isCrashed()){
if(!$task->hasCancelledRun()){
try{
$task->onCompletion($this->server);
$task->onCompletion();
if($task->removeDanglingStoredObjects()){
$this->logger->notice("AsyncTask " . get_class($task) . " stored local complex data but did not remove them after completion");
}

View File

@ -24,7 +24,6 @@ declare(strict_types=1);
namespace pocketmine\scheduler;
use pocketmine\Collectable;
use pocketmine\Server;
/**
* Class used to run async tasks in other threads.
@ -174,11 +173,9 @@ abstract class AsyncTask extends Collectable{
* Actions to execute when completed (on main thread)
* Implement this if you want to handle the data in your AsyncTask after it has been processed
*
* @param Server $server
*
* @return void
*/
public function onCompletion(Server $server){
public function onCompletion(){
}
@ -194,13 +191,11 @@ abstract class AsyncTask extends Collectable{
/**
* @internal Only call from AsyncPool.php on the main thread
*
* @param Server $server
*/
public function checkProgressUpdates(Server $server){
public function checkProgressUpdates(){
while($this->progressUpdates->count() !== 0){
$progress = $this->progressUpdates->shift();
$this->onProgressUpdate($server, unserialize($progress));
$this->onProgressUpdate(unserialize($progress));
}
}
@ -209,11 +204,10 @@ abstract class AsyncTask extends Collectable{
* All {@link AsyncTask#publishProgress} calls should result in {@link AsyncTask#onProgressUpdate} calls before
* {@link AsyncTask#onCompletion} is called.
*
* @param Server $server
* @param mixed $progress The parameter passed to {@link AsyncTask#publishProgress}. It is serialize()'ed
* @param mixed $progress The parameter passed to {@link AsyncTask#publishProgress}. It is serialize()'ed
* and then unserialize()'ed, as if it has been cloned.
*/
public function onProgressUpdate(Server $server, $progress){
public function onProgressUpdate($progress){
}

View File

@ -24,9 +24,7 @@ declare(strict_types=1);
namespace pocketmine\updater;
use pocketmine\scheduler\AsyncTask;
use pocketmine\Server;
use pocketmine\utils\Internet;
class UpdateCheckTask extends AsyncTask{
@ -72,7 +70,7 @@ class UpdateCheckTask extends AsyncTask{
}
}
public function onCompletion(Server $server){
public function onCompletion(){
/** @var AutoUpdater $updater */
$updater = $this->fetchLocal();
if($this->hasResult()){

View File

@ -25,7 +25,6 @@ namespace pmmp\TesterPlugin\tests;
use pmmp\TesterPlugin\Test;
use pocketmine\scheduler\AsyncTask;
use pocketmine\Server;
use pocketmine\utils\MainLogger;
class AsyncTaskMainLoggerTest extends Test{
@ -49,7 +48,7 @@ class AsyncTaskMainLoggerTest extends Test{
ob_end_flush();
}
public function onCompletion(Server $server){
public function onCompletion(){
/** @var AsyncTaskMainLoggerTest $test */
$test = $this->fetchLocal();
$test->setResult($this->success ? Test::RESULT_OK : Test::RESULT_FAILED);