起因
由于最近数据量指数级增长,导致php数据分析定时任务无法在规定的时间内执行完成,这导致了定时任务重复调用的请,结果是数据库直接崩溃,为了确保此类事件不再发生,需要对数据分析进程监控,确保在定时任务未完成的情况下不再调用相同的定时任务。
解决方案
-
- 1.最大限度利用,定时任务每分钟执行一次
- 2.分析进程唯一,可以查看定时任务执行日志
- 3.代码块
protected function pidPath() { $name='pidname'; $root=dirname(__DIR__); return $root."/".$name.".pid"; } protected function checkPid() { $path=$this->pidPath(); $pid=@file_get_contents($path); $pid=intval($pid); if($pid>0) { return $pid; } else { return 0; } } protected function setPid($id=null) { if($id===null) { $id=posix_getpid(); } file_put_contents($this->pidPath(),$id); } public function run(){ if($this->checkPid()>0) { echo "this script has running now"; return; } $this->setPid(); try{ $cmd = "cmd here"; exec($cmd. " >>/var/log/pidname.log $this->setPid(0); }catch (Exception $ex){ echo "Error:".$ex->getMessage(); $this->setPid(0); } }