Skip to content
This repository was archived by the owner on Dec 30, 2018. It is now read-only.

Commit 3672de7

Browse files
committed
v0.7.6 fix install and rebuild redis cache
Signed-off-by: zensh <admin@zensh.com>
1 parent b48b30f commit 3672de7

File tree

9 files changed

+150
-149
lines changed

9 files changed

+150
-149
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ api目录下的install.js是jsGen运行初始化文件,设置管理员初始
4141
//此命令依赖python和vs2012,请参考 https://github.com/TooTallNate/node-gyp/wiki/Visual-Studio-2010-Setup
4242
npm install //npm安装依赖模块,请确保依赖模块全部安装好。
4343
//windows下请运行 npm install --msvs_version=2012
44-
node app.js install //第一次启动jsGen,'install'参数用于初始化MongoDB数据库
44+
node app.js install //启动jsGen之前,初始化MongoDB数据库
45+
node app.js [recache] //正式启动,可选参数 `recache`,启动时重建redis缓存
4546

4647
npm start //正常启动,或 `node app.js`
4748

app.js

Lines changed: 93 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ serverDm.run(function () {
2525
jsGen.module.then = require('thenjs');
2626
jsGen.module.marked = require('marked');
2727
jsGen.module.rrestjs = require('rrestjs');
28-
jsGen.module.mongoskin = require('mongoskin');
2928
jsGen.module.nodemailer = require('nodemailer');
3029
jsGen.serverlog = jsGen.module.rrestjs.restlog;
3130
jsGen.conf = jsGen.module.rrestjs.config;
@@ -59,24 +58,55 @@ serverDm.run(function () {
5958
resJson = jsGen.lib.tools.resJson,
6059
TimeLimitCache = jsGen.lib.redis.TimeLimitCache;
6160

62-
redis.connect().then(function (defer) {
63-
redis.initConfig(jsGen.lib.json.GlobalConfig, defer); // 初始化config缓存
61+
function exit() {
62+
redis.close();
63+
jsGen.dao.db.close();
64+
jsGen.serverlog.error(error);
65+
return process.exit(1);
66+
}
67+
68+
// 带'install'参数启动则初始化MongoDB,完成后退出
69+
if (process.argv.indexOf('install') > 0) {
70+
require('./lib/install.js')().then(function () {
71+
console.log('jsGen installed!');
72+
return exit();
73+
}).fail(jsGen.thenErrLog);
74+
return;
75+
}
76+
77+
then.parallel([function (defer) {
78+
jsGen.dao.index.getGlobalConfig(defer);
79+
}, function (defer) {
80+
redis.connect().all(defer);
81+
}]).then(function (defer, result) {
82+
// 初始化config缓存
83+
redis.initConfig(result[0], defer);
84+
}, function (defer, error) {
85+
// redis 或者 mongodb 链接错误则退出
86+
jsGen.serverlog.error(error);
87+
return exit();
6488
}).then(function (defer, config) {
6589
jsGen.config = config;
66-
if (process.argv.indexOf('install') > 0) { // 带'install'参数启动则初始化MongoDB
67-
require('./api/install.js')().all(defer);
68-
} else { // Redis config缓存未赋值,则从MongoDB取值
69-
jsGen.dao.index.getGlobalConfig(defer);
70-
}
71-
}).then(function (defer, config) {
72-
each(jsGen.config, function (value, key, list) {
73-
if (key in config) {
74-
list[key] = config[key]; // 写入config缓存
90+
redis.userCache.index.total(function (error, users) {
91+
if (!users || process.argv.indexOf('recache') > 0) {
92+
// user缓存为空,则判断redis缓存为空,需要初始化
93+
// 启动时指定了recache
94+
var recache = require('./lib/recache.js');
95+
recache().then(function (defer2, result) {
96+
console.log('Redis cache rebuild success:', result);
97+
defer();
98+
}, function (defer2, error) {
99+
// redis 缓存重建失败则退出
100+
jsGen.serverlog.error(error);
101+
return exit();
102+
});
103+
} else {
104+
defer();
75105
}
76-
});
77-
defer(null, jsGen.config);
78-
}).then(function (defer, config) {
79-
var api = ['index', 'user', 'article', 'tag', 'collection', 'message', 'rebuild'];
106+
}); // 读取user缓存
107+
}).then(function (defer) {
108+
var api = ['index', 'user', 'article', 'tag', 'collection', 'message'],
109+
config = jsGen.config;
80110

81111
jsGen.cache = {};
82112
jsGen.cache.tag = new CacheLRU(config.tagCache);
@@ -102,78 +132,63 @@ serverDm.run(function () {
102132
data.nodejs = process.versions.node;
103133
data.rrestjs = _restConfig._version;
104134
jsGen.config.info = data;
105-
redis.userCache.index.total(defer); // 读取user缓存
106-
}).then(function (defer, users) {
107-
var rebuild = jsGen.api.rebuild;
108-
if (!users) { // user缓存为空,则判断redis缓存为空,需要初始化
109-
// 初始化redis缓存
110-
then(function (defer2) {
111-
rebuild.user().all(defer2);
112-
}).then(function (defer2) {
113-
rebuild.tag().all(defer2);
114-
}).then(function (defer2) {
115-
rebuild.article().all(defer);
116-
}).fail(defer);
117-
} else {
118-
defer();
119-
}
120-
}).then(function (defer) {
121-
http.createServer(function (req, res) {
122-
var dm = domain.create();
123135

124-
function errHandler(err, res, dm) {
125-
delete err.domain;
136+
function errHandler(err, res, dm) {
137+
delete err.domain;
126138

127-
try {
128-
res.on('finish', function () {
129-
//jsGen.dao.db.close();
130-
process.nextTick(function () {
131-
dm.dispose();
132-
});
139+
try {
140+
res.on('finish', function () {
141+
//jsGen.dao.db.close();
142+
process.nextTick(function () {
143+
dm.dispose();
133144
});
134-
if (err.hasOwnProperty('name')) {
135-
res.sendjson(resJson(err));
136-
} else {
137-
jsGen.serverlog.error(err);
138-
res.sendjson(resJson(jsGen.Err(jsGen.lib.msg.MAIN.requestDataErr)));
139-
}
140-
} catch (error) {
141-
delete error.domain;
142-
jsGen.serverlog.error(error);
143-
dm.dispose();
145+
});
146+
if (err.hasOwnProperty('name')) {
147+
res.sendjson(resJson(err));
148+
} else {
149+
jsGen.serverlog.error(err);
150+
res.sendjson(resJson(jsGen.Err(jsGen.lib.msg.MAIN.requestDataErr)));
144151
}
152+
} catch (error) {
153+
delete error.domain;
154+
jsGen.serverlog.error(error);
155+
dm.dispose();
145156
}
157+
}
146158

147-
function router(req, res) {
148-
if (req.path[0] === 'api' && jsGen.api[req.path[1]]) {
149-
jsGen.api[req.path[1]][req.method.toUpperCase()](req, res); // 处理api请求
150-
} else if (req.path[0].toLowerCase() === 'sitemap.xml') {
151-
jsGen.api.article.sitemap(req, res); // 响应搜索引擎sitemap,动态生成
152-
} else if (req.path[0].slice(-3).toLowerCase() === 'txt') {
153-
// 直接响应static目录的txt文件,如robots.txt
159+
function router(req, res) {
160+
if (req.path[0] === 'api' && jsGen.api[req.path[1]]) {
161+
jsGen.api[req.path[1]][req.method.toUpperCase()](req, res); // 处理api请求
162+
} else if (req.path[0].toLowerCase() === 'sitemap.xml') {
163+
jsGen.api.article.sitemap(req, res); // 响应搜索引擎sitemap,动态生成
164+
} else if (req.path[0].slice(-3).toLowerCase() === 'txt') {
165+
// 直接响应static目录的txt文件,如robots.txt
166+
then(function (defer) {
167+
fs.readFile(processPath + jsGen.conf.staticFolder + req.path[0], 'utf8', defer);
168+
}).then(function (defer, txt) {
169+
res.setHeader('Content-Type', 'text/plain');
170+
res.send(txt);
171+
}).fail(res.throwError);
172+
} else if (jsGen.robotReg.test(req.useragent)) {
173+
jsGen.api.article.robot(req, res); // 处理搜索引擎请求
174+
} else {
175+
jsGen.config.visitors = 1; // 访问次数+1
176+
res.setHeader('Content-Type', 'text/html');
177+
if (jsGen.cache.indexTpl) {
178+
res.send(jsGen.cache.indexTpl); // 响应首页index.html
179+
} else {
154180
then(function (defer) {
155-
fs.readFile(processPath + jsGen.conf.staticFolder + req.path[0], 'utf8', defer);
156-
}).then(function (defer, txt) {
157-
res.setHeader('Content-Type', 'text/plain');
158-
res.send(txt);
181+
fs.readFile(processPath + jsGen.conf.staticFolder + '/index.html', 'utf8', defer);
182+
}).then(function (defer, tpl) {
183+
jsGen.cache.indexTpl = tpl;
184+
res.send(jsGen.cache.indexTpl);
159185
}).fail(res.throwError);
160-
} else if (jsGen.robotReg.test(req.useragent)) {
161-
jsGen.api.article.robot(req, res); // 处理搜索引擎请求
162-
} else {
163-
jsGen.config.visitors = 1; // 访问次数+1
164-
res.setHeader('Content-Type', 'text/html');
165-
if (jsGen.cache.indexTpl) {
166-
res.send(jsGen.cache.indexTpl); // 响应首页index.html
167-
} else {
168-
then(function (defer) {
169-
fs.readFile(processPath + jsGen.conf.staticFolder + '/index.html', 'utf8', defer);
170-
}).then(function (defer, tpl) {
171-
jsGen.cache.indexTpl = tpl;
172-
res.send(jsGen.cache.indexTpl);
173-
}).fail(res.throwError);
174-
}
175186
}
176187
}
188+
}
189+
190+
http.createServer(function (req, res) {
191+
var dm = domain.create();
177192

178193
res.throwError = function (defer, err) { // 处理then.js捕捉的错误
179194
if (!util.isError(err)) {

config/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ module.exports = {
6969
tempCacheTime: 0,//模版缓存时间
7070
tempCacheFolder: '/tmp/tpl', //模版缓存 存放目录
7171
//mongodb 配置
72-
isMongodb: true, //是否开启mongodb支持,注意:如果使用数据库存储session,这里必须开启
72+
isMongodb: false, //是否开启mongodb支持,注意:如果使用数据库存储session,这里必须开启
7373
MongodbIp: '127.0.0.1', //mongodb地址
7474
MongodbRC: false,//如果是false表示不使用mongodb的副本集,否则为字符串,表示副本集的名称
7575
MongodbRChost: [],//表示mongodb副本集的ip:port数组。

config/config_dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ module.exports = {
6969
tempCacheTime: 0,//模版缓存时间
7070
tempCacheFolder: '/tmp/tpl', //模版缓存 存放目录
7171
//mongodb 配置
72-
isMongodb: true, //是否开启mongodb支持,注意:如果使用数据库存储session,这里必须开启
72+
isMongodb: false, //是否开启mongodb支持,注意:如果使用数据库存储session,这里必须开启
7373
MongodbIp: '127.0.0.1', //mongodb地址
7474
MongodbRC: false,//如果是false表示不使用mongodb的副本集,否则为字符串,表示副本集的名称
7575
MongodbRChost: [],//表示mongodb副本集的ip:port数组。

dao/mongoDao.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33

44
var mongoIp = jsGen.conf.MongodbIp || '127.0.0.1',
55
mongoPort = jsGen.conf.MongodbPort || 27017,
6-
mongoDbName = jsGen.conf.MongodbDefaultDbName || 'jsGen';
6+
mongoDbName = jsGen.conf.MongodbDefaultDbName || 'jsGen',
7+
mongoskin = require('mongoskin');
78

89
module.exports = {
9-
db: jsGen.module.mongoskin.db(mongoIp + ':' + mongoPort + '/?auto_reconnect=true', {
10+
db: mongoskin.db(mongoIp + ':' + mongoPort + '/?auto_reconnect=true', {
11+
w: 0,
12+
native_parser: true,
1013
database: mongoDbName
1114
})
1215
};

api/install.js renamed to lib/install.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
/*global require, module, Buffer, jsGen*/
33

44
var then = jsGen.module.then,
5-
errorHandler = jsGen.lib.tools.errorHandler;
5+
dao = jsGen.dao,
6+
db = dao.db;
67

78
module.exports = function () {
8-
var globalConfig;
99
return then(function (defer) {
10-
jsGen.dao.db.createCollection('global', {
10+
db.createCollection('global', {
1111
w: 1
1212
}, defer);
1313
}).then(function (defer, collection) {
@@ -17,10 +17,9 @@ module.exports = function () {
1717
background: true
1818
}, defer);
1919
}).then(function (defer) {
20-
jsGen.dao.index.initGlobalConfig(defer);
20+
dao.index.initGlobalConfig(defer);
2121
}).then(function (defer, config) {
22-
globalConfig = config;
23-
jsGen.dao.db.createCollection("articles", {
22+
db.createCollection("articles", {
2423
w: 1
2524
}, defer);
2625
}).then(function (defer, collection) {
@@ -30,12 +29,12 @@ module.exports = function () {
3029
background: true
3130
}, defer);
3231
}).then(function (defer) {
33-
jsGen.dao.db.command({
32+
db.command({
3433
collMod: "articles",
3534
usePowerOf2Sizes: true
3635
}, defer);
3736
}).then(function (defer) {
38-
jsGen.dao.db.createCollection("collections", {
37+
db.createCollection("collections", {
3938
w: 1
4039
}, defer);
4140
}).then(function (defer, collection) {
@@ -45,12 +44,12 @@ module.exports = function () {
4544
background: true
4645
}, defer);
4746
}).then(function (defer) {
48-
jsGen.dao.db.command({
47+
db.command({
4948
collMod: "collections",
5049
usePowerOf2Sizes: true
5150
}, defer);
5251
}).then(function (defer) {
53-
jsGen.dao.db.createCollection("messages", {
52+
db.createCollection("messages", {
5453
w: 1
5554
}, defer);
5655
}).then(function (defer, collection) {
@@ -60,12 +59,12 @@ module.exports = function () {
6059
background: true
6160
}, defer);
6261
}).then(function (defer) {
63-
jsGen.dao.db.command({
62+
db.command({
6463
collMod: "messages",
6564
usePowerOf2Sizes: true
6665
}, defer);
6766
}).then(function (defer) {
68-
jsGen.dao.db.createCollection("tags", {
67+
db.createCollection("tags", {
6968
w: 1
7069
}, defer);
7170
}).then(function (defer, collection) {
@@ -75,12 +74,12 @@ module.exports = function () {
7574
background: true
7675
}, defer);
7776
}).then(function (defer) {
78-
jsGen.dao.db.command({
77+
db.command({
7978
collMod: "tags",
8079
usePowerOf2Sizes: true
8180
}, defer);
8281
}).then(function (defer) {
83-
jsGen.dao.db.createCollection("users", {
82+
db.createCollection("users", {
8483
w: 1
8584
}, defer);
8685
}).then(function (defer, collection) {
@@ -90,21 +89,19 @@ module.exports = function () {
9089
background: true
9190
}, defer);
9291
}).then(function (defer) {
93-
jsGen.dao.db.command({
92+
db.command({
9493
collMod: "users",
9594
usePowerOf2Sizes: true
9695
}, defer);
9796
}).then(function (defer) {
98-
jsGen.dao.user.setNewUser({
99-
_id: jsGen.dao.user.convertID('Uadmin'), // 超级管理员的用户Uid,请勿修改
97+
dao.user.setNewUser({
98+
_id: dao.user.convertID('Uadmin'), // 超级管理员的用户Uid,请勿修改
10099
name: 'admin', // 超级管理员的用户名,请勿修改
101100
email: 'admin@jsgen.org', // 超级管理员的邮箱,请自行修改
102101
passwd: jsGen.lib.tools.SHA256('admin@jsgen.org'), // 超级管理员的初始密码,请自行修改
103102
role: 5, // 超级管理员最高权限,请勿修改
104103
avatar: jsGen.lib.tools.gravatar('admin@jsgen.org'), // 超级管理员的gravatar头像,请自行修改
105104
desc: '梦造互联网 By ZENSH' // 超级管理员的个人简介,请自行修改
106105
}, defer);
107-
}).then(function (defer) {
108-
defer(null, globalConfig);
109-
}).fail(errorHandler);
106+
});
110107
};

0 commit comments

Comments
 (0)