const TaskBase = require("../../task.base");
const settings = require("../../../../config/settings");
var excel = require('exceljs');
const system = require("../../../system");
const fs = require('fs');
class MonitorTask extends TaskBase {
  constructor() {
    super(TaskBase.getServiceName(MonitorTask));
  }
  /**
   * 每隔10分钟检查一次
   * @param {*} taskName 
   */
  formatedTaskName (taskName) {
    console.log("formatedTaskName", "ddddddddddddddddddd")
    return taskName + "|i|10"
  }
  isFail (taskName, lastAccess) {
    let n = Date.now()
    let postMinute = (n - lastAccess-800) / 1000 / 60
    let tnameparams = taskName.split("|")
    if (tnameparams.length == 3) {
      let tname = tnameparams[0].trim()
      let mtype = tnameparams[1].trim()
      let pvalue = Number(tnameparams[2].trim())
      if (mtype == "i") {//每几分钟执行一次
        console.log(taskName, postMinute, "流逝的时间")
        //如果流失的时间大于循环调度时间,则报异常到丁丁
        if (postMinute > pvalue) {
          this.sendDing(tname + "可能失联啦...请检查")
        } else {
          if (tname == this.serviceName) {
            this.sendDing(tname + "监控精灵依然在线，请放心....")
          }
        }
      }
      if (mtype == "a") {//
        let onedaym = 24 * 60
        if (postMinute > onedaym) {
          this.sendDing(tname + "可能失联啦...请检查")
        }
      }

    } else {
      this.sendDing(taskName + ":任务名不符合监控格式")
      this.redisClient.hdel("taskuptime", taskName)
    }

  }
  async subBeforeTask (params) {
    console.log("前置操作......", this.serviceName);
  }
  async subDoTask (params) {
    let x = await this.redisClient.hgetall("taskuptime")
    if (x) {
      for (let entry in x) {
        console.log(entry, "taskName.....................")
        this.isFail(entry, x[entry])
      }
    } else {
      console.log("没有可以监控的任务.....");
    }
    console.log("MonitorTask.....");
  }
}
module.exports = MonitorTask;