Commit 43247fc8 by liimin

增加定时任务 每日将总数写入缓存

1 parent 2d0a2ba7
/*
* @Author: limin
* @Date: 2019-11-21 11:20:15
* @Last Modified by: limin
* @Last Modified time: 2019-11-26 15:58:28
*/
module.exports = {
PREFIX: '_glsx_data_screen_'
};
\ No newline at end of file \ No newline at end of file
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @Author: limin * @Author: limin
* @Date: 2019-11-21 11:20:01 * @Date: 2019-11-21 11:20:01
* @Last Modified by: limin * @Last Modified by: limin
* @Last Modified time: 2019-11-25 18:09:24 * @Last Modified time: 2019-11-26 15:37:02
*/ */
"use strict"; "use strict";
......
...@@ -2,10 +2,11 @@ ...@@ -2,10 +2,11 @@
* @Author: limin * @Author: limin
* @Date: 2019-11-21 11:19:43 * @Date: 2019-11-21 11:19:43
* @Last Modified by: limin * @Last Modified by: limin
* @Last Modified time: 2019-11-25 17:27:05 * @Last Modified time: 2019-11-26 16:03:10
*/ */
const moment = require("moment"); const moment = require("moment");
const constant = require("../common/constant");
// exports.relativeTime = time => moment(new Date(time * 1000)).fromNow(); // exports.relativeTime = time => moment(new Date(time * 1000)).fromNow();
exports.date = (n, f = "YYYY-MM-DD") => exports.date = (n, f = "YYYY-MM-DD") =>
...@@ -37,3 +38,4 @@ exports.seqOption = function({ pid, n, limit, order }) { ...@@ -37,3 +38,4 @@ exports.seqOption = function({ pid, n, limit, order }) {
} }
return option; return option;
}; };
exports.constant = constant
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_summary', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
day: {
type: DataTypes.DATE,
allowNull: false,
unique: true
},
total: {
type: DataTypes.INTEGER(10),
allowNull: true
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_summary'
});
Model.associate = function() {
}
return Model;
};
const Subscription = require('egg').Subscription; const Subscription = require("egg").Subscription;
class UpdateCache extends Subscription { class UpdateCache extends Subscription {
constructor(ctx) {
super(ctx);
this.UserService = ctx.service.screen.user;
this.helper = ctx.helper;
this.CacheService = ctx.service.cache; //redis 缓存服务
}
/*
* * * * * *
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ |
│ │ │ │ │ └ 星期 (0 - 7) (0或7都是星期日)
│ │ │ │ └───── 月份 (1 - 12)
│ │ │ └────────── 日期 (1 - 31)
│ │ └─────────────── 小时 (0 - 23)
│ └──────────────────── 分钟 (0 - 59)
└───────────────────────── 秒 (0 - 59, optional)
*/
// 通过 schedule 属性来设置定时任务的执行间隔等配置 // 通过 schedule 属性来设置定时任务的执行间隔等配置
static get schedule() { static get schedule() {
return { return {
interval: '1s', // 1 分钟间隔 // interval: '1m', // 定时任务 1 分钟间隔
type: 'all', // 指定所有的 worker 都需要执行 cron: "0 30 0 * * *", // 定点任务 每天 凌晨 0点30 执行一次
immediate:true type: "all", // 指定所有的 worker 都需要执行
immediate: true //启动立即执行一次
}; };
} }
// subscribe 是真正定时任务执行时被运行的函数 // subscribe 是真正定时任务执行时被运行的函数
async subscribe() { async subscribe() {
console.log('1111111111111'); this.logger.info('===============执行日常任务-缓存每日用户总数 begin==================')
const today_summary = await this.UserService.user_summary_today();
if (today_summary.length) {
const [{ day, total }] = today_summary;
const value = JSON.stringify({ day, total });
this.logger.info('today_summary:%s',value)
await this.CacheService.set(
`user_summary_${day}`,
value,
25 * 60 * 60 //25小时有效
);
}
this.logger.info('===============执行日常任务-缓存每日用户总数 end==================')
} }
} }
module.exports = UpdateCache;
\ No newline at end of file \ No newline at end of file
module.exports = UpdateCache;
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @Author: limin * @Author: limin
* @Date: 2019-11-21 14:32:51 * @Date: 2019-11-21 14:32:51
* @Last Modified by: limin * @Last Modified by: limin
* @Last Modified time: 2019-11-21 17:07:57 * @Last Modified time: 2019-11-26 16:37:00
*/ */
"use strict"; "use strict";
...@@ -15,14 +15,16 @@ class CacheService extends Service { ...@@ -15,14 +15,16 @@ class CacheService extends Service {
this.session = ctx.session; this.session = ctx.session;
this.ResponseCode = ctx.response.ResponseCode; this.ResponseCode = ctx.response.ResponseCode;
this.ServerResponse = ctx.response.ServerResponse; this.ServerResponse = ctx.response.ServerResponse;
this.helper = ctx.helper;
this.PREFIX = ctx.helper.constant.PREFIX;
} }
async set(key, value, ex = 60 * 60) { async set(key, value, ex = 60 * 60) {
this.app.redis && (await this.app.redis.set(key, value, "EX", ex)); this.app.redis && (await this.app.redis.set(`${this.PREFIX}${key}`, value, "EX", ex));
} }
async get(key, fnGetFromDB, ex) { async get(key, fnGetFromDB, ex) {
if (this.app.redis) { if (this.app.redis) {
return await this.app.redis.get(key); return await this.app.redis.get(`${this.helper.constant.PREFIX}${key}`);
} }
if (fnGetFromDB) { if (fnGetFromDB) {
var data = await fnGetFromDB(); var data = await fnGetFromDB();
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @Author: limin * @Author: limin
* @Date: 2019-11-21 11:18:37 * @Date: 2019-11-21 11:18:37
* @Last Modified by: limin * @Last Modified by: limin
* @Last Modified time: 2019-11-25 17:28:20 * @Last Modified time: 2019-11-26 15:51:19
*/ */
const Service = require("egg").Service; const Service = require("egg").Service;
...@@ -23,6 +23,7 @@ class UserService extends Service { ...@@ -23,6 +23,7 @@ class UserService extends Service {
this.MCityMilesDaily = ctx.model.TUserCityAverageMilesDay; this.MCityMilesDaily = ctx.model.TUserCityAverageMilesDay;
this.MCityDriveDaysMonth = ctx.model.TUserCityAverageDriveMonth; this.MCityDriveDaysMonth = ctx.model.TUserCityAverageDriveMonth;
this.MCityJourney = ctx.model.TUserCityJourneyDaily; this.MCityJourney = ctx.model.TUserCityJourneyDaily;
this.MUserSummary = ctx.model.TUserSummary;
this.ResponseCode = ctx.response.ResponseCode; this.ResponseCode = ctx.response.ResponseCode;
this.ServerResponse = ctx.response.ServerResponse; this.ServerResponse = ctx.response.ServerResponse;
} }
...@@ -189,6 +190,17 @@ class UserService extends Service { ...@@ -189,6 +190,17 @@ class UserService extends Service {
const data = await this.MCityJourney.findAll(option); const data = await this.MCityJourney.findAll(option);
return data; return data;
} }
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 获取当天用户总数
*/
async user_summary_today() {
const data = await this.MUserSummary.findAll({order:[['day', 'DESC']],limit:1, raw: true});
return data;
}
} }
module.exports = UserService; module.exports = UserService;
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @Author: limin * @Author: limin
* @Date: 2019-11-16 09:26:53 * @Date: 2019-11-16 09:26:53
* @Last Modified by: limin * @Last Modified by: limin
* @Last Modified time: 2019-11-25 12:46:32 * @Last Modified time: 2019-11-26 16:01:04
*/ */
/* eslint valid-jsdoc: "off" */ /* eslint valid-jsdoc: "off" */
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!