国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 PHP源碼 PHP daemon class

PHP daemon class

Nov 08, 2016 pm 05:37 PM

Daemon class implemented in PHP. You can implement queues or scheduled tasks out of crontab on the server.

When using it, inherit from this class, override the _doTask method, and initialize and execute it through main.

<?php
 
class Daemon {
 
    const DLOG_TO_CONSOLE = 1;
    const DLOG_NOTICE = 2;
    const DLOG_WARNING = 4;
    const DLOG_ERROR = 8;
    const DLOG_CRITICAL = 16;
 
    const DAPC_PATH =  &#39;/tmp/daemon_apc_keys&#39;;
 
    /**
     * User ID
     *
     * @var int
     */
    public $userID = 65534; // nobody
 
    /**
     * Group ID
     *
     * @var integer
     */
    public $groupID = 65533; // nobody
 
    /**
     * Terminate daemon when set identity failure ?
     *
     * @var bool
     * @since 1.0.3
     */
    public $requireSetIdentity = false;
 
    /**
     * Path to PID file
     *
     * @var string
     * @since 1.0.1
     */
    public $pidFileLocation = &#39;/tmp/daemon.pid&#39;;
 
    /**
     * processLocation
     * 進(jìn)程信息記錄目錄
     *
     * @var string
     */
    public $processLocation = &#39;&#39;;
 
    /**
     * processHeartLocation
     * 進(jìn)程心跳包文件
     *
     * @var string
     */
    public $processHeartLocation = &#39;&#39;;
 
    /**
     * Home path
     *
     * @var string
     * @since 1.0
     */
    public $homePath = &#39;/&#39;;
 
    /**
     * Current process ID
     *
     * @var int
     * @since 1.0
     */
    protected $_pid = 0;
 
    /**
     * Is this process a children
     *
     * @var boolean
     * @since 1.0
     */
    protected $_isChildren = false;
 
    /**
     * Is daemon running
     *
     * @var boolean
     * @since 1.0
     */
    protected $_isRunning = false;
 
