server = $server; $this->size = $size; $memoryLimit = (int) max(-1, (int) $this->server->getProperty("memory.async-worker-hard-limit", 1024)); for($i = 0; $i < $this->size; ++$i){ $this->workerUsage[$i] = 0; $this->workers[$i] = new AsyncWorker($this->server->getLogger(), $i + 1, $memoryLimit); $this->workers[$i]->setClassLoader($this->server->getLoader()); $this->workers[$i]->start(); } } public function getSize() : int{ return $this->size; } public function increaseSize(int $newSize){ if($newSize > $this->size){ $memoryLimit = (int) max(-1, (int) $this->server->getProperty("memory.async-worker-hard-limit", 1024)); for($i = $this->size; $i < $newSize; ++$i){ $this->workerUsage[$i] = 0; $this->workers[$i] = new AsyncWorker($this->server->getLogger(), $i + 1, $memoryLimit); $this->workers[$i]->setClassLoader($this->server->getLoader()); $this->workers[$i]->start(); } $this->size = $newSize; } } public function submitTaskToWorker(AsyncTask $task, int $worker){ if(isset($this->tasks[$task->getTaskId()]) or $task->isGarbage()){ return; } if($worker < 0 or $worker >= $this->size){ throw new \InvalidArgumentException("Invalid worker $worker"); } $this->tasks[$task->getTaskId()] = $task; $this->workers[$worker]->stack($task); $this->workerUsage[$worker]++; $this->taskWorkers[$task->getTaskId()] = $worker; } public function submitTask(AsyncTask $task) : int{ if(isset($this->tasks[$task->getTaskId()]) or $task->isGarbage()){ return -1; } $selectedWorker = mt_rand(0, $this->size - 1); $selectedTasks = $this->workerUsage[$selectedWorker]; for($i = 0; $i < $this->size; ++$i){ if($this->workerUsage[$i] < $selectedTasks){ $selectedWorker = $i; $selectedTasks = $this->workerUsage[$i]; } } $this->submitTaskToWorker($task, $selectedWorker); return $selectedWorker; } private function removeTask(AsyncTask $task, bool $force = false){ if(isset($this->taskWorkers[$task->getTaskId()])){ if(!$force and ($task->isRunning() or !$task->isGarbage())){ return; } $this->workerUsage[$this->taskWorkers[$task->getTaskId()]]--; } unset($this->tasks[$task->getTaskId()]); unset($this->taskWorkers[$task->getTaskId()]); } public function removeTasks(){ do{ foreach($this->tasks as $task){ $task->cancelRun(); $this->removeTask($task); } if(count($this->tasks) > 0){ Server::microSleep(25000); } }while(count($this->tasks) > 0); for($i = 0; $i < $this->size; ++$i){ $this->workerUsage[$i] = 0; } $this->taskWorkers = []; $this->tasks = []; $this->collectWorkers(); } private function collectWorkers(){ foreach($this->workers as $worker){ $worker->collect(); } } public function collectTasks(){ Timings::$schedulerAsyncTimer->startTiming(); foreach($this->tasks as $task){ if(!$task->isGarbage()){ $task->checkProgressUpdates($this->server); } if($task->isGarbage() and !$task->isRunning() and !$task->isCrashed()){ if(!$task->hasCancelledRun()){ try{ $task->onCompletion($this->server); if(!$this->server->getScheduler()->removeLocalComplex($task)){ $this->server->getLogger()->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 asychronous task " . (new \ReflectionClass($task))->getShortName() . ": " . $e->getMessage()); $this->server->getLogger()->logException($e); $this->server->getScheduler()->removeLocalComplex($task); //silent } } $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->removeTask($task, true); } } $this->collectWorkers(); Timings::$schedulerAsyncTimer->stopTiming(); } }