PHPDaemon\Core\Daemon::getStateOfWorkers PHP Метод

getStateOfWorkers() публичный статический Метод

Get state of workers.
public static getStateOfWorkers ( ) : array
Результат array - information.
    public static function getStateOfWorkers()
    {
        $offset = 0;
        $stat = ['idle' => 0, 'busy' => 0, 'alive' => 0, 'shutdown' => 0, 'preinit' => 0, 'init' => 0, 'reloading' => 0];
        $readed = 0;
        while (($buf = Daemon::$shm_wstate->read($readed, 1024)) !== false) {
            $buflen = mb_orig_strlen($buf);
            $readed += $buflen;
            for ($i = 0; $i < $buflen; ++$i) {
                $code = ord($buf[$i]);
                if ($code >= 100) {
                    // reloaded (shutdown)
                    $code -= 100;
                    if ($code !== Daemon::WSTATE_SHUTDOWN) {
                        ++$stat['alive'];
                        if (Daemon::$process instanceof Thread\Master) {
                            Daemon::$process->reloadWorker($offset + $i + 1);
                            ++$stat['reloading'];
                            continue;
                        }
                    }
                }
                if ($code === Daemon::WSTATE_IDLE) {
                    // idle
                    ++$stat['alive'];
                    ++$stat['idle'];
                } elseif ($code === Daemon::WSTATE_BUSY) {
                    // busy
                    ++$stat['alive'];
                    ++$stat['busy'];
                } elseif ($code === Daemon::WSTATE_SHUTDOWN) {
                    // shutdown
                    ++$stat['shutdown'];
                } elseif ($code === Daemon::WSTATE_PREINIT) {
                    // pre-init
                    ++$stat['alive'];
                    ++$stat['preinit'];
                    ++$stat['idle'];
                } elseif ($code === Daemon::WSTATE_INIT) {
                    // init
                    ++$stat['alive'];
                    ++$stat['init'];
                    ++$stat['idle'];
                }
            }
        }
        return $stat;
    }

Usage Example

Пример #1
0
    /**
     * Called when request iterated.
     * @return integer Status.
     */
    public function run()
    {
        $stime = microtime(true);
        $this->header('Content-Type: text/html; charset=utf-8');
        ?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
            <title>Server status.</title>
        </head>
        <body>
        <br/>Uptime: <b><?php 
        echo DateTime::diffAsText(Daemon::$startTime, time());
        ?>
</b>
        <br/><br/><b>State of workers:</b><?php 
        $stat = Daemon::getStateOfWorkers();
        ?>
        <br/>Idle: <?php 
        echo $stat['idle'];
        ?>
        <br/>Busy: <?php 
        echo $stat['busy'];
        ?>
        <br/>Total alive: <?php 
        echo $stat['alive'];
        ?>
        <br/>Shutdown: <?php 
        echo $stat['shutdown'];
        ?>
        <br/>Pre-init: <?php 
        echo $stat['preinit'];
        ?>
        <br/>Wait-init: <?php 
        echo $stat['waitinit'];
        ?>
        <br/>Init: <?php 
        echo $stat['init'];
        ?>
        <br/>
        <br/>Request took: <?php 
        printf('%f', round(microtime(true) - $stime, 6));
        ?>
        </body>
        </html>
        <?php 
    }
All Usage Examples Of PHPDaemon\Core\Daemon::getStateOfWorkers