-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.php
61 lines (44 loc) · 1.23 KB
/
watcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
$dir = dirname( __FILE__ ).'/';
$restart_node = true;
$sockets = array();
$folders = array();
$read = array();
$write = NULL;
$except = NULL;
function watch_dir( $dir ) {
global $sockets, $folders;
$inf = inotify_init();
stream_set_blocking( $inf, 1 );
inotify_add_watch( $inf, $dir, IN_MODIFY | IN_CREATE );
$i = count( $sockets );
$sockets[$i] = $inf;
$folders[$i] = dirname( $dir );
}
watch_dir( $dir.'server' );
watch_dir( $dir.'server.js' );
do {
sleep( 1 );
foreach( $read as $inf ) {
$changes = inotify_read( $inf );
foreach( $changes as $change ) {
$i = array_search( $inf, $sockets );
$folder = $folders[$i];
if( $change['mask'] & IN_ISDIR ) {
$dirname = $folder . DIRECTORY_SEPARATOR . $change['name'];
if( !in_array( $dirname, $folders )) watch_dir( $dirname );
} else {
$restart_node = true;
}
}
}
if( $restart_node ) {
foreach( explode( "\n", `ps ax|grep server` ) as $line)
if( preg_match( '/^\s*(\d+)\s.*server.js$/', $line, $proc )) `kill {$proc[1]}`;
sleep( 1 );
$restart_node = false;
`node {$dir}server.js > {$dir}logs/nodejs.log &`;
echo date( '[d.m.Y H:i:s] ')."Restarted nodejs\n";
}
$read = $sockets;
} while( stream_select( $read, $write, $except, NULL ));