從零搭建 Node.js 企業級 Web 伺服器(十一):定時任務

烏柏木發表於2020-08-04

配置定時任務

定時任務也就是由時間觸發的執行過程,屬於很常見的業務邏輯。Unix 在早期版本就提供了定時任務排程模組 Cron,並在各類 Linux 系統上沿用至今。Cron 的配置檔案 crontab 具有全面卻清晰的格式,能夠解決大多數場景下的定時任務配置問題,企業級伺服器可以使用類 crontab 的格式靈活配置的各種定時任務邏輯,以下為 crontab 的格式:

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

本章將基於上一章已完成的工程 host1-tech/nodejs-server-examples - 10-log 通過 node-schedule 以類似 crontab 的方式配置定時任務,檢測可能含有網路攻擊的店鋪資訊並通過 nodemailer 將可疑店鋪資訊郵件傳送給管理員。在工程根目錄執行 node-schedule 與 nodemailer 的安裝命令:

$ yarn add node-schedule nodemailer # 本地安裝 node-schedule、nodemailer
# ...
info Direct dependencies
├─ node-schedule@1.3.2
└─ nodemailer@6.4.11
# ...

網路攻擊巡檢

現在實現對網路攻擊資訊的定時檢測與報警的邏輯。先補充服務層邏輯:

// src/services/shop.js
const { Shop } = require('../models');

class ShopService {
  async init() {}

-  async find({ id, pageIndex = 0, pageSize = 10, logging }) {
+  async find({ id, pageIndex = 0, pageSize = 10, where, logging }) {
    if (id) {
      return [await Shop.findByPk(id, { logging })];
    }

    return await Shop.findAll({
      offset: pageIndex * pageSize,
      limit: pageSize,
+      where,
      logging,
    });
  }
  // ...
}
// ...
// src/services/mail.js
const { promisify } = require('util');
const nodemailer = require('nodemailer');
const { mailerOptions } = require('../config');

class MailService {
  mailer;

  async init() {
    this.mailer = nodemailer.createTransport(mailerOptions);
    await promisify(this.mailer.verify)();
  }

  async sendMail(params) {
    return await this.mailer.sendMail({
      from: mailerOptions.auth.user,
      ...params,
    });
  }
}

let service;
module.exports = async () => {
  if (!service) {
    service = new MailService();
    await service.init();
  }
  return service;
};
// src/config/index.js
const merge = require('lodash.merge');
const logger = require('../utils/logger');
const { logging } = logger;

const config = {
  // 預設配置
  default: {
    // ...
+    mailerOptions: {
+      host: 'smtp.126.com',
+      port: 465,
+      secure: true,
+      logger: logger.child({ type: 'mail' }),
+      auth: {
+        user: process.env.MAILER_USER,
+        pass: process.env.MAILER_PASS,
+      },
+    },
  },
  // ...
};
// ...
# .env.local
GITHUB_CLIENT_ID='b8ada004c6d682426cfb'
GITHUB_CLIENT_SECRET='0b13f2ab5651f33f879a535fc2b316c6c731a041'
+
+MAILER_USER='ht_nse@126.com'
+MAILER_PASS='CAEJHSTBWNOKHRVL'

注意由於應用節點可能不止 1 個,執行巡檢時將使用分散式鎖限制執行節點數量以避免重複報警,這裡藉助資料庫來實現分散式鎖:

$ # 生成定時任務鎖的 model 檔案與 schema 遷移檔案
$ yarn sequelize model:generate --name scheduleLock --attributes name:string,counter:integer

$ # 將 src/models/schedulelock.js 命名為 src/models/scheduleLock.js
$ mv src/models/schedulelock.js src/models/scheduleLock.js

$ tree src/models # 展示 src/models 目錄內容結構
src/models
├── config
│   └── index.js
├── index.js
├── migrate
│   ├── 20200725045100-create-shop.js
│   ├── 20200727025727-create-session.js
│   └── 20200801120113-create-schedule-lock.js
├── scheduleLock.js
├── seed
│   └── 20200725050230-first-shop.js
└── shop.js

調整 src/models/scheduleLock.jssrc/models/migrate/20200801120113-create-schedule-lock.js

// src/models/scheduleLock.js
const { Model } = require('sequelize');

module.exports = (sequelize, DataTypes) => {
  class scheduleLock extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
    }
  }
  scheduleLock.init(
    {
      name: DataTypes.STRING,
      counter: DataTypes.INTEGER,
    },
    {
      sequelize,
      modelName: 'ScheduleLock',
      tableName: 'schedule_lock',
    }
  );
  return scheduleLock;
};
// src/models/migrate/20200801120113-create-schedule-lock.js
module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('schedule_lock', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER,
      },
      name: {
        type: Sequelize.STRING,
      },
      counter: {
        type: Sequelize.INTEGER,
      },
      created_at: {
        allowNull: false,
        type: Sequelize.DATE,
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE,
      },
    });
  },
  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('schedule_lock');
  },
};

然後寫入巡檢邏輯:

$ mkdir src/schedules # 新建 src/schedules 存放定時任務

