timer.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. 'use strict';
  2. var redis = require('../libs/redis');
  3. var redisClient = redis.redisClient;
  4. var redisCo = redis.redisCo;
  5. var C = require('../config');
  6. var F = require('../common/function');
  7. var co = require('co');
  8. var _ = require('underscore');
  9. _.str = require('underscore.string');
  10. _.v = require('validator');
  11. var request = require('koa-request');
  12. // 清楚之前的定时器
  13. var clear_timer = function* (clear_prefix,timer_prefix) {
  14. clear_prefix = _.str.sprintf(C.redisPre.timer_id_prefix, clear_prefix);
  15. timer_prefix = _.str.sprintf(C.redisPre.timer_id_prefix, timer_prefix);
  16. console.log("timerid his:",clear_prefix);
  17. var timerid_his = yield redisCo.keys(clear_prefix);
  18. if (F.isNull(timerid_his)) return;
  19. console.log("clear timer his:",timerid_his);
  20. for (var i = 0; i < timerid_his.length; i++) {
  21. var timerid = timerid_his[i];
  22. if (timerid.indexOf(timer_prefix) == -1) {
  23. yield redisCo.del(timerid);
  24. console.log("del timerid:",timerid);
  25. }
  26. }
  27. };
  28. function timerMgr(app, common_mgr) {
  29. var model_map = app.model_mgr.model_map;
  30. var mgr_map = common_mgr.mgr_map;
  31. var that = this;
  32. this.app = app;
  33. this.timeout_id_map = {};
  34. this.interval_id_map = {};
  35. this.start_time = new Date().getTime();
  36. this.timer_prefix = _.str.sprintf("timer#%s:%s#%s_", C.inner_host, app.port, that.start_time);
  37. this.clear_prefix = _.str.sprintf("timer#%s:%s#*", C.inner_host, app.port);
  38. co(clear_timer(this.clear_prefix,this.timer_prefix));
  39. this.recordTimerid = function* (key, timerid) {
  40. }
  41. this.getTimerid = function* (key) {
  42. }
  43. this.innerRunTimeout = function* (fn,timeout,redis_timer_id,delete_redis = true) {
  44. that.timeout_id_map[redis_timer_id] = setTimeout(function() {
  45. co(function*(){
  46. var get_timerid_res = yield mgr_map.redis.getTimerId(redis_timer_id);
  47. if (F.isNull(get_timerid_res)) { // 说明定时器被删除
  48. var real_timerid = that.timeout_id_map[redis_timer_id];
  49. if (F.isNull(real_timerid)) {
  50. F.log("err","redis timeris is null,but can not find real timerid to clear");
  51. return;
  52. }
  53. clearTimeout(real_timerid);
  54. delete that.timeout_id_map[redis_timer_id];
  55. return;
  56. }
  57. try {
  58. yield fn(); // 执行回调
  59. } catch(e) {
  60. console.log(e);
  61. }
  62. try {
  63. if(delete_redis == true) {
  64. yield mgr_map.redis.delTimerId(redis_timer_id);
  65. }
  66. var real_timerid = that.timeout_id_map[redis_timer_id];
  67. if (F.isNull(real_timerid)) {
  68. F.log("err","redis timeris is null,but can not find real timerid to clear when exe close");
  69. }
  70. clearTimeout(real_timerid);
  71. delete that.timeout_id_map[redis_timer_id];
  72. } catch(e) {
  73. console.log(e);
  74. }
  75. }());
  76. },timeout);
  77. return redis_timer_id;
  78. };
  79. this.innerRunInterval = function* (fn,timeout,redis_timer_id) {
  80. try {
  81. yield fn(); // 立马执行
  82. } catch(e) {
  83. console.log(e);
  84. }
  85. that.interval_id_map[redis_timer_id] = setInterval(function() {
  86. co(function*(){
  87. var get_timerid_res = yield mgr_map.redis.getTimerId(redis_timer_id);
  88. console.log("########:"+redis_timer_id);
  89. if (F.isNull(get_timerid_res)) { // 说明定时器被删除
  90. mgr_map.logs.roomManInLogs('定时器被删除', {'timer_id':redis_timer_id});
  91. var real_timerid = that.interval_id_map[redis_timer_id];
  92. if (F.isNull(real_timerid)) {
  93. F.log("err","redis timeris is null,but can not find real timerid to clear");
  94. return;
  95. }
  96. clearInterval(real_timerid);
  97. delete that.interval_id_map[redis_timer_id];
  98. return;
  99. }
  100. try {
  101. yield fn(); // 执行回调
  102. } catch(e) {
  103. console.log(e);
  104. }
  105. }());
  106. },timeout);
  107. return redis_timer_id;
  108. };
  109. // 判断定时器是否存在 interval timeout 定时器都适用
  110. this.isTimerExist = function* (timerid) {
  111. var get_timerid_res = yield mgr_map.redis.getTimerId(timerid);
  112. if (F.isNull(get_timerid_res)) { // 说明定时器被删除
  113. return 0;
  114. }
  115. return 1;
  116. };
  117. // fn 必须是异步函数
  118. this.innerSetTimeout = function* (fn,timeout) {
  119. if (timeout > 2147483647) F.throwErrCode(10012);
  120. var redis_timer_id = yield mgr_map.redis.getNextReqId("timer");
  121. redis_timer_id = this.timer_prefix + redis_timer_id;
  122. yield mgr_map.redis.setTimerId(redis_timer_id);
  123. return yield this.innerRunTimeout(fn,timeout,redis_timer_id);
  124. };
  125. this.innerClearTimeout = function* (timerid) {
  126. yield mgr_map.redis.delTimerId(timerid);
  127. };
  128. // fn 必须是异步函数
  129. this.innerSetInterval = function* (fn,timeout, starttime) {
  130. if (timeout > 2147483647 || starttime > 2147483647) F.throwErrCode(10014);
  131. mgr_map.logs.roomManInLogs('定时器内部参数:', {'timeout':timeout, 'starttime':starttime});
  132. var redis_timer_id = yield mgr_map.redis.getNextReqId("timer");
  133. redis_timer_id = this.timer_prefix + redis_timer_id;
  134. yield mgr_map.redis.setTimerId(redis_timer_id);
  135. if (!F.isNull(starttime)) {
  136. var timer = this;
  137. yield this.innerRunTimeout(function* () {
  138. yield timer.innerRunInterval(fn,timeout,redis_timer_id);
  139. },starttime,redis_timer_id,false);
  140. } else {
  141. yield this.innerRunInterval(fn,timeout,redis_timer_id);
  142. }
  143. return redis_timer_id;
  144. };
  145. this.innerClearInterval = function* (timerid) {
  146. yield mgr_map.redis.delTimerId(timerid);
  147. };
  148. };
  149. module.exports = timerMgr;