12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- 'use strict';
- const Subscription = require('egg').Subscription;
- // 2023/8/25 每日统计登录人数
- class dayUsers extends Subscription {
- /**
- * cron
- * * * * * * *
- * ┬ ┬ ┬ ┬ ┬ ┬
- * │ │ │ │ │ |
- * │ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun)
- * │ │ │ │ └───── month (1 - 12)
- * │ │ │ └────────── day of month (1 - 31)
- * │ │ └─────────────── hour (0 - 23)
- * │ └──────────────────── minute (0 - 59)
- * └───────────────────────── second (0 - 59, optional)
- * @returns {{cron: string, type: string}}
- */
- // 通过 schedule 属性来设置定时任务的执行间隔等配置
- static get schedule() {
- return {
- // interval: '1m', // 1 分钟间隔
- // interval: '10s', // 10s
- // cron: '0 0 */3 * * *', // 每三小时准点执行一次
- cron: '0 58 23 * * *', // 每天23:58执行一次
- type: 'all', // 指定所有的 worker 都需要执行
- };
- }
- // subscribe 是真正定时任务执行时被运行的函数
- async subscribe() {
- const that = this;
- const seq = that.app.Sequelize;
- const tmpTime = that.app.szjcomo.time();
- const transaction = await that.app.model.transaction();
- try {
- const users = await that.app.model.Users.findAll({
- where: { update_ttime: { [seq.Op.between]: [ `${that.app.szjcomo.date('Y-m-d', tmpTime)} 00:00:00`, `${that.app.szjcomo.date('Y-m-d', tmpTime)} 23:59:59` ] } },
- order: [[ 'update_ttime', 'asc' ]],
- attributes: [ 'user_id', 'nickname', 'headimgurl', 'login_time', 'update_ttime', 'is_employee' ],
- });
- const result = JSON.parse(JSON.stringify(users));
- // 2023/8/27 批量创建用户登录记录
- const datas = await result.map(item => {
- return Object.assign(item, {
- create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
- });
- });
- await that.app.model.UserLoginLogs.bulkCreate(datas, { transaction });
- transaction.commit();
- } catch (e) {
- console.log(e);
- if (transaction) {
- transaction.rollback();
- }
- }
- }
- }
- module.exports = dayUsers;
|