diff --git a/apphouse.js b/apphouse.js index 432050c..9627cd5 100644 --- a/apphouse.js +++ b/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); @@ -32,8 +43,6 @@ process.on('SIGHUP', function() { }) process.on('SIGINT', function() { - console.log('Stopping'); - /* Stop all application */ appMgr.stopAll(function() { process.exit(1); diff --git a/lib/app-manager.js b/lib/app-manager.js index c6f7157..64e1f83 100644 --- a/lib/app-manager.js +++ b/lib/app-manager.js @@ -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; @@ -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': @@ -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 ]); - } + }); }); } }); @@ -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(); @@ -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) @@ -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(); @@ -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); } }; diff --git a/lib/app-worker/syscall/manager.js b/lib/app-worker/syscall/manager.js index aa0b478..768e9c5 100644 --- a/lib/app-worker/syscall/manager.js +++ b/lib/app-worker/syscall/manager.js @@ -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); @@ -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) @@ -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); diff --git a/lib/runtime.js b/lib/runtime.js index 0a98572..fec022f 100644 --- a/lib/runtime.js +++ b/lib/runtime.js @@ -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(); + }); }); }; diff --git a/logs/system/apphouse.log b/logs/system/apphouse.log deleted file mode 100644 index e69de29..0000000