degrade.js
4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
'use strict';
import Router from 'koa-router';
import moment from 'moment';
import _ from 'lodash';
import process from 'process';
import {Degrade, DegradeServer, MemcachedHost} from '../../models';
import getter from '../../zookeeper/getter';
import setter from '../../zookeeper/setter';
import tester from '../../zookeeper/tester';
import Memcached from 'memcached';
const router = new Router();
// memcached operation start
const memcachedGetter = (path) => new Promise(async (resolve, reject) => {
let memcached = await getMemcached();
memcached.get(path, (err, data) => {
if (err) {
console.log(`Got error when get "${path}" value from memcached`);
resolve(false);
} else {
resolve(data === 'true');
}
process.nextTick(() => {
memcached.end();
});
});
});
const memcachedSetter = (path, value) => new Promise(async (resolve, reject) => {
let memcached = await getMemcached();
memcached.set(path, value, 2592000, (err) => {
if (err) {
resolve();
} else {
resolve(`set ${path} value to: ${value} success`);
}
process.nextTick(() => {
memcached.end();
});
});
});
// 对某些path通过memcached处理值的存储
const gotoMemcached = path => path.indexOf('/pc/cart') > -1;
const getMemcached = async () => {
let queryHosts = await MemcachedHost.findAll();
let hosts;
if (queryHosts.length === 0) {
hosts = ['memcache1.yohoops.org:12111', 'memcache2.yohoops.org:12111', 'memcache3.yohoops.org:12111'];
} else {
hosts = _.map(queryHosts, 'host');
}
return new Memcached(hosts);
}
// memcached operation end
const ctl = {
async getServer() {
let server = await DegradeServer.findAll({});
server = _.last(server);
if (server) {
return `${server.ip}:${server.port}`;
}
},
async index (ctx) {
let serverPath = await ctl.getServer();
let serverSplit = serverPath ? serverPath.split(':') : ['', ''];
await ctx.render('action/degrade', {
ip: serverSplit[0],
port: serverSplit[1]
});
},
async connect(ctx) {
let {ip, port} = ctx.query;
let server = `${ip}:${port}`;
let connected = await tester(server);
// connecting test
if (!connected) {
ctx.body = `<p class="connect-err">
<i class="fa fa-wheelchair" aria-hidden="true"></i>
Sorry, I can not connect to <span class="server-name">${server}</span>.Please check whether your ip/port is correct or the zookeeper server is running
</p>`;
return;
}
let degrades = await Degrade.findAll();
for(let i of degrades) {
// write memcached in modules of pc cart
if (gotoMemcached(i.path)) {
i.checked = await memcachedGetter(i.path);
} else {
i.checked = await getter(server, i.path);
}
}
let pc = _.filter(degrades, o => _.startsWith(o.path, '/pc'));
let wap = _.filter(degrades, o => _.startsWith(o.path, '/wap'));
await ctx.render('action/degrade_list', {
layout: false,
pc: pc,
wap: wap
});
},
async server(ctx) {
let ip = ctx.request.body.ip;
let port = ctx.request.body.port;
let serverCount = await DegradeServer.count({});
// keep one server
if (serverCount) {
let serverConfig = await DegradeServer.findAll({});
let id = _.last(serverConfig)._id; // get the latest item
await DegradeServer.update({
_id: id
}, {
$set: {
ip: ip,
port: port
}
});
} else {
await DegradeServer.insert({
ip: ip,
port: port
});
}
ctx.body = {
code: 200,
message: `${serverCount ? 'update' : 'new'} server success`
};
},
async setter(ctx) {
let {checked, id} = ctx.query;
let theDegrade = await Degrade.findById(id);
let path = theDegrade.path;
let result;
if (gotoMemcached(path)) {
result = await memcachedSetter(path, checked.toString());
} else {
let serverPath = await ctl.getServer();
result = await setter(serverPath, path, checked.toString());
}
if (result) {
ctx.body = {
code: 200,
message: 'update success'
};
} else {
ctx.body = {
code: 500,
message: 'update fail,Please retry'
}
}
}
};
router.get('/', ctl.index);
router.post('/server', ctl.server);
router.get('/connect', ctl.connect);
router.get('/setter', ctl.setter);
export default router;