$ tree src -L 1       # 展示 src 目錄內容結構
src
├── config
├── controllers
├── middlewares
├── models
├── moulds
├── schedules
├── server.js
├── services
└── utils
// src/schedules/inspectAttack.js
const { basename } = require('path');
const schedule = require('node-schedule');
const { sequelize, ScheduleLock, Sequelize } = require('../models');
const mailService = require('../services/mail');
const shopService = require('../services/shop');
const escapeHtmlInObject = require('../utils/escape-html-in-object');
const logger = require('../utils/logger');
const { Op } = Sequelize;

// 當前任務的鎖名稱
const LOCK_NAME = basename(__dirname);
// 鎖的最長佔用時間
const LOCK_TIMEOUT = 15 * 60 * 1000;
// 分散式任務併發數
const CONCURRENCY = 1;
// 報警郵件傳送物件
const MAIL_RECEIVER = 'licg9999@126.com';

class InspectAttack {
  mailService;
  shopService;

  async init() {
    this.mailService = await mailService();
    this.shopService = await shopService();

    // 每到 15 分時巡檢一次
    schedule.scheduleJob('*/15 * * * *', this.findAttackedShopInfoAndSendMail);
  }

  findAttackedShopInfoAndSendMail = async () => {
    // 上鎖
    const lockUpT = await sequelize.transaction();
    try {
      const [lock] = await ScheduleLock.findOrCreate({
        where: { name: LOCK_NAME },
        defaults: { name: LOCK_NAME, counter: 0 },
        transaction: lockUpT,
      });

      if (lock.counter >= CONCURRENCY) {
        if (Date.now() - lock.updatedAt.valueOf() > LOCK_TIMEOUT) {
          lock.counter--;
          await lock.save({ transaction: lockUpT });
        }
        await lockUpT.commit();
        return;
      }

      lock.counter++;
      await lock.save({ transaction: lockUpT });
      await lockUpT.commit();
    } catch (err) {
      logger.error(err);
      await lockUpT.rollback();
      return;
    }

    try {
      // 尋找異常資料
      const shops = await this.shopService.find({
        pageSize: 100,
        where: {
          name: { [Op.or]: [{ [Op.like]: '<%' }, { [Op.like]: '%>' }] },
        },
      });

      // 傳送報警郵件
      if (shops.length) {
        const subject = '安全警告,發現可疑店鋪資訊!';
        const html = `
  <div>以下是伺服器巡檢發現的疑似含有網路攻擊的店鋪資訊:</div>
  <pre>
  ${shops
    .map((shop) => JSON.stringify(escapeHtmlInObject(shop), null, 2))
    .join('\n')}
  </pre>`;
        await this.mailService.sendMail({ to: MAIL_RECEIVER, subject, html });
      }
    } catch {}

    // 解鎖
    const lockDownT = await sequelize.transaction();
    try {
      const lock = await ScheduleLock.findOne({
        where: { name: LOCK_NAME },
        transaction: lockDownT,
      });
      if (lock.counter > 0) {
        lock.counter--;
        await lock.save({ transaction: lockDownT });
      }
      await lockDownT.commit();
    } catch {
      await lockDownT.rollback();
    }
  };
}

module.exports = async () => {
  const s = new InspectAttack();
  await s.init();
};
// src/schedules/index.js
const inspectAttackSchedule = require('./inspectAttack');

module.exports = async function initSchedules() {
  await inspectAttackSchedule();
};
// src/server.js
const express = require('express');
const { resolve } = require('path');
const { promisify } = require('util');
const initMiddlewares = require('./middlewares');
const initControllers = require('./controllers');
+const initSchedules = require('./schedules');
const logger = require('./utils/logger');

const server = express();
const port = parseInt(process.env.PORT || '9000');
const publicDir = resolve('public');
const mouldsDir = resolve('src/moulds');

async function bootstrap() {
  server.use(await initMiddlewares());
  server.use(express.static(publicDir));
  server.use('/moulds', express.static(mouldsDir));
  server.use(await initControllers());
  server.use(errorHandler);
+  await initSchedules();
  await promisify(server.listen.bind(server, port))();
  logger.info(`> Started on port ${port}`);
}
// ...

檢視報警

在新增兩個含有網路攻擊的店鋪資訊之後,即可在分鐘數為 15 的倍數時收到一則警告郵件:

6b237b86e0f827754b34a9218855c44e3ba7dc1b.jpg

5b65bc396739366f0dd8d89b023b2097fb6045f7.jpg

本章原始碼

host1-tech/nodejs-server-examples - 11-schedule

更多閱讀

從零搭建 Node.js 企業級 Web 伺服器(零):靜態服務
從零搭建 Node.js 企業級 Web 伺服器(一):介面與分層
從零搭建 Node.js 企業級 Web 伺服器(二):校驗
從零搭建 Node.js 企業級 Web 伺服器(三):中介軟體
從零搭建 Node.js 企業級 Web 伺服器(四):異常處理
從零搭建 Node.js 企業級 Web 伺服器(五):資料庫訪問
從零搭建 Node.js 企業級 Web 伺服器(六):會話
從零搭建 Node.js 企業級 Web 伺服器(七):認證登入
從零搭建 Node.js 企業級 Web 伺服器(八):網路安全
從零搭建 Node.js 企業級 Web 伺服器(九):配置項
從零搭建 Node.js 企業級 Web 伺服器(十):日誌
從零搭建 Node.js 企業級 Web 伺服器(十一):定時任務
從零搭建 Node.js 企業級 Web 伺服器(十二):遠端呼叫

相關文章