git vor 2 Jahren
Ursprung
Commit
c20c67a8d6
2 geänderte Dateien mit 198 neuen und 191 gelöschten Zeilen
  1. 194 0
      Home/Lib/Action/InotifyMonitor.php
  2. 4 191
      Home/Lib/Action/ListeningFileCreateAction.class.php

+ 194 - 0
Home/Lib/Action/InotifyMonitor.php

@@ -0,0 +1,194 @@
+<?php
+class InotifyMonitor {
+             
+             const MONITOR_EVENT  = IN_CLOSE_WRITE;
+             const EVENT_MASK  = [
+                 IN_ACCESS => 'File was accessed (read)',
+                 IN_MODIFY => 'File was modified',
+                 IN_ATTRIB => 'Metadata changed',
+                 IN_CLOSE_WRITE => 'File opened for writing was closed',
+                 IN_CLOSE_NOWRITE => 'File not opened for writing was closed',
+                 IN_OPEN => 'File was opened',
+                 IN_MOVED_TO => 'File moved into watched directory',
+                 IN_MOVED_FROM => 'File moved out of watched directory',
+                 IN_CREATE => 'File or directory created in watched directory',
+                 IN_DELETE => 'File or directory deleted in watched directory',
+                 IN_DELETE_SELF => 'Watched file or directory was deleted',
+                 IN_MOVE_SELF => 'Watch file or directory was moved',
+                 IN_CLOSE => 'Equals to IN_CLOSE_WRITE | IN_CLOSE_NOWRITE',
+                 IN_MOVE => 'Equals to IN_MOVED_FROM | IN_MOVED_TO',
+                 IN_ALL_EVENTS => 'Bitmask of all the above constants',
+                 IN_UNMOUNT => 'File system containing watched object was unmounted',
+                 IN_Q_OVERFLOW => 'Event queue overflowed (wd is -1 for this event)',
+                 IN_IGNORED => 'Watch was removed (explicitly by inotify_rm_watch() or because file was removed or filesystem unmounted',
+                 IN_ISDIR => 'Subject of this event is a directory',
+                 IN_ONLYDIR => 'Only watch pathname if it is a directory',
+                 IN_DONT_FOLLOW => 'Do not dereference pathname if it is a symlink',
+                 IN_MASK_ADD => 'Add events to watch mask for this pathname if it already exists',
+                 IN_ONESHOT => 'Monitor pathname for one event, then remove from watch list.',
+                 1073741840 => 'High-bit: File not opened for writing was closed',
+                 1073741856 => 'High-bit: File was opened',
+                 1073742080 => 'High-bit: File or directory created in watched directory',
+                 1073742336 => 'High-bit: File or directory deleted in watched directory',
+             ];
+          
+             public $fds  = [];
+              
+             public $paths  = [];
+              
+             public $wds = [];
+              
+             public $timeout  = 3;
+             
+             public $redis=null;
+          
+           
+             public  function __construct( $paths,$ip='127.0.0.1',$password='',$post=6379){
+                 if (!empty($paths)) {
+                     foreach ($paths as $path) {
+                         if (file_exists($path)) {
+                             if (is_dir($path)) {
+                                 $this->addDir($path);
+                             } else {
+                                 $this->addFile($path);
+                             }
+                         }
+                     }
+                 }
+                //  if (!empty($post_url)) {
+                //      $this->url = $post_url;
+                //  }
+                $this->redis = new Redis();
+                $this->redis->pconnect($ip,$post,2.5);
+                $this->redis->auth($password); //设置密码
+                $this->redis->select(1);
+                $result = $this->redis->ping();
+                echo "redis连接结果=>".$result.PHP_EOL;
+             }
+             public  function __destruct(  ){
+                 if (!empty($this->fds)) {
+                     foreach ($this->fds as $fd) {
+                         fclose($fd);
+                     }
+                 }
+             }
+              
+           
+             public  function addFile( $file ){
+                 $file = realpath($file);
+                 $fd = inotify_init();
+                 $fid = (int)$fd;
+                 //保存inotify资源
+                 $this->fds[$fid] = $fd;
+                 //设置为非阻塞模式
+                 stream_set_blocking($this->fds[$fid], 0);
+                 //保存文件路径
+                 $this->paths[$fid] = $file;
+                 //保存监控描述�?
+                 $this->wds[$fid] =inotify_add_watch($this->fds[$fid], $file, self::MONITOR_EVENT);
+             }
+              
+           
+             public  function addDir( $dir ){
+                 $dir = realpath($dir);
+                 if ($dh = opendir($dir)) {
+                     //将目录加入监控中
+                     $fd = inotify_init();
+                     //一般文件的资源描述符是一个整形,可以用来当索�?
+                     $fid = (int)$fd;
+                     $this->fds[$fid] = $fd;
+                     stream_set_blocking($this->fds[$fid], 0);
+                     $this->paths[$fid] = $dir;
+                     $this->wds[$fid] = inotify_add_watch($this->fds[$fid], $dir, self::MONITOR_EVENT);
+                     //遍历目录下文�?
+                     while (($file = readdir($dh)) !== false) {
+                         if ($file == '.' || $file == '..') {
+                             continue;
+                         }
+                         $file = $dir . DIRECTORY_SEPARATOR . $file;
+                         if (is_dir($file)) {
+                             $this->addDir($file);
+                         }
+                     }
+                     closedir($dh);
+                 }
+             }
+              
+           
+             public  function remove( $fid ){
+                 unset($this->paths[$fid]);
+                 fclose($this->fds[$fid]);
+                 unset($this->fds[$fid]);
+             }
+              
+           
+             public  function run(  ){
+                echo '我开始运行了'.PHP_EOL;
+                
+                 while (true) {
+                     $reads = $this->fds;
+                     $write = [];
+                     $except = [];//异常事件
+                     if (stream_select($reads, $write, $except, $this->timeout) > 0) {
+                         if (!empty($reads)) {
+                          
+                             foreach ($reads as $read) {
+                                 //从可读流中读取数�?
+                                 $events = inotify_read($read);
+                                 //资源描述符,整形
+                                 $fid = (int)$read;
+                                 //获取inotify实例的路�?
+                                 $path = $this->paths[$fid];
+                                 foreach ($events as $event) {
+                                    echo '监听读取'.PHP_EOL;
+                                   
+                                     $file = $path .'/' . $event['name'];
+                                     
+                                     if ($event['mask']==IN_CLOSE_WRITE) {
+                                         echo 'create file->'.$file.PHP_EOL;
+                                
+                                         //写入kafka
+                                         $this->set_redis($file);
+                                         //$this->postData($data);
+                                     }
+                                      
+                                     //echo $event['name'], ' --- ', self::EVENT_MASK[$event['mask']], PHP_EOL;
+                                 }
+                             }
+                         }
+                         if (!empty($except)) {
+                             //debug_log('push_vidieo_images_log',json_encode($except));//记录下异常事�?
+                         }
+                     } else {
+                         //echo '------------------', PHP_EOL;
+                         //echo '当前监控的路径列�?, PHP_EOL;
+                         //print_r($this->paths);
+                         //echo '------------------', PHP_EOL;
+                     }
+                 }
+             }
+              
+             /**
+              * function
+              * 文件目录
+              * @param [type] $data
+              * @return void
+              */
+             public function set_redis($data)
+             {
+                if(!strstr($data,'md5'))
+                {
+                    $this->redis->lpush('redis_to_kafka',$data);
+                }
+               
+
+             }
+              
+}
+         
+
+        //  //调用函数
+        // $path = "/test";
+        // $inotify =  new InotifyMonitor([$path]);
+        // echo '进入运行�?;
+        // $inotify->run();

