degrade-ssr-task.js
2.01 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
const schedule = require('node-schedule');
const _ = require('lodash');
const ZookeeperModel = require('../web/models/zookeeperModel');
const Operation = require('../logger/operation');
const {client} = require('../../lib/redis');
module.exports = () => {
let zookeeperModel = new ZookeeperModel();
schedule.scheduleJob('* * * * * *', async () => {
const appsData = await client.getAsync('degradeSSRKeys');
const apps = JSON.parse(appsData || '[]');
_.forEach(apps, async app => {
const zkDegradePath = `/wap/webapp/${app}-degrade`;
const degradeKey = `${app}:degradessr`;
const isDegradeKey = `${degradeKey}:isdegrade`;
const [degradeJson, isDegrade] = await Promise.all([client.getAsync(degradeKey), client.getAsync(isDegradeKey)]);
const zkIsDegrade = await zookeeperModel.getPath(zkDegradePath)
const degrades = JSON.parse(degradeJson || '[]');
if (!isDegrade && zkIsDegrade === 'true') { // isdegrade的redis key已过期,且没有触发限制刷新key过期时间,则降级开关关闭
zookeeperModel.setPath(zkDegradePath, false);
console.log('SSR降级恢复')
Operation.action({
_id: 0,
username: 'ssr-degrade'
}, 'SSR降级恢复', '降级恢复' , {app});
}
_.forEach(degrades, async item => {
const key = `${degradeKey}:${item.time}`;
const result = await client.getAsync(key);
if (item.time * item.tick < +result) { // 大于qps限制,降级
const degradeData = {
...item,
maxTick: +result
};
console.log('触发降级策略')
Operation.action({
_id: 0,
username: 'ssr-degrade'
}, 'SSR降级', '触发降级策略' ,`${isDegradeKey},${JSON.stringify(degradeData)}`);
client.setexAsync(isDegradeKey, item.delay * 60, JSON.stringify(degradeData));
client.delAsync(key);
zookeeperModel.setPath(zkDegradePath, true);
}
});
})
});
}