2015-05-27
一、场景描述:
最近我们一块业务,需要不断的监听一个目录的变化,如果目录中有文件,则启动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(); }}
1
CI框架连接数据库配置操作以及多数据库操作
09-05
2
asp 简单读取数据表并列出来 ASP如何快速从数据库读取大量数据
05-17
3
C语言关键字及其解释介绍 C语言32个关键字详解
04-05
4
C语言中sizeof是什么意思 c语言里sizeof怎样用法详解
04-26
5
PHP中的魔术方法 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep,
09-05
6
将视频设置为Android手机开机动画的教程
12-11
7
PHP中的(++i)前缀自增 和 (i++)后缀自增
09-05
8
常用dos命令及语法
09-27
最简单的asp登陆界面代码 asp登陆界面源代码详细介绍
2017-04-12
PHP中include和require区别之我见
2014-09-05
php递归返回值的问题
2014-09-05
如何安装PHPstorm并配置方法教程 phpstorm安装后要进行哪些配置
2017-05-03
单片机编程好学吗?单片机初学者怎样看懂代码
2022-03-21
PHP 教程之如何使用BLOB存取图片信息实例
2014-09-05
学ug编程如何快速入门?
2022-03-17
学习使用C语言/C++编程的7个步骤!超赞~
2022-03-20
PHP数组函数array
2014-09-05
零基础的初学者怎样学习java,或者应该先学什么?
2022-03-21
像素拼图最新版下载v5.01 安卓版
其它手游 23.76MB
下载
机器人战斗竞技场手机版下载v3.71 安卓版
其它手游 77.19MB
下载
果冻人大乱斗最新版下载v1.1.0 安卓版
其它手游 113.98MB
下载
王者100刀最新版下载v1.2 安卓版
其它手游 72.21MB
下载
trueskate真实滑板正版下载v1.5.102 安卓版
其它手游 83.48MB
下载
矢量跑酷2最新版下载v1.2.1 安卓版
其它手游 119.39MB
下载
休闲解压合集下载v1.0.0 安卓版
其它手游 33.41MB
下载
指尖游戏大师最新版下载v4.0.0 安卓版
其它手游 74.77MB
下载飞天萌猫下载v3.0.3 安卓版
下载
火柴人越狱大逃脱下载v1.1 安卓版
下载
末日战场生死局下载v1.0 安卓版
下载
安静书女孩世界下载v1.0 安卓版
下载
消个寿司下载v1.00 免费版
下载
趣味找猫猫下载v1.1.2 安卓版
下载
方块男孩大战恶霸最新版下载v1.0.18 安卓版
下载
根本难不倒我下载v1.0.1 最新版
下载