Home > Backend Development > PHP Tutorial > PHP implementation of program singleton operation_PHP tutorial

PHP implementation of program singleton operation_PHP tutorial

WBOY
Release: 2016-07-13 10:19:52
Original
1124 people have browsed it

PHP implements program singleton operation

1. Scene description:

Recently, in our business, we need to constantly monitor changes in a directory. If there are files in the directory, start a PHP script to process them. The initial solution is to use crontab to execute the sh script. The script is roughly as follows:

SOK=`ps -ef |grep /www/sender.sh | grep -v grep|wc -l`
if [[ $SOK < 2 ]];then
        for f in `ls /www/queue`; do
                        php /www/logsender.php /www/queue/$f
        done
Copy after login


An exception occurred during actual operation: the method of ps -ef | grep xxx may not be able to correctly determine whether the process is executing, and the if condition will never be established, causing the PHP script to never be executed. After consideration, we decided to create a class that is independent of other modules and can implement singleton operation of the process to solve this problem.

2. Scheme design

1. Implement process singleton through PID file

2. Automatically create and delete PID files when the program starts and exits, so that no business code is required to consider PID file deletion

3. Try to ensure code independence without affecting business code

3. Principle

1. Start creating PID file

2. Bind semaphores such as program exit and kill, used to delete PID files

3. Add a destructor and delete the PID file when the object is destroyed

4. Problems encountered

When the program exits normally, the semaphore cannot be captured. I don’t know if the semaphore is selected wrongly. Signals such as ctrl+c are normal. If the semaphore can be captured when the program exits normally, it can be replaced by destruction. Function solution, more stable

5. Code

<!--?php
/**
 * Created by PhpStorm.
 * User: huyanping
 * Date: 14-8-13
 * Time: 下午2:25
 *
 * 实现程序单例运行,调用方式:
 * declare(ticks = 1);//注意:一定要在外部调用文件中首部调用该声明,否则程序会无法监听到信号量
 * $single = new DaemonSingle(__FILE__);
 * $single--->single();
 *
 */
 
class DaemonSingle {
 
    //PID文件路径
    private $pid_dir;
 
    //PID文件名称
    private $filename;
 
    //PID文件完整路径名称
    private $pid_file;
 
    /**
     * 构造函数
     * @param $filename
     * @param string $pid_dir
     */
    public function __construct($filename, $pid_dir=&#39;/tmp/&#39;){
        if(empty($filename)) throw new JetException(&#39;filename cannot be empty...&#39;);
        $this->filename = $filename;
        $this->pid_dir = $pid_dir;
        $this->pid_file = $this->pid_dir . DIRECTORY_SEPARATOR . substr(basename($this->filename), 0, -4) . &#39;.pid&#39;;
    }
 
    /**
     * 单例模式启动接口
     * @throws JetException
     */
    public function single(){
        $this->check_pcntl();
        if(file_exists($this->pid_file)) {
            throw new Exception(&#39;the process is already running...&#39;);
        }
        $this->create_pid_file();
    }
 
    /**
     * @throws JetException
     */
    private function create_pid_file()
    {
        if (!is_dir($this->pid_dir)) {
            mkdir($this->pid_dir);
        }
        $fp = fopen($this->pid_file, &#39;w&#39;);
        if(!$fp){
            throw new Exception(&#39;cannot create pid file...&#39;);
        }
        fwrite($fp, posix_getpid());
        fclose($fp);
        $this->pid_create = true;
    }
 
    /**
     * 环境检查
     * @throws Exception
     */
    public function check_pcntl()
    {
        // Make sure PHP has support for pcntl
        if (!function_exists(&#39;pcntl_signal&#39;)) {
            $message = &#39;PHP does not appear to be compiled with the PCNTL extension.  This is neccesary for daemonization&#39;;
            throw new Exception($message);
        }
        //信号处理
        pcntl_signal(SIGTERM, array(&$this, signal_handler));
        pcntl_signal(SIGINT, array(&$this, signal_handler));
        pcntl_signal(SIGQUIT, array(&$this, signal_handler));
 
        // Enable PHP 5.3 garbage collection
        if (function_exists(&#39;gc_enable&#39;)) {
            gc_enable();
            $this->gc_enabled = gc_enabled();
        }
    }
 
    /**
     * 信号处理函数,程序异常退出时,安全删除PID文件
     * @param $signal
     */
    public function signal_handler($signal)
    {
        switch ($signal) {
            case SIGINT :
            case SIGQUIT:
            case SIGTERM:{
                self::safe_quit();
                break;
            }
        }
    }
 
    /**
     * 安全退出,删除PID文件
     */
    public function safe_quit()
    {
        if (file_exists($this->pid_file)) {
            $pid = intval(posix_getpid());
            $file_pid = intval(file_get_contents($this->pid_file));
            if($pid == $file_pid){
                unlink($this->pid_file);
            }
        }
        posix_kill(0, SIGKILL);
        exit(0);
    }
 
    /**
     * 析构函数,删除PID文件
     */
    public function __destruct(){
            $this->safe_quit();
    }
}
Copy after login



www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/871198.htmlTechArticlePHP implementation program singleton operation 1. Scenario description: Recently, our business needs to constantly monitor changes in a directory , if there are files in the directory, start the PHP script to process them. Most...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template