app.js
3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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;