当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP实现程序单例运行

PHP实现程序单例运行

2018年10月25日  | 移动技术网IT编程  | 我要评论

 

一、场景描述:

最近我们一块业务,需要不断的监听一个目录的变化,如果目录中有文件,则启动php脚本处理掉。最初的方案是使用crontab执行sh脚本,脚本大概如下:

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


实际运行中出现了异常:ps -ef | grep xxx的方式,可能无法正确的判断进程是否正在执行,if条件永远都不会成立,使得php脚本永远不会执行。经过考虑后,决定建立一个独立于其他模块的,能够实现进程单例运行的类,解决这个问题。

二、方案设计

1、通过pid文件实现进程单例

2、程序启动、退出自动创建、删除pid文件,做到不需要业务代码考虑pid文件删除

3、尽量保证代码独立性,不影响业务代码

三、原理

1、启动创建pid文件

2、绑定程序退出、被杀死等信号量,用于删除pid文件

3、添加析构函数,对象被销毁时,删除pid文件

四、遇到的问题

程序正常退出时,无法捕获到信号量,不知道是不是信号量选错了,ctrl+c等信号是正常的,如果可以解决捕获程序正常退出时的信号量,则可以替代析构函数方案,更加稳定

五、代码
 

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='/tmp/'){
        if(empty($filename)) throw new jetexception('filename cannot be empty...');
        $this->filename = $filename;
        $this->pid_dir = $pid_dir;
        $this->pid_file = $this->pid_dir . directory_separator . substr(basename($this->filename), 0, -4) . '.pid';
    }
 
    /**
     * 单例模式启动接口
     * @throws jetexception
     */
    public function single(){
        $this->check_pcntl();
        if(file_exists($this->pid_file)) {
            throw new exception('the process is already running...');
        }
        $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, 'w');
        if(!$fp){
            throw new exception('cannot create pid file...');
        }
        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('pcntl_signal')) {
            $message = 'php does not appear to be compiled with the pcntl extension.  this is neccesary for daemonization';
            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('gc_enable')) {
            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();
    }
}



 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网