+ 4 - 191
Home/Lib/Action/ListeningFileCreateAction.class.php

@@ -7,198 +7,11 @@ class ListeningFileCreateAction extends Action {
 
 
  
-	private  function InotifyMonitor( $path ){
-		return new class([$path]){
-			  const MONITOR_EVENT  = IN_CLOSE_WRITE;
-		             const EVENT_MASK  = [
-		                 IN_ACCESS => 'File was accessed (read)',
-		                 IN_MODIFY => 'File was modified',
-		                 IN_ATTRIB => 'Metadata changed',
-		                 IN_CLOSE_WRITE => 'File opened for writing was closed',
-		                 IN_CLOSE_NOWRITE => 'File not opened for writing was closed',
-		                 IN_OPEN => 'File was opened',
-		                 IN_MOVED_TO => 'File moved into watched directory',
-		                 IN_MOVED_FROM => 'File moved out of watched directory',
-		                 IN_CREATE => 'File or directory created in watched directory',
-		                 IN_DELETE => 'File or directory deleted in watched directory',
-		                 IN_DELETE_SELF => 'Watched file or directory was deleted',
-		                 IN_MOVE_SELF => 'Watch file or directory was moved',
-		                 IN_CLOSE => 'Equals to IN_CLOSE_WRITE | IN_CLOSE_NOWRITE',
-		                 IN_MOVE => 'Equals to IN_MOVED_FROM | IN_MOVED_TO',
-		                 IN_ALL_EVENTS => 'Bitmask of all the above constants',
-		                 IN_UNMOUNT => 'File system containing watched object was unmounted',
-		                 IN_Q_OVERFLOW => 'Event queue overflowed (wd is -1 for this event)',
-		                 IN_IGNORED => 'Watch was removed (explicitly by inotify_rm_watch() or because file was removed or filesystem unmounted',
-		                 IN_ISDIR => 'Subject of this event is a directory',
-		                 IN_ONLYDIR => 'Only watch pathname if it is a directory',
-		                 IN_DONT_FOLLOW => 'Do not dereference pathname if it is a symlink',
-		                 IN_MASK_ADD => 'Add events to watch mask for this pathname if it already exists',
-		                 IN_ONESHOT => 'Monitor pathname for one event, then remove from watch list.',
-		                 1073741840 => 'High-bit: File not opened for writing was closed',
-		                 1073741856 => 'High-bit: File was opened',
-		                 1073742080 => 'High-bit: File or directory created in watched directory',
-		                 1073742336 => 'High-bit: File or directory deleted in watched directory',
-		             ];
-		          
-		             public $fds  = [];
-		              
-		             public $paths  = [];
-		              
-		             public $wds = [];
-		              
-		             public $timeout  = 3;
-		             
-		             public $redis=null;
-		          
-		           
-		             public  function __construct( $paths ){
-		                 if (!empty($paths)) {
-		                     foreach ($paths as $path) {
-		                         if (file_exists($path)) {
-		                             if (is_dir($path)) {
-		                                 $this->addDir($path);
-		                             } else {
-		                                 $this->addFile($path);
-		                             }
-		                         }
-		                     }
-		                 }
-		                //  if (!empty($post_url)) {
-		                //      $this->url = $post_url;
-		                //  }
-		                $this->redis = new Redis();
-		                $this->redis->pconnect('127.0.0.1', 6379,2.5);
-		                $this->redis->select(1);
-		                $result = $this->redis->ping();
-		                echo "redis连接结果=>".$result.PHP_EOL;
-		             }
-		             public  function __destruct(  ){
-		                 if (!empty($this->fds)) {
-		                     foreach ($this->fds as $fd) {
-		                         fclose($fd);
-		                     }
-		                 }
-		             }
-		              
-		           
-		             public  function addFile( $file ){
-		                 $file = realpath($file);
-		                 $fd = inotify_init();
-		                 $fid = (int)$fd;
-		                 //保存inotify资源
-		                 $this->fds[$fid] = $fd;
-		                 //设置为非阻塞模式
-		                 stream_set_blocking($this->fds[$fid], 0);
-		                 //保存文件路径
-		                 $this->paths[$fid] = $file;
-		                 //保存监控描述�?
-		                 $this->wds[$fid] =inotify_add_watch($this->fds[$fid], $file, self::MONITOR_EVENT);
-		             }
-		              
-		           
-		             public  function addDir( $dir ){
-		                 $dir = realpath($dir);
-		                 if ($dh = opendir($dir)) {
-		                     //将目录加入监控中
-		                     $fd = inotify_init();
-		                     //一般文件的资源描述符是一个整形,可以用来当索�?
-		                     $fid = (int)$fd;
-		                     $this->fds[$fid] = $fd;
-		                     stream_set_blocking($this->fds[$fid], 0);
-		                     $this->paths[$fid] = $dir;
-		                     $this->wds[$fid] = inotify_add_watch($this->fds[$fid], $dir, self::MONITOR_EVENT);
-		                     //遍历目录下文�?
-		                     while (($file = readdir($dh)) !== false) {
-		                         if ($file == '.' || $file == '..') {
-		                             continue;
-		                         }
-		                         $file = $dir . DIRECTORY_SEPARATOR . $file;
-		                         if (is_dir($file)) {
-		                             $this->addDir($file);
-		                         }
-		                     }
-		                     closedir($dh);
-		                 }
-		             }
-		              
-		           
-		             public  function remove( $fid ){
-		                 unset($this->paths[$fid]);
-		                 fclose($this->fds[$fid]);
-		                 unset($this->fds[$fid]);
-		             }
-		              
-		           
-		             public  function run(  ){
-		       
-		                 while (true) {
-		                     $reads = $this->fds;
-		                     $write = [];
-		                     $except = [];//异常事件
-		                     if (stream_select($reads, $write, $except, $this->timeout) > 0) {
-		                         if (!empty($reads)) {
-		                           
-		                             foreach ($reads as $read) {
-		                                 //从可读流中读取数�?
-		                                 $events = inotify_read($read);
-		                                 //资源描述符,整形
-		                                 $fid = (int)$read;
-		                                 //获取inotify实例的路�?
-		                                 $path = $this->paths[$fid];
-		                                 foreach ($events as $event) {
-		                                    echo '监听读取'.PHP_EOL;
-		                                   
-		                                     $file = $path .'/'. $event['name'];
-		                                     
-		                                     if ($event['mask']==IN_CLOSE_WRITE) {
-		                                        echo '记录一个文件';
-		                                        //  $file = iconv('gb2312', 'UTF-8', $file);
-		                                         echo 'create file->'.$file.PHP_EOL;
-		                                
-		                                         //写入kafka
-		                                         $this->set_redis($file);
-		                                         //$this->postData($data);
-		                                     }
-		                                      
-		                                     //echo $event['name'], ' --- ', self::EVENT_MASK[$event['mask']], PHP_EOL;
-		                                 }
-		                             }
-		                         }
-		                         if (!empty($except)) {
-		                             //debug_log('push_vidieo_images_log',json_encode($except));//记录下异常事�?
-		                         }
-		                     } else {
-		                         //echo '------------------', PHP_EOL;
-		                         //echo '当前监控的路径列�?, PHP_EOL;
-		                         //print_r($this->paths);
-		                         //echo '------------------', PHP_EOL;
-		                     }
-		                 }
-		             }
-		              
-		             /**
-		              * function
-		              * 文件目录
-		              * @param [type] $data
-		              * @return void
-		              */
-		             public function set_redis($data)
-		             {
-		                $this->redis->lpush('redis_to_kafka',$data);
-		
-		             }
-		              
-			
-			
-			
-			
-			
-		}
-	}
-	
- 
 	public  function start_listening(  ){
-		$this->InotifyMonitor('/test')->run();
+		include('InotifyMonitor.php');
+				//文件夹,ip,密码,端口
+		$test = new InotifyMonitor(['/test'],'192.168.1.105','',6379);
+		$test->run();
 	}