    /**
     * Constructor
     *
     * @return void
     */
    public function __construct() {
 
        error_reporting(0);
        set_time_limit(0);
        ob_implicit_flush();
 
        register_shutdown_function(array(&$this, &#39;releaseDaemon&#39;));
    }
 
    /**
     * 啟動進(jìn)程
     *
     * @return bool
     */
    public function main() {
 
        $this->_logMessage(&#39;Starting daemon&#39;);
 
        if (!$this->_daemonize()) {
            $this->_logMessage(&#39;Could not start daemon&#39;, self::DLOG_ERROR);
 
            return false;
        }
 
        $this->_logMessage(&#39;Running...&#39;);
 
        $this->_isRunning = true;
 
        while ($this->_isRunning) {
            $this->_doTask();
        }
 
        return true;
    }
 
    /**
     * 停止進(jìn)程
     *
     * @return void
     */
    public function stop() {
 
        $this->_logMessage(&#39;Stoping daemon&#39;);
 
        $this->_isRunning = false;
    }
 
    /**
     * Do task
     *
     * @return void
     */
    protected function _doTask() {
        // override this method
    }
 
    /**
     * _logMessage
     * 記錄日志
     *
     * @param string 消息
     * @param integer 級別
     * @return void
     */
    protected function _logMessage($msg, $level = self::DLOG_NOTICE) {
        // override this method
    }
 
    /**
     * Daemonize
     *
     * Several rules or characteristics that most daemons possess:
     * 1) Check is daemon already running
     * 2) Fork child process
     * 3) Sets identity
     * 4) Make current process a session laeder
     * 5) Write process ID to file
     * 6) Change home path
     * 7) umask(0)
     *
     * @access private
     * @since 1.0
     * @return void
     */
    private function _daemonize() {
 
        ob_end_flush();
 
        if ($this->_isDaemonRunning()) {
            // Deamon is already running. Exiting
            return false;
        }
 
        if (!$this->_fork()) {
            // Coudn&#39;t fork. Exiting.
            return false;
        }
 
        if (!$this->_setIdentity() && $this->requireSetIdentity) {
            // Required identity set failed. Exiting
            return false;
        }
 
        if (!posix_setsid()) {
            $this->_logMessage(&#39;Could not make the current process a session leader&#39;, self::DLOG_ERROR);
 
            return false;
        }
 
        if (!$fp = fopen($this->pidFileLocation, &#39;w&#39;)) {
            $this->_logMessage(&#39;Could not write to PID file&#39;, self::DLOG_ERROR);
            return false;
        } else {
            fputs($fp, $this->_pid);
            fclose($fp);
        }
 
        // 寫入監(jiān)控日志
        $this->writeProcess();
 
        chdir($this->homePath);
        umask(0);
 
        declare(ticks = 1);
 
        pcntl_signal(SIGCHLD, array(&$this, &#39;sigHandler&#39;));
        pcntl_signal(SIGTERM, array(&$this, &#39;sigHandler&#39;));
        pcntl_signal(SIGUSR1, array(&$this, &#39;sigHandler&#39;));
        pcntl_signal(SIGUSR2, array(&$this, &#39;sigHandler&#39;));
 
        return true;
    }
 
    /**
     * Cheks is daemon already running
     *
     * @return bool
     */
    private function _isDaemonRunning() {
 
        $oldPid = file_get_contents($this->pidFileLocation);
 
        if ($oldPid !== false && posix_kill(trim($oldPid),0))
        {
            $this->_logMessage(&#39;Daemon already running with PID: &#39;.$oldPid, (self::DLOG_TO_CONSOLE | self::DLOG_ERROR));
 
            return true;
        }
        else
        {
            return false;
        }
    }
 
    /**
     * Forks process
     *
     * @return bool
     */
    private function _fork() {
 
        $this->_logMessage(&#39;Forking...&#39;);
 
        $pid = pcntl_fork();
 
        if ($pid == -1) {
            // 出錯
            $this->_logMessage(&#39;Could not fork&#39;, self::DLOG_ERROR);
 
            return false;
        } elseif ($pid) {
            // 父進(jìn)程
            $this->_logMessage(&#39;Killing parent&#39;);
 
            exit();
        } else {
            // fork的子進(jìn)程
            $this->_isChildren = true;
            $this->_pid = posix_getpid();
 
            return true;
        }
    }
 
    /**
     * Sets identity of a daemon and returns result
     *
     * @return bool
     */
    private function _setIdentity() {
 
        if (!posix_setgid($this->groupID) || !posix_setuid($this->userID))
        {
            $this->_logMessage(&#39;Could not set identity&#39;, self::DLOG_WARNING);
 
            return false;
        }
        else
        {
            return true;
        }
    }
 
    /**
     * Signals handler
     *
     * @access public
     * @since 1.0
     * @return void
     */
    public function sigHandler($sigNo) {
 
        switch ($sigNo)
        {
            case SIGTERM:   // Shutdown
                $this->_logMessage(&#39;Shutdown signal&#39;);
                exit();
                break;
 
            case SIGCHLD:   // Halt
                $this->_logMessage(&#39;Halt signal&#39;);
                while (pcntl_waitpid(-1, $status, WNOHANG) > 0);
                break;
            case SIGUSR1:   // User-defined
                $this->_logMessage(&#39;User-defined signal 1&#39;);
                $this->_sigHandlerUser1();
                break;
            case SIGUSR2:   // User-defined
                $this->_logMessage(&#39;User-defined signal 2&#39;);
                $this->_sigHandlerUser2();
                break;
        }
    }
 
    /**
     * Signals handler: USR1
     *  主要用于定時清理每個進(jìn)程里被緩存的域名dns解析記錄
     *
     * @return void
     */
    protected function _sigHandlerUser1() {
        apc_clear_cache(&#39;user&#39;);
    }
 
    /**
     * Signals handler: USR2
     * 用于寫入心跳包文件
     *
     * @return void
     */
    protected function _sigHandlerUser2() {
 
        $this->_initProcessLocation();
 
        file_put_contents($this->processHeartLocation, time());
 
        return true;
    }
 
    /**
     * Releases daemon pid file
     * This method is called on exit (destructor like)
     *
     * @return void
     */
    public function releaseDaemon() {
 
        if ($this->_isChildren && is_file($this->pidFileLocation)) {
            $this->_logMessage(&#39;Releasing daemon&#39;);
 
            unlink($this->pidFileLocation);
        }
    }
 
    /**
     * writeProcess
     * 將當(dāng)前進(jìn)程信息寫入監(jiān)控日志,另外的腳本會掃描監(jiān)控日志的數(shù)據(jù)發(fā)送信號,如果沒有響應(yīng)則重啟進(jìn)程
     *
     * @return void
     */
    public function writeProcess() {
 
        // 初始化 proc
        $this->_initProcessLocation();
 
        $command = trim(implode(&#39; &#39;, $_SERVER[&#39;argv&#39;]));
 
        // 指定進(jìn)程的目錄
        $processDir = $this->processLocation . &#39;/&#39; . $this->_pid;
        $processCmdFile = $processDir . &#39;/cmd&#39;;
        $processPwdFile = $processDir . &#39;/pwd&#39;;
 
        // 所有進(jìn)程所在的目錄
        if (!is_dir($this->processLocation)) {
            mkdir($this->processLocation, 0777);
            chmod($processDir, 0777);
        }
 
        // 查詢重復(fù)的進(jìn)程記錄
        $pDirObject = dir($this->processLocation);
        while ($pDirObject && (($pid = $pDirObject->read()) !== false)) {
            if ($pid == &#39;.&#39; || $pid == &#39;..&#39; || intval($pid) != $pid) {
                continue;
            }
 
            $pDir = $this->processLocation . &#39;/&#39; . $pid;
            $pCmdFile = $pDir . &#39;/cmd&#39;;
            $pPwdFile = $pDir . &#39;/pwd&#39;;
            $pHeartFile = $pDir . &#39;/heart&#39;;
 
            // 根據(jù)cmd檢查啟動相同參數(shù)的進(jìn)程
            if (is_file($pCmdFile) && trim(file_get_contents($pCmdFile)) == $command) {
                unlink($pCmdFile);
                unlink($pPwdFile);
                unlink($pHeartFile);
 
                // 刪目錄有緩存
                usleep(1000);
 
                rmdir($pDir);
            }
        }
 
        // 新進(jìn)程目錄
        if (!is_dir($processDir)) {
            mkdir($processDir, 0777);
            chmod($processDir, 0777);
        }
 
        // 寫入命令參數(shù)
        file_put_contents($processCmdFile, $command);
        file_put_contents($processPwdFile, $_SERVER[&#39;PWD&#39;]);
 
        // 寫文件有緩存
        usleep(1000);
 
        return true;
    }
 
    /**
     * _initProcessLocation
     * 初始化
     *
     * @return void
     */
    protected function _initProcessLocation() {
 
        $this->processLocation = ROOT_PATH . &#39;/app/data/proc&#39;;
        $this->processHeartLocation = $this->processLocation . &#39;/&#39; . $this->_pid . &#39;/heart&#39;;
    }
}


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)