updateData.js 865 B

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