mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 01:46:04 +00:00
Implemented global, tasks timings
This commit is contained in:
@ -178,7 +178,7 @@ class ServerScheduler{
|
||||
$period = 1;
|
||||
}
|
||||
|
||||
return $this->handle(new TaskHandler($task, $this->nextId(), $delay, $period));
|
||||
return $this->handle(new TaskHandler(!($task instanceof PluginTask) ? "Scheduler" : null, $task, $this->nextId(), $delay, $period));
|
||||
}
|
||||
|
||||
private function handle(TaskHandler $handler){
|
||||
@ -201,12 +201,15 @@ class ServerScheduler{
|
||||
public function mainThreadHeartbeat($currentTick){
|
||||
$this->currentTick = $currentTick;
|
||||
while($this->isReady($this->currentTick)){
|
||||
/** @var TaskHandler $task */
|
||||
$task = $this->queue->extract();
|
||||
if($task->isCancelled()){
|
||||
unset($this->tasks[$task->getTaskId()]);
|
||||
continue;
|
||||
}else{
|
||||
$task->timings->startTiming();
|
||||
$task->run($this->currentTick);
|
||||
$task->timings->stopTiming();
|
||||
}
|
||||
if($task->isRepeating()){
|
||||
$task->setNextRun($this->currentTick + $task->getPeriod());
|
||||
|
@ -21,6 +21,8 @@
|
||||
|
||||
namespace pocketmine\scheduler;
|
||||
|
||||
use pocketmine\event\Timings;
|
||||
|
||||
class TaskHandler{
|
||||
|
||||
/** @var Task */
|
||||
@ -41,17 +43,25 @@ class TaskHandler{
|
||||
/** @var bool */
|
||||
protected $cancelled = false;
|
||||
|
||||
/** @var \pocketmine\event\TimingsHandler */
|
||||
public $timings;
|
||||
|
||||
public $timingName = null;
|
||||
|
||||
/**
|
||||
* @param Task $task
|
||||
* @param int $taskId
|
||||
* @param int $delay
|
||||
* @param int $period
|
||||
* @param string $timingName
|
||||
* @param Task $task
|
||||
* @param int $taskId
|
||||
* @param int $delay
|
||||
* @param int $period
|
||||
*/
|
||||
public function __construct(Task $task, $taskId, $delay = -1, $period = -1){
|
||||
public function __construct($timingName, Task $task, $taskId, $delay = -1, $period = -1){
|
||||
$this->task = $task;
|
||||
$this->taskId = $taskId;
|
||||
$this->delay = $delay;
|
||||
$this->period = $period;
|
||||
$this->timingName = $timingName === null ? "Unknown" : $timingName;
|
||||
$this->timings = Timings::getPluginTaskTimings($this, $period);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -127,6 +137,7 @@ class TaskHandler{
|
||||
public function remove(){
|
||||
$this->cancelled = true;
|
||||
$this->task->setHandler(null);
|
||||
$this->timings->remove();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -135,4 +146,14 @@ class TaskHandler{
|
||||
public function run($currentTick){
|
||||
$this->task->onRun($currentTick);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTaskName(){
|
||||
if($this->timingName !== null){
|
||||
return $this->timingName;
|
||||
}
|
||||
return get_class($this->task);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user