Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Utilisation de l'API de forever (support de la dernière version)

  • Loading branch information...
commit b1252adda030923b6409826554a6e30960e21fe3 1 parent 87fb7a1
@FGRibreau authored
Showing with 86 additions and 159 deletions.
  1. +41 −81 app.coffee
  2. +43 −76 app.js
  3. +2 −2 package.json
View
122 app.coffee
@@ -2,92 +2,48 @@ express = require 'express'
async = require 'async'
fs = require 'fs'
forever = require 'forever'
+_ = require 'underscore'
#todo
class foreverUI
constructor: ->
- # ~/.forever/pids/
- #todo
- @foreverHome = process.env.HOME+'/.forever'
-
- @configJson = @_readConfSync("#{@foreverHome}/config.json")
-
- @pidsPath = @configJson.pidPath
-
-
- _readConfSync: (filename) ->
- data = fs.readFileSync(fs.realpathSync(filename), 'utf8')
- conf = {}
-
- return conf if(!data)
-
- try
- conf = JSON.parse(data)
-
- catch ignoreError
- return {}
-
- return conf
# Get process logs
- getInfo: (pid, cb) ->
+ info: (uid, cb) ->
- pid = parseInt(pid, 10)
+ forever.list("", (err, processes) ->
- proc = @process.filter((o) ->
- o.pid == pid
- )
-
- return cb {} if proc.length != 1
-
- proc = proc[0]
-
- out = ''
-
- async.map([proc.logFile, proc.outFile, proc.errFile].filter((s) -> s != undefined), (filename, cb) ->
-
- fs.readFile(filename, (err, data) ->
- d = data.toString().trim()
-
- if(!d || d == '\n')
- cb(null, [filename, 'Empty log'])
- else
- cb(null, [filename, data.toString()])
+ if(err)
+ return cb(null)
+
+ proc = _.find(processes, (o) ->
+ o.uid == uid
)
- , (err, results) ->
- cb results
- )
-
-
- getProcess: (cb) ->
-
- @process = []
+ console.log proc
- fs.readdir @pidsPath, (err, files) =>
+ async.map([proc.logFile, proc.outFile, proc.errFile].filter((s) -> s != undefined), (filename, cb) ->
- # todo
- files.forEach((filename) =>
- return false if filename.lastIndexOf('.fvr') == -1
+ fs.readFile(filename, (err, data) ->
+ d = data.toString().trim()
- _conf = @_readConfSync "#{@pidsPath}/#{filename}"
- _conf.id = _conf.pid
+ if(!d || d == '\n')
+ cb(null, [filename, 'Empty log'])
+ else
+ cb(null, [filename, data.toString()])
+ )
- @process.push _conf
+ , (err, results) ->
+ cb results
)
+ )
- cb @process
-
stop: (cb) ->
cb {}
restart: (cb) ->
cb {}
-
-
-
-
UI = new foreverUI()
@@ -119,40 +75,44 @@ app.set 'view options',
app.get('/', (req, res) ->
- res.render('index.ejs', { process: UI.process})
+ forever.list("", (err, results) ->
+ res.render('index.ejs', {process: results})
+ )
+
)
app.get('/refresh/', (req, res) ->
- UI.getProcess (process) ->
- res.send JSON.stringify(process), { 'Content-Type': 'text/javascript' }, 200
+ forever.list("", (err, results) ->
+ res.send JSON.stringify(results), { 'Content-Type': 'text/javascript' }, 200
+ )
+
)
app.get('/processes', (req, res) ->
# Refresh process list (#todo use fs.watch instead)
- UI.getProcess(() ->
- res.send JSON.stringify(UI.process), { 'Content-Type': 'text/javascript' }, 200
+ forever.list("", (err, results) ->
+ res.send JSON.stringify(results), { 'Content-Type': 'text/javascript' }, 200
)
)
-app.get('/restart/:pid', (req, res) ->
- UI.restart req.params.pid, (res) ->
+app.get('/restart/:uid', (req, res) ->
+ UI.restart req.params.uid, (res) ->
res.send JSON.stringify(res), { 'Content-Type': 'text/javascript' }, 200
)
-app.get('/stop/:pid', (req, res) ->
- UI.stop req.params.pid, (res) ->
+app.get('/stop/:uid', (req, res) ->
+ UI.stop req.params.uid, (res) ->
res.send JSON.stringify(res), { 'Content-Type': 'text/javascript' }, 200
)
-app.get('/info/:pid', (req, res) ->
- UI.getInfo req.params.pid, (infos) ->
+app.get('/info/:uid', (req, res) ->
+ UI.info req.params.uid, (infos) ->
res.send JSON.stringify(infos), { 'Content-Type': 'text/javascript' }, 200
)
-UI.getProcess(() ->
- console.log 'Listening on 127.0.0.1:3000'
-
- #todo
- app.listen 8085, "127.0.0.1"
-)
+
+#todo
+app.listen 8085, "127.0.0.1"
+
+console.log "Listening on 127.0.0.1:8085"
View
119 app.js
@@ -1,72 +1,39 @@
(function() {
- var UI, app, async, express, forever, foreverUI, fs;
- var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
+ var UI, app, async, express, forever, foreverUI, fs, _;
express = require('express');
async = require('async');
fs = require('fs');
forever = require('forever');
+ _ = require('underscore');
foreverUI = (function() {
- function foreverUI() {
- this.foreverHome = process.env.HOME + '/.forever';
- this.configJson = this._readConfSync("" + this.foreverHome + "/config.json");
- this.pidsPath = this.configJson.pidPath;
- }
- foreverUI.prototype._readConfSync = function(filename) {
- var conf, data;
- data = fs.readFileSync(fs.realpathSync(filename), 'utf8');
- conf = {};
- if (!data) {
- return conf;
- }
- try {
- conf = JSON.parse(data);
- } catch (ignoreError) {
- return {};
- }
- return conf;
- };
- foreverUI.prototype.getInfo = function(pid, cb) {
- var out, proc;
- pid = parseInt(pid, 10);
- proc = this.process.filter(function(o) {
- return o.pid === pid;
- });
- if (proc.length !== 1) {
- return cb({});
- }
- proc = proc[0];
- out = '';
- return async.map([proc.logFile, proc.outFile, proc.errFile].filter(function(s) {
- return s !== void 0;
- }), function(filename, cb) {
- return fs.readFile(filename, function(err, data) {
- var d;
- d = data.toString().trim();
- if (!d || d === '\n') {
- return cb(null, [filename, 'Empty log']);
- } else {
- return cb(null, [filename, data.toString()]);
- }
+ function foreverUI() {}
+ foreverUI.prototype.info = function(uid, cb) {
+ return forever.list("", function(err, processes) {
+ var proc;
+ if (err) {
+ return cb(null);
+ }
+ proc = _.find(processes, function(o) {
+ return o.uid === uid;
+ });
+ console.log(proc);
+ return async.map([proc.logFile, proc.outFile, proc.errFile].filter(function(s) {
+ return s !== void 0;
+ }), function(filename, cb) {
+ return fs.readFile(filename, function(err, data) {
+ var d;
+ d = data.toString().trim();
+ if (!d || d === '\n') {
+ return cb(null, [filename, 'Empty log']);
+ } else {
+ return cb(null, [filename, data.toString()]);
+ }
+ });
+ }, function(err, results) {
+ return cb(results);
});
- }, function(err, results) {
- return cb(results);
});
};
- foreverUI.prototype.getProcess = function(cb) {
- this.process = [];
- return fs.readdir(this.pidsPath, __bind(function(err, files) {
- files.forEach(__bind(function(filename) {
- var _conf;
- if (filename.lastIndexOf('.fvr') === -1) {
- return false;
- }
- _conf = this._readConfSync("" + this.pidsPath + "/" + filename);
- _conf.id = _conf.pid;
- return this.process.push(_conf);
- }, this));
- return cb(this.process);
- }, this));
- };
foreverUI.prototype.stop = function(cb) {
return cb({});
};
@@ -103,47 +70,47 @@
layout: false
});
app.get('/', function(req, res) {
- return res.render('index.ejs', {
- process: UI.process
+ return forever.list("", function(err, results) {
+ return res.render('index.ejs', {
+ process: results
+ });
});
});
app.get('/refresh/', function(req, res) {
- return UI.getProcess(function(process) {
- return res.send(JSON.stringify(process), {
+ return forever.list("", function(err, results) {
+ return res.send(JSON.stringify(results), {
'Content-Type': 'text/javascript'
}, 200);
});
});
app.get('/processes', function(req, res) {
- return UI.getProcess(function() {
- return res.send(JSON.stringify(UI.process), {
+ return forever.list("", function(err, results) {
+ return res.send(JSON.stringify(results), {
'Content-Type': 'text/javascript'
}, 200);
});
});
- app.get('/restart/:pid', function(req, res) {
- return UI.restart(req.params.pid, function(res) {
+ 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);
});
});
- app.get('/stop/:pid', function(req, res) {
- return UI.stop(req.params.pid, function(res) {
+ 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);
});
});
- app.get('/info/:pid', function(req, res) {
- return UI.getInfo(req.params.pid, function(infos) {
+ 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);
});
});
- UI.getProcess(function() {
- console.log('Listening on 127.0.0.1:3000');
- return app.listen(8085, "127.0.0.1");
- });
+ app.listen(8085, "127.0.0.1");
+ console.log("Listening on 127.0.0.1:8085");
}).call(this);
View
4 package.json
@@ -1,6 +1,6 @@
{
"name" : "forever-webui",
- "description" : "Forever Web ui",
+ "description" : "Forever Web UI",
"keywords": ["forever","web ui"],
"version": "0.1.0",
"homepage": "http://fgribreau.com",
@@ -8,7 +8,7 @@
"async":"0.1.15",
"coffee-script":"*",
"express":">= 2.5.0",
- "forever":">= 0.7.2"
+ "forever":">= 0.7.3"
},
"repository": "",
"author" : "Francois-Guillaume Ribreau <npm@fgribreau.com> (http://fgribreau.com)",
Please sign in to comment.
Something went wrong with that request. Please try again.