Showing
1 changed file
with
38 additions
and
33 deletions
@@ -9,48 +9,53 @@ module.exports = () => { | @@ -9,48 +9,53 @@ module.exports = () => { | ||
9 | let zookeeperModel = new ZookeeperModel(); | 9 | let zookeeperModel = new ZookeeperModel(); |
10 | 10 | ||
11 | schedule.scheduleJob('* * * * * *', async () => { | 11 | schedule.scheduleJob('* * * * * *', async () => { |
12 | + console.log('scheduleJob') | ||
12 | const appsData = await client.getAsync('degradeSSRKeys'); | 13 | const appsData = await client.getAsync('degradeSSRKeys'); |
13 | 14 | ||
14 | const apps = JSON.parse(appsData || '[]'); | 15 | const apps = JSON.parse(appsData || '[]'); |
15 | 16 | ||
16 | _.forEach(apps, async app => { | 17 | _.forEach(apps, async app => { |
17 | - const zkDegradePath = `/wap/webapp/${app}-degrade`; | ||
18 | - const degradeKey = `${app}:degradessr`; | ||
19 | - const isDegradeKey = `${degradeKey}:isdegrade`; | ||
20 | - const [degradeJson, isDegrade] = await Promise.all([client.getAsync(degradeKey), client.getAsync(isDegradeKey)]); | ||
21 | - const zkIsDegrade = await zookeeperModel.getPath(zkDegradePath) | ||
22 | - | ||
23 | - const degrades = JSON.parse(degradeJson || '[]'); | ||
24 | - | ||
25 | - if (!isDegrade && zkIsDegrade === 'true') { // isdegrade的redis key已过期,且没有触发限制刷新key过期时间,则降级开关关闭 | ||
26 | - zookeeperModel.setPath(zkDegradePath, false); | ||
27 | - console.log('SSR降级恢复') | ||
28 | - Operation.action({ | ||
29 | - _id: 0, | ||
30 | - username: 'ssr-degrade' | ||
31 | - }, 'SSR降级恢复', '降级恢复' , {app}); | ||
32 | - } | ||
33 | - | ||
34 | - _.forEach(degrades, async item => { | ||
35 | - const key = `${degradeKey}:${item.time}`; | ||
36 | - const result = await client.getAsync(key); | ||
37 | - | ||
38 | - if (item.time * item.tick < +result) { // 大于qps限制,降级 | ||
39 | - const degradeData = { | ||
40 | - ...item, | ||
41 | - maxTick: +result | ||
42 | - }; | ||
43 | - | ||
44 | - console.log('触发降级策略') | 18 | + try { |
19 | + const zkDegradePath = `/wap/webapp/${app}-degrade`; | ||
20 | + const degradeKey = `${app}:degradessr`; | ||
21 | + const isDegradeKey = `${degradeKey}:isdegrade`; | ||
22 | + const [degradeJson, isDegrade] = await Promise.all([client.getAsync(degradeKey), client.getAsync(isDegradeKey)]); | ||
23 | + const zkIsDegrade = await zookeeperModel.getPath(zkDegradePath) | ||
24 | + | ||
25 | + const degrades = JSON.parse(degradeJson || '[]'); | ||
26 | + | ||
27 | + if (!isDegrade && zkIsDegrade === 'true') { // isdegrade的redis key已过期,且没有触发限制刷新key过期时间,则降级开关关闭 | ||
28 | + zookeeperModel.setPath(zkDegradePath, false); | ||
29 | + console.log('SSR降级恢复') | ||
45 | Operation.action({ | 30 | Operation.action({ |
46 | _id: 0, | 31 | _id: 0, |
47 | username: 'ssr-degrade' | 32 | username: 'ssr-degrade' |
48 | - }, 'SSR降级', '触发降级策略' ,`${isDegradeKey},${JSON.stringify(degradeData)}`); | ||
49 | - client.setexAsync(isDegradeKey, item.delay * 60, JSON.stringify(degradeData)); | ||
50 | - client.delAsync(key); | ||
51 | - zookeeperModel.setPath(zkDegradePath, true); | 33 | + }, 'SSR降级恢复', '降级恢复' , {app}); |
52 | } | 34 | } |
53 | - }); | 35 | + |
36 | + _.forEach(degrades, async item => { | ||
37 | + const key = `${degradeKey}:${item.time}`; | ||
38 | + const result = await client.getAsync(key); | ||
39 | + | ||
40 | + if (item.time * item.tick < +result) { // 大于qps限制,降级 | ||
41 | + const degradeData = { | ||
42 | + ...item, | ||
43 | + maxTick: +result | ||
44 | + }; | ||
45 | + | ||
46 | + console.log('触发降级策略') | ||
47 | + Operation.action({ | ||
48 | + _id: 0, | ||
49 | + username: 'ssr-degrade' | ||
50 | + }, 'SSR降级', '触发降级策略' ,`${isDegradeKey},${JSON.stringify(degradeData)}`); | ||
51 | + client.setexAsync(isDegradeKey, item.delay * 60, JSON.stringify(degradeData)); | ||
52 | + client.delAsync(key); | ||
53 | + zookeeperModel.setPath(zkDegradePath, true); | ||
54 | + } | ||
55 | + }); | ||
56 | + } catch (error) { | ||
57 | + console.log(error); | ||
58 | + } | ||
54 | }) | 59 | }) |
55 | }); | 60 | }); |
56 | } | 61 | } |
-
Please register or login to post a comment