'use strict'; const C = require('../config'); const F = require('../common/function'); const path = require('path'); const _ = require('underscore'); _.str = require('underscore.string'); _.v = require('validator'); var co = require('co'); var ready = require('ready'); module.exports = function (app, commonManager) { let modelMap = app.model_mgr.model_map; let managerMap = commonManager.mgr_map; var model_map = app.model_mgr.model_map; var mgr_map = commonManager.mgr_map; var that = this; var dbtimer_obj = this; this.revertLastRunningJob = function* () { try { yield managerMap.redis.setnx("dbtimer_lock", 60*60); let running_job_list = yield modelMap.db_timer.query(null,{ fields: 'id,next_exe_timestamp', where: 'status = ?', values: [modelMap.db_timer.status.excuting], }); if (!F.isNull(running_job_list)) { yield modelMap.db_timer.update(null,{ where: 'status = ?', values: [modelMap.db_timer.status.excuting], update_values: { status: modelMap.db_timer.status.noexcute, } }); for (let i = 0; i < running_job_list.length; i++) { let job = running_job_list[i]; yield managerMap.redis.addJobToRedis(job.next_exe_timestamp,job.id); } } yield managerMap.redis.del("dbtimer_lock"); F.addDebugLogs(["revertLastRunningJob suc"]); } catch (e) { F.addErrLogs(["revertLastRunningJob err:",e.stack]); yield managerMap.redis.del("dbtimer_lock"); } }; /** * 添加数据库长存定时器 * @param timeout 多久后执行(毫秒) * @param key 要执行的函数映射名 * @param data 执行时要用到的数据 * @returns {{result: DBTimer id}} */ this.addDBTimer = function* (timeout,key,data={},dbConnection) { var inner_key = "dbTimer."+key; let fn = eval("managerMap."+inner_key); if (typeof(fn) != "function") F.throwErrCode(10040); let data_str = JSON.stringify(data); let cur_time = new Date(); let next_exe_timestamp = cur_time.getTime() + timeout; let next_exe_time = F.getTimeByDelta(timeout/1000); let res = yield modelMap.db_timer.insert(dbConnection,{ '`key`':inner_key, '`data`':data_str, 'next_exe_timestamp':next_exe_timestamp, 'next_exe_time':next_exe_time }); let jobid = F.isNull(dbConnection) ? res.insertId : res[0].insertId; yield managerMap.redis.addJobToRedis(next_exe_timestamp,jobid); return jobid; }; // db 定时器固定写法 jobdata为 addDBTimer得data参数 //this.startBookPrivateNotice = function* (job_data) { // console.log("a######################333",job_data); // job_data.c = job_data.c+1; // if (job_data.c < 3) { // let jobid = yield managerMap.dbTimer.addDBTimer(5*1000,"startBookPrivateNotice",job_data); // console.log("jobid:",jobid); // } //} //判断是否有新socket过来 如果没有要退出房间 下麦 this.socketCleaner = function* (job_data) { let uid = job_data.uid; let socketId = job_data.socketId; let room_list = job_data.room_list; F.addDebugLogs(["exe socketCleaner:",uid,socketId,room_list]); for (let i = 0; i < room_list.length; i++) { let room_id = room_list[i]; let socket_list = yield mgr_map.redis.getSocketListByUidAndRoomid(uid,room_id); // 查找当前用户在这个房间有没新socket F.addDebugLogs(["socketCleaner socket_list:",socket_list]); if (F.isNull(socket_list)) yield mgr_map.room.leaveRoom(uid,socketId,room_id); // 没有新socket 执行退出房间函数 } } /** * 删除三天前的db_timer_bak */ this.dealDbTimeerBakData = function* () { // 查询30天前的时间戳 let deal_time = F.getTimeByDelta(-259200); yield modelMap.db_timer_bak.delete(null, { where: 'next_exe_time <= ?', values: [deal_time] }); return true; }; };