PocketMine-MP 5.15.1 git-be6754494fdbbb9dd57c058ba0e33a4a78c4581f
ServerKiller.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\utils;
25
27use function hrtime;
28use function intdiv;
29
30class ServerKiller extends Thread{
31 private bool $stopped = false;
32
33 public function __construct(
34 public int $time = 15
35 ){}
36
37 protected function onRun() : void{
38 $start = hrtime(true);
39 $remaining = $this->time * 1_000_000;
40 $this->synchronized(function() use (&$remaining, $start) : void{
41 while(!$this->stopped && $remaining > 0){
42 $this->wait($remaining);
43 $remaining -= intdiv(hrtime(true) - $start, 1000);
44 }
45 });
46 if($remaining <= 0){
47 echo "\nTook too long to stop, server was killed forcefully!\n";
48 @Process::kill(Process::pid());
49 }
50 }
51
52 public function quit() : void{
53 $this->synchronized(function() : void{
54 $this->stopped = true;
55 $this->notify();
56 });
57 parent::quit();
58 }
59
60 public function getThreadName() : string{
61 return "Server Killer";
62 }
63}
static kill(int $pid, bool $subprocesses=false)
Definition: Process.php:131