ssrDegradeModel.js
2.51 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
const model = require('../../../lib/model');
const {client} = require('../../../lib/redis');
const ZookeeperModel = require('../../web/models/zookeeperModel');
const _ = require('lodash');
class SsrDegradeModel extends model {
constructor(ctx) {
super(ctx);
}
async getApps() {
const appsData = await client.getAsync('degradeSSRKeys');
const apps = JSON.parse(appsData || '[]');
return Promise.all(_.map(apps, this.getApp));
}
async getApp(app) {
const zookeeperModel = new ZookeeperModel();
const [degradeJson, isDegradeJson] = await Promise.all([
client.getAsync(`${app}:degradessr`),
client.getAsync(`${app}:degradessr:isdegrade`)]
);
const zkIsDegrade = await zookeeperModel.getPath(`/wap/webapp/${app}-degrade`);
const degrades = JSON.parse(degradeJson || '[]');
let isDegrade;
if (isDegradeJson) {
isDegrade = JSON.parse(isDegradeJson);
_.forEach(degrades, d => {
if (d.time === isDegrade.time && d.tick === isDegrade.tick) {
d.status = true;
d.maxTick = d.maxTick;
}
})
}
return {
app,
degrades,
status: zkIsDegrade === 'true'
};
}
async editApp(data) {
const zookeeperModel = new ZookeeperModel();
const appsData = await client.getAsync('degradeSSRKeys');
const apps = JSON.parse(appsData || '[]');
if (!apps.some(app => app === data.app)) {
apps.push(data.app);
client.setAsync('degradeSSRKeys', JSON.stringify(apps));
}
zookeeperModel.setPath(`/wap/webapp/${data.app}-degrade`, false);
client.delAsync(`${data.app}:degradessr:isdegrade`);
const degrades = _.map(data.degrades, d => {
return {
time: d.time,
tick: d.tick,
delay: d.delay
}
})
return await client.setAsync(`${data.app}:degradessr`, JSON.stringify(degrades));
}
async deleteApp(data) {
const zookeeperModel = new ZookeeperModel();
const appsData = await client.getAsync('degradeSSRKeys');
const apps = JSON.parse(appsData || '[]');
const saveApps = apps.filter(app => app !== data.app);
client.setAsync('degradeSSRKeys', JSON.stringify(saveApps));
client.delAsync(`${data.app}:degradessr`);
client.delAsync(`${data.app}:degradessr:isdegrade`);
zookeeperModel.setPath(`/wap/webapp/${data.app}-degrade`, false);
return true;
}
}
module.exports = SsrDegradeModel;