Skip to content

Commit

Permalink
Merge pull request #621 from soyuka/ghost-processes
Browse files Browse the repository at this point in the history
Attempt to fix #613
  • Loading branch information
Unitech committed Aug 8, 2014
2 parents 2ab6e20 + 090c032 commit 826e106
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 43 deletions.
2 changes: 2 additions & 0 deletions lib/Satan.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ Satan.executeRemote = function(method, env, fn) {
//stop watching when process is deleted
if (method.indexOf('delete') !== -1) {
Satan.stopWatch(method, env, fn);
} else if(method.indexOf('kill') !== -1) {
Satan.stopWatch('deleteAll', env, fn);
} else if (process.argv.indexOf('--watch') !== -1 && method.indexOf('stop') !== -1) {
Satan.stopWatch(method, env, fn);
} else if (process.argv.indexOf('--watch') !== -1 && method.indexOf('restart') !== -1) {
Expand Down
30 changes: 24 additions & 6 deletions lib/Watcher.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var chokidar = require('chokidar');
var p = require('path');
var util = require('util');
var log = require('debug')('pm2:watch')

module.exports = {
watchers: [],
Expand Down Expand Up @@ -54,11 +55,28 @@ module.exports = {
}
)
.on('all', function(event, path) {
console.log('File changed. Reloading.');
require('./God').restartProcessId(pm2_env.pm_id, function(err, list) {
console.log('Process restarted');
});
var self = this;

log('File changed, reloading');

if(self.restarting === true) {
log('Already restarting skipping');
return;
} else {

self.restarting = true;

require('./God').restartProcessId(pm2_env.pm_id, function(err, list) {
self.restarting = false;

if(err) {
log('Error while restarting', err);
} else {
log('Process restarted');
}
});

}
});


Expand All @@ -73,9 +91,9 @@ module.exports = {
* @return
*/
close: function(id) {
if(this.watchers[id])
if(this.watchers[id]) {
return this.watchers[id].close();
else {
} else {
return false;
}
}
Expand Down
79 changes: 42 additions & 37 deletions test/bash/watch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,45 @@ source "${SRC}/include.sh"

cd $file_path

function waituntil {
for (( i = 0; i <= $1; i++ )); do
sleep 0.2
echo -n "."
done
echo ""
}

echo -e "\033[1mRunning tests:\033[0m"

#####################
# Watch for changes #
#####################
if [ -f server-watch.js ]; then
rm server-watch.js
fi

$pm2 kill

sleep 1

cp server-watch.bak.js server-watch.js

$pm2 start --watch server-watch.js

sleep 1

echo "setTimeout(function() { console.log('still ok') }, 200)" >> server-watch.js
should 'process should be watched' 'watch: true' 1

for (( i = 0; i <= 10; i++ )); do
sleep 0.2
echo -n "."
done
echo "console.log('test');" >> server-watch.js

$pm2 list

should 'process should have been restarted' 'restart_time: 1' 1
should 'process should be online' "status: 'online'" 1

$pm2 kill
rm server-watch.js

sleep 1

rm server-watch.js

###############

Expand Down Expand Up @@ -72,8 +84,6 @@ cp server-watch.bak.js server-watch.js

$pm2 start --watch server-watch.js

sleep 1

$pm2 restart 0

should 'process should be watched' 'watch: true' 1
Expand All @@ -82,19 +92,8 @@ $pm2 stop --watch 0

should 'process should have stopped beeing watched' 'watch: false' 1

$pm2 list

echo "setInterval(function() { console.log('still ok'); }, 100);" > server-watch.js

cat server-watch.js

for (( i = 0; i <= 10; i++ )); do
sleep 0.1
echo -n "."
done

$pm2 list

should 'process should not have been restarted on file change' 'restart_time: 1' 1

cp server-watch.bak.js server-watch.js
Expand All @@ -104,32 +103,19 @@ $pm2 restart 0
should 'process should restart and not be watched' 'watch: false' 1

$pm2 restart --watch 0
should 'process should be watched' 'watch: true' 1

should 'process should restart and be watched' 'watch: true' 1

$pm2 kill
sleep 1

echo "setTimeout(function() { console.log('watch me!') })" >> server-watch.js

for (( i = 0; i <= 10; i++ )); do
sleep 0.2
echo -n "."
done

$pm2 list

should 'process should have restart because of a file change' 'restart_time: 4' 1

$pm2 kill
rm server-watch.js

#############
# JSON test #
#############
# we've already seen before that "watch: true" is really watching when changing a file

$pm2 start --watch all.json
sleep 1

should 'processes should be watched' 'watch: true' 8

$pm2 stop --watch all
Expand All @@ -140,3 +126,22 @@ $pm2 restart --watch all
should 'processes should be watched' 'watch: true' 8

$pm2 kill

##########
# delete #
##########

cp server-watch.bak.js server-watch.js

$pm2 start server-watch.js --watch
$pm2 stop 0
$pm2 delete 0

echo "setTimeout(function() { console.log('watch me!') })" >> server-watch.js

waituntil 10

should 'process should not have been restarted' 'watch: true' 0

$pm2 kill
rm server-watch.js

0 comments on commit 826e106

Please sign in to comment.