Skip to content

Commit

Permalink
Fixed a bug that not clear runtime environment when shutdown service
Browse files Browse the repository at this point in the history
  • Loading branch information
cfsghost committed Apr 7, 2012
1 parent b4aa791 commit 28c902d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 47 deletions.
25 changes: 17 additions & 8 deletions apphouse.js
@@ -1,19 +1,30 @@
var config = require('config');
var AppHouse = require('./lib/apphouse.js');
var path = require('path');
var fs = require('fs');

/* Config */
config.path.app = config.path.app || path.join(__dirname, 'apps');
config.path.log = config.path.log || path.join(__dirname, 'logs');
config.path.runtime = config.path.runtime || path.join(__dirname, 'runtime');

/* Initializing Application Manager */
var appMgr = new AppHouse.AppManager(config.path.app, config.path.log, config.path.runtime, config.admin.tools);
appMgr.runAll();
/* Initializing */
var appMgr;
var router;

/* Initializing Router */
var router = new AppHouse.Router(appMgr);
router.run();
/* Initializing log directory */
fs.mkdir(config.path.log, function() {
fs.mkdir(path.join(config.path.log, 'apps'), function() {

/* Initializing Application Manager */
appMgr = new AppHouse.AppManager(config.path.app, config.path.log, config.path.runtime, config.admin.tools);
appMgr.runAll();

/* Initializing Router */
router = new AppHouse.Router(appMgr);
router.run();
});
});

