degrade-ssr-task.js 2.01 KB
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);
        }
      });
    })
  });
}