'use strict'; const Subscription = require('egg').Subscription; class UpdateData extends Subscription { // 通过 schedule 属性来设置定时任务的执行间隔等配置 static get schedule() { return { // interval: '24h', // 1 分钟间隔 cron: '0 0 5 * * ? ', // 每天凌晨6点 type: 'worker', // all 指定所有的都需要执行 worker 其中一个 // immediate: true, cronOptions: { tz: 'Asia/Shanghai', }, }; } // subscribe 是真正定时任务执行时被运行的函数 async subscribe() { await this.beforeTask(); await this.service.statisticsService.task(); await this.endTask(); } async beforeTask() { this.app.logger.info('条件任务开始'); } async endTask() { this.app.logger.info('条件任务结束'); } } module.exports = UpdateData;