dbTimer.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 'use strict';
  2. const C = require('../config');
  3. const F = require('../common/function');
  4. const path = require('path');
  5. const _ = require('underscore');
  6. _.str = require('underscore.string');
  7. _.v = require('validator');
  8. var co = require('co');
  9. var ready = require('ready');
  10. module.exports = function (app, commonManager) {
  11. let modelMap = app.model_mgr.model_map;
  12. let managerMap = commonManager.mgr_map;
  13. var model_map = app.model_mgr.model_map;
  14. var mgr_map = commonManager.mgr_map;
  15. var that = this;
  16. var dbtimer_obj = this;
  17. this.revertLastRunningJob = function* () {
  18. try {
  19. yield managerMap.redis.setnx("dbtimer_lock", 60*60);
  20. let running_job_list = yield modelMap.db_timer.query(null,{
  21. fields: 'id,next_exe_timestamp',
  22. where: 'status = ?',
  23. values: [modelMap.db_timer.status.excuting],
  24. });
  25. if (!F.isNull(running_job_list)) {
  26. yield modelMap.db_timer.update(null,{
  27. where: 'status = ?',
  28. values: [modelMap.db_timer.status.excuting],
  29. update_values: {
  30. status: modelMap.db_timer.status.noexcute,
  31. }
  32. });
  33. for (let i = 0; i < running_job_list.length; i++) {
  34. let job = running_job_list[i];
  35. yield managerMap.redis.addJobToRedis(job.next_exe_timestamp,job.id);
  36. }
  37. }
  38. yield managerMap.redis.del("dbtimer_lock");
  39. F.addDebugLogs(["revertLastRunningJob suc"]);
  40. } catch (e) {
  41. F.addErrLogs(["revertLastRunningJob err:",e.stack]);
  42. yield managerMap.redis.del("dbtimer_lock");
  43. }
  44. };
  45. /**
  46. * 添加数据库长存定时器
  47. * @param timeout 多久后执行(毫秒)
  48. * @param key 要执行的函数映射名
  49. * @param data 执行时要用到的数据
  50. * @returns {{result: DBTimer id}}
  51. */
  52. this.addDBTimer = function* (timeout,key,data={},dbConnection) {
  53. var inner_key = "dbTimer."+key;
  54. let fn = eval("managerMap."+inner_key);
  55. if (typeof(fn) != "function") F.throwErrCode(10040);
  56. let data_str = JSON.stringify(data);
  57. let cur_time = new Date();
  58. let next_exe_timestamp = cur_time.getTime() + timeout;
  59. let next_exe_time = F.getTimeByDelta(timeout/1000);
  60. let res = yield modelMap.db_timer.insert(dbConnection,{
  61. '`key`':inner_key,
  62. '`data`':data_str,
  63. 'next_exe_timestamp':next_exe_timestamp,
  64. 'next_exe_time':next_exe_time
  65. });
  66. let jobid = F.isNull(dbConnection) ? res.insertId : res[0].insertId;
  67. yield managerMap.redis.addJobToRedis(next_exe_timestamp,jobid);
  68. return jobid;
  69. };
  70. // db 定时器固定写法 jobdata为 addDBTimer得data参数
  71. //this.startBookPrivateNotice = function* (job_data) {
  72. // console.log("a######################333",job_data);
  73. // job_data.c = job_data.c+1;
  74. // if (job_data.c < 3) {
  75. // let jobid = yield managerMap.dbTimer.addDBTimer(5*1000,"startBookPrivateNotice",job_data);
  76. // console.log("jobid:",jobid);
  77. // }
  78. //}
  79. //判断是否有新socket过来 如果没有要退出房间 下麦
  80. this.socketCleaner = function* (job_data) {
  81. let uid = job_data.uid;
  82. let socketId = job_data.socketId;
  83. let room_list = job_data.room_list;
  84. F.addDebugLogs(["exe socketCleaner:",uid,socketId,room_list]);
  85. for (let i = 0; i < room_list.length; i++) {
  86. let room_id = room_list[i];
  87. let socket_list = yield mgr_map.redis.getSocketListByUidAndRoomid(uid,room_id); // 查找当前用户在这个房间有没新socket
  88. F.addDebugLogs(["socketCleaner socket_list:",socket_list]);
  89. if (F.isNull(socket_list)) yield mgr_map.room.leaveRoom(uid,socketId,room_id); // 没有新socket 执行退出房间函数
  90. }
  91. }
  92. /**
  93. * 删除三天前的db_timer_bak
  94. */
  95. this.dealDbTimeerBakData = function* () {
  96. // 查询30天前的时间戳
  97. let deal_time = F.getTimeByDelta(-259200);
  98. yield modelMap.db_timer_bak.delete(null, {
  99. where: 'next_exe_time <= ?',
  100. values: [deal_time]
  101. });
  102. return true;
  103. };
  104. };