app.js 3.86 KB
const path = require('path'); 
const Queue = require("./app/common/queue");
// module.exports = app => {
  // app.beforeStart(async function() {
  //   // 应用会等待这个函数执行完成才启动
  //   // await app.model.sync({ force: true }); // 开发环境使用
  //   await app.model.sync({});
  // });
  class AppBootHook {
    constructor(app) {
      this.app = app;
      this.helper = app.helper;
    }
    async configWillLoad() {
      console.log(`
          ◆ 项目:广联赛讯数据大屏后台
          ◆ 语言:Javascript
          ◆ 框架:Node.js + Egg.js + MySql
          ◆ 版本控制: git-github
          ◆ 代码风格:eslint-standard
          ◆ 编辑器: VSCode
          ◆ 数据库:  mysql
          ◆ 服务器端脚本: Node.js
          ◆ author: limin
      `)
    }
    async didReady() {

      const ctx = await this.app.createAnonymousContext();

      console.log('\r\n          应用启动成功!\r\n')

      console.log('          ◆ 1.加载校验规则\r\n')
      await this._init_user_validations_(ctx);

      console.log('          ◆ 2.重置时间对象\r\n')
      await this._init_user_date_times_(ctx)

      console.log('          ◆ 3.初始化激活消息队列\r\n')
      await this._init_user_activation_queue_(ctx)
      await this._init_merchant_activation_queue_(ctx)
    }

    async _init_user_validations_() {
      const directory = path.join(this.app.config.baseDir, './app/validate');
      this.app.loader.loadToApp(directory, 'validate');
    }

    async _init_user_date_times_(ctx) {
      const { service, helper } = ctx;
      const { date, constant, moment } = helper
      const yesterday = date(-1);
      const today = date(0);
      var  { init_date, user_cache_update_time_diff } = this.app.config.sys;
      const up_time = moment().subtract(user_cache_update_time_diff +1 ,'milliseconds');
      await service.cache.set(constant.DATES,{
        today,
        yesterday,
        cache_data_last_date:init_date,
        cache_data_last_date_merchant:init_date,
        cache_update_time:up_time,
        cache_update_time_merchant:up_time
      });
    }

    async _init_user_activation_queue_(ctx) {
      const { service, helper } = ctx;
      const { USER_ACTIVE_DATA_LASI_ID } = helper.constant;
      const size = this.app.config.sys.user_activation_queue_size
      const _last_data = await service.screen.user.user_active({limit:size});
      let queue = new Queue({size,data:_last_data.reverse(),cache:service.cache,fetch:service.screen.user.activation,const_id:USER_ACTIVE_DATA_LASI_ID});
      this.app.queue = queue;
      // service.cache.set(USER_ACTIVE_DATA_LASI_ID,(_last_data &&_last_data.length && _last_data[0].id) || 0 )
      //调用一次真实数据  覆盖初始化数据
      // const { service, helper } = ctx;
      // const { USER_ACTIVE_DATA_LASI_ID } = helper.constant;
      // const _last_id = await service.cache.get(USER_ACTIVE_DATA_LASI_ID) || 0;
      
      // const data = this.app.queue.peek();
      // this.app.queue.fill(async ()=>await service.screen.user.activation({ limit: queue.size(), last_id:_last_id }),data)
      // .then(last_id=>last_id && service.cache.set(USER_ACTIVE_DATA_LASI_ID,last_id))
    }

    async _init_merchant_activation_queue_(ctx) {
      const { service, helper } = ctx;
      const { MERCHANT_ACTIVE_DATA_LASI_ID } = helper.constant;
      const size = this.app.config.sys.user_activation_queue_size
      const _last_data = await service.screen.merchant.merchant_active({limit:size});
      let queue = new Queue({size,data:_last_data.reverse(),cache:service.cache,fetch:service.screen.merchant.activation,const_id:MERCHANT_ACTIVE_DATA_LASI_ID});
      this.app.merchant_queue = queue;
      // service.cache.set(MERCHANT_ACTIVE_DATA_LASI_ID,(_last_data && _last_data.length && _last_data[0].id) || 0 )
    }
  }

  module.exports = AppBootHook;