Permalink
Browse files

Uniformisation du serveur (besoin de refactoring)

  • Loading branch information...
1 parent 71847c9 commit d887bd56cd6166d2f9da7924a564f2c5635f2b1b @FGRibreau committed Nov 22, 2011
Showing with 199 additions and 46 deletions.
  1. +59 −22 app.coffee
  2. +91 −24 app.js
  3. 0 public/appjs/ProcessListView.js
  4. +49 −0 public/css/style.css
View
@@ -4,6 +4,9 @@ fs = require 'fs'
forever = require 'forever'
_ = require 'underscore'
+process.on "uncaughtException", (err) ->
+ console.log "Caught exception: " + err
+
#todo
class foreverUI
@@ -13,26 +16,35 @@ class foreverUI
findProcessByUID: (uid, cb) ->
forever.list("", (err, processes) ->
- if(err)
- return cb(null)
+ return cb(err, null) if(err)
- cb(_.find(processes, (o) -> o.uid == uid))
+ cb(null, _.find(processes, (o) -> o.uid == uid))
)
- findIndexByUID: (uid, cb) ->
+ findProcIndexByUID: (uid, cb) ->
forever.list("", (err, processes) ->
- if(err)
- return cb(null)
+ return cb(err, null) if err
- cb(_.find(processes, (o) -> o.uid == uid))
+ i = -1
+
+ while processes[++i]
+
+ if(processes[i].uid == uid)
+ return cb(null, i)
+
+ cb("Process '#{uid}' not found", null)
)
# Get process logs
info: (uid, cb) ->
- @findProcessByUID(uid, (proc) ->
+ @findProcessByUID(uid, (err, proc) ->
+
+ return cb(err, null) if err
+
+ return cb("Undefined proc", null) if !proc
async.map([proc.logFile, proc.outFile, proc.errFile].filter((s) -> s != undefined), (filename, cb) ->
@@ -46,18 +58,37 @@ class foreverUI
)
, (err, results) ->
- cb results
+ cb err, results
)
)
+
+ # stop a process by it's uid
stop: (uid, cb) ->
- @findProcessByUID(uid, (proc) ->
+ @findProcIndexByUID(uid, (err, index) ->
+
+ return cb(err, null) if err
+
+ forever.stop(index)
+ .on('stop', (res) -> cb(null, true))
+ .on('error', (err) -> cb(err, null))
)
- cb {}
+ # restart a process by it's uid
restart: (uid, cb) ->
- cb {}
+
+ @findProcIndexByUID(uid, (err, index) ->
+
+ return cb(err, null) if err
+
+ forever.restart(index)
+ .on('restart', (res) ->
+
+ cb(null, true)
+ )
+ .on('error', (err) -> cb(err, null))
+ )
UI = new foreverUI()
@@ -103,30 +134,36 @@ app.get('/refresh/', (req, res) ->
)
app.get('/processes', (req, res) ->
- # Refresh process list (#todo use fs.watch instead)
forever.list("", (err, results) ->
res.send JSON.stringify(results), { 'Content-Type': 'text/javascript' }, 200
)
)
+#todo : refactoring needed here
app.get('/restart/:uid', (req, res) ->
- UI.restart req.params.uid, (res) ->
- res.send JSON.stringify(res), { 'Content-Type': 'text/javascript' }, 200
+ UI.restart req.params.uid, (err, results) ->
+ if err
+ res.send JSON.stringify(status:'error', details:err), { 'Content-Type': 'text/javascript' }, 500
+ else
+ res.send JSON.stringify(status:'success', details:results), { 'Content-Type': 'text/javascript' }, 200
)
app.get('/stop/:uid', (req, res) ->
- UI.stop req.params.uid, (res) ->
- res.send JSON.stringify(res), { 'Content-Type': 'text/javascript' }, 200
+ UI.stop req.params.uid, (err, results) ->
+ if err
+ res.send JSON.stringify(status:'error', details:err), { 'Content-Type': 'text/javascript' }, 500
+ else
+ res.send JSON.stringify(status:'success', details:results), { 'Content-Type': 'text/javascript' }, 200
)
app.get('/info/:uid', (req, res) ->
- UI.info req.params.uid, (infos) ->
- res.send JSON.stringify(infos), { 'Content-Type': 'text/javascript' }, 200
+ UI.info req.params.uid, (err, results) ->
+ if err
+ res.send JSON.stringify(status:'error', details:err), { 'Content-Type': 'text/javascript' }, 500
+ else
+ res.send JSON.stringify(status:'success', details:results), { 'Content-Type': 'text/javascript' }, 200
)
-
-
#todo
app.listen 8085, "127.0.0.1"
-
console.log "Listening on 127.0.0.1:8085"
View
115 app.js
@@ -5,30 +5,44 @@
fs = require('fs');
forever = require('forever');
_ = require('underscore');
+ process.on("uncaughtException", function(err) {
+ return console.log("Caught exception: " + err);
+ });
foreverUI = (function() {
function foreverUI() {}
foreverUI.prototype.findProcessByUID = function(uid, cb) {
return forever.list("", function(err, processes) {
if (err) {
- return cb(null);
+ return cb(err, null);
}
- return cb(_.find(processes, function(o) {
+ return cb(null, _.find(processes, function(o) {
return o.uid === uid;
}));
});
};
- foreverUI.prototype.findIndexByUID = function(uid, cb) {
+ foreverUI.prototype.findProcIndexByUID = function(uid, cb) {
return forever.list("", function(err, processes) {
+ var i;
if (err) {
- return cb(null);
+ return cb(err, null);
}
- return cb(_.find(processes, function(o) {
- return o.uid === uid;
- }));
+ i = -1;
+ while (processes[++i]) {
+ if (processes[i].uid === uid) {
+ return cb(null, i);
+ }
+ }
+ return cb("Process '" + uid + "' not found", null);
});
};
foreverUI.prototype.info = function(uid, cb) {
- return this.findProcessByUID(uid, function(proc) {
+ return this.findProcessByUID(uid, function(err, proc) {
+ if (err) {
+ return cb(err, null);
+ }
+ if (!proc) {
+ return cb("Undefined proc", null);
+ }
return async.map([proc.logFile, proc.outFile, proc.errFile].filter(function(s) {
return s !== void 0;
}), function(filename, cb) {
@@ -42,16 +56,33 @@
}
});
}, function(err, results) {
- return cb(results);
+ return cb(err, results);
});
});
};
foreverUI.prototype.stop = function(uid, cb) {
- this.findProcessByUID(uid, function(proc) {});
- return cb({});
+ return this.findProcIndexByUID(uid, function(err, index) {
+ if (err) {
+ return cb(err, null);
+ }
+ return forever.stop(index).on('stop', function(res) {
+ return cb(null, true);
+ }).on('error', function(err) {
+ return cb(err, null);
+ });
+ });
};
foreverUI.prototype.restart = function(uid, cb) {
- return cb({});
+ return this.findProcIndexByUID(uid, function(err, index) {
+ if (err) {
+ return cb(err, null);
+ }
+ return forever.restart(index).on('restart', function(res) {
+ return cb(null, true);
+ }).on('error', function(err) {
+ return cb(err, null);
+ });
+ });
};
return foreverUI;
})();
@@ -104,24 +135,60 @@
});
});
app.get('/restart/:uid', function(req, res) {
- return UI.restart(req.params.uid, function(res) {
- return res.send(JSON.stringify(res), {
- 'Content-Type': 'text/javascript'
- }, 200);
+ return UI.restart(req.params.uid, function(err, results) {
+ if (err) {
+ return res.send(JSON.stringify({
+ status: 'error',
+ details: err
+ }), {
+ 'Content-Type': 'text/javascript'
+ }, 500);
+ } else {
+ return res.send(JSON.stringify({
+ status: 'success',
+ details: results
+ }), {
+ 'Content-Type': 'text/javascript'
+ }, 200);
+ }
});
});
app.get('/stop/:uid', function(req, res) {
- return UI.stop(req.params.uid, function(res) {
- return res.send(JSON.stringify(res), {
- 'Content-Type': 'text/javascript'
- }, 200);
+ return UI.stop(req.params.uid, function(err, results) {
+ if (err) {
+ return res.send(JSON.stringify({
+ status: 'error',
+ details: err
+ }), {
+ 'Content-Type': 'text/javascript'
+ }, 500);
+ } else {
+ return res.send(JSON.stringify({
+ status: 'success',
+ details: results
+ }), {
+ 'Content-Type': 'text/javascript'
+ }, 200);
+ }
});
});
app.get('/info/:uid', function(req, res) {
- return UI.info(req.params.uid, function(infos) {
- return res.send(JSON.stringify(infos), {
- 'Content-Type': 'text/javascript'
- }, 200);
+ return UI.info(req.params.uid, function(err, results) {
+ if (err) {
+ return res.send(JSON.stringify({
+ status: 'error',
+ details: err
+ }), {
+ 'Content-Type': 'text/javascript'
+ }, 500);
+ } else {
+ return res.send(JSON.stringify({
+ status: 'success',
+ details: results
+ }), {
+ 'Content-Type': 'text/javascript'
+ }, 200);
+ }
});
});
app.listen(8085, "127.0.0.1");
No changes.
View
@@ -0,0 +1,49 @@
+body {
+ padding-top: 10px;
+}
+
+.menubar {
+ position:absolute;
+ bottom:10px;
+}
+
+.menubar.left{
+ left:10px;
+}
+
+.menubar.right{
+ right:10px;
+}
+
+.hidden{display:none}
+.span5{text-align:right;}
+
+.hero-unit{position:relative;}
+
+.row.load {
+ background: no-repeat url(/img/load.gif) 20px 50%;
+ -webkit-transition:background-image 250ms ease-in 500ms;
+}
+
+ .row.load .span11{
+ text-indent: 30px;
+ -webkit-transition:all 250ms ease-in;
+ }
+
+
+ .row .span11{
+ text-indent: 0px;
+ -webkit-transition:all 250ms ease-in;
+ }
+
+
+ .alert-message-content pre{
+ resize: vertical;
+ height:200px;
+ overflow:auto;
+ }
+
+footer p{
+ color: #CCC;
+ text-align: center;
+}

0 comments on commit d887bd5

Please sign in to comment.