Changed how exceptions work and are logged, throw proper exceptions on tasks

This commit is contained in:
Shoghi Cervantes
2015-09-18 12:03:24 +02:00
parent 472fcfa4c7
commit 0bcf639a98
15 changed files with 52 additions and 78 deletions

View File

@ -47,7 +47,7 @@ class AsyncPool{
for($i = 0; $i < $this->size; ++$i){
$this->workerUsage[$i] = 0;
$this->workers[$i] = new AsyncWorker;
$this->workers[$i] = new AsyncWorker($this->server->getLogger(), $i);
$this->workers[$i]->setClassLoader($this->server->getLoader());
$this->workers[$i]->start();
}
@ -62,7 +62,7 @@ class AsyncPool{
if($newSize > $this->size){
for($i = $this->size; $i < $newSize; ++$i){
$this->workerUsage[$i] = 0;
$this->workers[$i] = new AsyncWorker;
$this->workers[$i] = new AsyncWorker($this->server->getLogger(), $i);
$this->workers[$i]->setClassLoader($this->server->getLoader());
$this->workers[$i]->start();
}
@ -142,16 +142,16 @@ class AsyncPool{
Timings::$schedulerAsyncTimer->startTiming();
foreach($this->tasks as $task){
if($task->isGarbage() and !$task->isRunning()){
if($task->isGarbage() and !$task->isRunning() and !$task->isCrashed()){
if(!$task->hasCancelledRun()){
$task->onCompletion($this->server);
}
$this->removeTask($task);
}elseif($task->isTerminated()){
$this->removeTask($task, true);
}elseif($task->isTerminated() or $task->isCrashed()){
$this->server->getLogger()->critical("Could not execute asynchronous task " . (new \ReflectionClass($task))->getShortName() . ": Task crashed");
$this->removeTask($task, true);
}
}

View File

@ -39,16 +39,27 @@ abstract class AsyncTask extends \Collectable{
/** @var int */
private $taskId = null;
private $crashed = false;
public function run(){
$this->result = null;
if($this->cancelRun !== true){
$this->onRun();
try{
$this->onRun();
}catch(\Throwable $e){
$this->crashed = true;
$this->worker->handleException($e);
}
}
$this->setGarbage();
}
public function isCrashed(){
return $this->crashed;
}
/**
* @return mixed
*/

View File

@ -25,6 +25,14 @@ use pocketmine\Worker;
class AsyncWorker extends Worker{
private $logger;
private $id;
public function __construct(\ThreadedLogger $logger, $id){
$this->logger = $logger;
$this->id = $id;
}
public function run(){
$this->registerClassLoader();
gc_enable();
@ -32,14 +40,13 @@ class AsyncWorker extends Worker{
global $store;
$store = [];
}
public function start(int $options = PTHREADS_INHERIT_NONE){
parent::start(PTHREADS_INHERIT_CONSTANTS | PTHREADS_INHERIT_FUNCTIONS);
public function handleException(\Throwable $e){
$this->logger->logException($e);
}
public function getThreadName(){
return "Asynchronous Worker";
return "Asynchronous Worker #" . $this->id;
}
}

View File

@ -26,7 +26,6 @@ use pocketmine\Server;
use pocketmine\utils\Utils;
use pocketmine\utils\UUID;
use pocketmine\utils\VersionString;
use pocketmine\utils\UUID;
class SendUsageTask extends AsyncTask{

View File

@ -249,10 +249,7 @@ class ServerScheduler{
$task->run($this->currentTick);
}catch(\Throwable $e){
Server::getInstance()->getLogger()->critical("Could not execute task " . $task->getTaskName() . ": " . $e->getMessage());
$logger = Server::getInstance()->getLogger();
if($logger instanceof MainLogger){
$logger->logException($e);
}
Server::getInstance()->getLogger()->logException($e);
}
$task->timings->stopTiming();
}