process.on('uncaughtException', function(err) {
console.err(err.stack);
Expand All @@ -32,8 +43,6 @@ process.on('SIGHUP', function() {
})

process.on('SIGINT', function() {
console.log('Stopping');

/* Stop all application */
appMgr.stopAll(function() {
process.exit(1);
Expand Down
86 changes: 53 additions & 33 deletions lib/app-manager.js
Expand Up @@ -30,8 +30,10 @@ module.exports = function(_path, _logpath, _runtimepath, _tools) {
var priv = Priv.instanceList[id];

/* Start Worker */
console.log('Initializing ' + App.appName + '.' + id);
var instProcess = priv.process = child_process.fork(workerPath);

var aaa = 111;
/* Auto-restart */
instProcess.on('exit', function(code) {
var delay = 3000;
Expand Down Expand Up @@ -128,10 +130,12 @@ module.exports = function(_path, _logpath, _runtimepath, _tools) {

case 'appStarted':
instance.state = 'running';
console.log('Started ' + App.appName + '.' + id);

/* Application was started, call callback function */
if (callback)
callback();

break;

case 'appInitError':
Expand Down Expand Up @@ -204,24 +208,20 @@ module.exports = function(_path, _logpath, _runtimepath, _tools) {
/* Initializing runtime environment */
runtime.init(id, function(runtimeDir) {
runtime.initApp(id, appRealDir, function() {
var managerAPIPath = path.join(runtimeDir, 'AppHouseManager');

/* Export Manager API to this App */
var targetLogPath = path.join(runtimeDir, '.AppHouse_logs');
path.exists(targetLogPath, function(exists) {
if (exists) {
child_process.spawn('umount', [ targetLogPath ]);
fs.unlink(targetLogPath);
}
var targetLogPath = path.join(managerAPIPath, 'Logs');
var umount = child_process.spawn('umount', [ targetLogPath ]);
umount.on('exit', function() {
fs.unlink(targetLogPath);

/* Mounting to export log directory if this app is a manager */
if (App.isManager) {
path.exists(targetLogPath, function(exists) {
if (!exists) {
fs.mkdir(targetLogPath, function() {
child_process.spawn('mount', [ '--bind', logPath, targetLogPath ]);
});
} else {
fs.mkdir(managerAPIPath, function() {
fs.mkdir(targetLogPath, function() {
child_process.spawn('mount', [ '--bind', logPath, targetLogPath ]);
}
});
});
}
});
Expand Down Expand Up @@ -352,6 +352,8 @@ module.exports = function(_path, _logpath, _runtimepath, _tools) {
var instance = App.instanceList[id];
var priv = Priv.instanceList[id];

console.log('Stopping ' + App.appName + '.' + id);

if (instance.state == 'stop') {
if (callback)
callback();
Expand All @@ -376,10 +378,45 @@ module.exports = function(_path, _logpath, _runtimepath, _tools) {
return;
}

/* update state */
instance.state = 'stop';

if (callback)
callback();
};

this._stopApp = function(id, callback) {
var App = self.applications[id];

/* update state */
App.state = 'stop';

/* Release runtime environment */
var runtimeDir = runtime.getRuntimeDir(id);

if (App.isManager) {
/* Directory for manager API */
var managerAPIPath = path.join(runtimeDir, 'AppHouseManager');

/* Release log directory of Manager API */
var targetLogPath = path.join(managerAPIPath, 'Logs');
var umount = child_process.spawn('umount', [ targetLogPath ]);
umount.on('exit', function() {
/* Release runtime directory now */
runtime.release(id, function() {
if (callback)
callback();
});
});
} else {
/* Release runtime directory now */
runtime.release(id, function() {
if (callback)
callback();
});
}
};

this.stopApp = function(id, callback) {
if (!self.applications[id]) {
if (callback)
Expand All @@ -388,6 +425,8 @@ module.exports = function(_path, _logpath, _runtimepath, _tools) {
return;
}

console.log('Shutting down ' + self.applications[id].appName);

if (!self.applications[id].state == 'stop') {
if (callback)
callback();
Expand All @@ -409,26 +448,7 @@ module.exports = function(_path, _logpath, _runtimepath, _tools) {
if (index < self.applications[id].instanceList.length) {
self.stopInstance(index, self.applications[id], self.appPriv[id], sicb);
} else {
/* update state */
self.applications[id].state = 'stop';

/* Release runtime environment */
var runtimeDir = runtime.getRuntimeDir(id);

/* Release log directory of Manager API */
var targetLogPath = path.join(runtimeDir, '.AppHouse_logs');
path.exists(targetLogPath, function(exists) {
if (exists) {
child_process.spawn('umount', [ targetLogPath ]);
fs.unlink(targetLogPath);
}

/* Release runtime directory now */
runtime.release(id, function() {
if (callback)
callback();
});
});
self._stopApp(id, callback);
}
};

Expand Down
5 changes: 3 additions & 2 deletions lib/app-worker/syscall/manager.js
Expand Up @@ -5,6 +5,7 @@ module.exports = function(_process, _ipc) {
var self = this;
var workerProcess = _process;
var ipc = _ipc;
var managerPath = path.join('/', 'AppHouseManager');

this.getAppInfo = function(id, callback) {
var callbackID = ipc.register(callback);
Expand Down Expand Up @@ -63,7 +64,7 @@ module.exports = function(_process, _ipc) {
this.watchLog = function(id, callback) {
self.getAppInfo(id, function(app) {
var startByte = 0;
var appLogfile = path.join('/.AppHouse_logs', 'apps', path.basename(app.appLogPath));
var appLogfile = path.join(managerPath, 'Logs', 'apps', path.basename(app.appLogPath));

fs.stat(appLogfile, function(err, stats){
if (err)
Expand Down Expand Up @@ -119,7 +120,7 @@ module.exports = function(_process, _ipc) {

this.unwatchLog = function(id) {
self.getAppInfo(id, function(app) {
var appLogfile = path.join('/.AppHouse_logs', 'apps', path.basename(app.appLogPath));
var appLogfile = path.join(managerPath, 'Logs', 'apps', path.basename(app.appLogPath));

/* Unwatch log file */
fs.unwatchFile(appLogfile);
Expand Down
12 changes: 8 additions & 4 deletions lib/runtime.js
Expand Up @@ -68,10 +68,14 @@ module.exports = function(_runtimepath) {
};

this.initLog = function(id, LogPath, callback) {
var link_process = child_process.spawn('ln', [ '-fn', LogPath, path.join(self.getLogDir(id), path.basename(LogPath)) ]);
link_process.on('exit', function(code) {
if (callback)
callback();
fs.open(LogPath, 'a', function(err, fd) {
fs.close(fd);

var link_process = child_process.spawn('ln', [ '-fn', LogPath, path.join(self.getLogDir(id), path.basename(LogPath)) ]);
link_process.on('exit', function(code) {
if (callback)
callback();
});
});
};

Expand Down
Empty file removed logs/system/apphouse.log
Empty file.

0 comments on commit 28c902d

Please sign in to comment.