cronJob.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 'use strict';
  2. var C = require('../config');
  3. var F = require('../common/function');
  4. var _ = require('underscore');
  5. _.str = require('underscore.string');
  6. var async_request = require('request');
  7. var crypto = require('crypto');
  8. var co = require('co');
  9. function cronJobMgr(app, common_mgr) {
  10. var model_map = app.model_mgr.model_map;
  11. var mgr_map = common_mgr.mgr_map;
  12. this.job_list = [];
  13. var that = this;
  14. // fn必须是异步函数
  15. this.addJob = function (time_str,fn) {
  16. //if (F.isTime(time_str)) {
  17. this.job_list.push([time_str,fn]);
  18. //} else {
  19. //F.log("error:","add job err: timestr %s format not right",[time_str]);
  20. //}
  21. };
  22. this.isSubMatch = function(systime,selftime) {
  23. systime = systime.toString();
  24. selftime = selftime.toString();
  25. if ("*" == selftime) return true;
  26. if (systime == selftime) return true;
  27. if (selftime.indexOf("*/") == 0) {
  28. var ival = parseInt(systime);
  29. selftime = selftime.replace("*/",ival.toString()+"%");
  30. var res = eval(selftime);
  31. return res == 0;
  32. }
  33. return false;
  34. };
  35. this.isMatch = function(systime,selftime) {
  36. var [y,M,d,h,m,s] = selftime.split(/[- :]/);
  37. var [sysy,sysM,sysd,sysh,sysm,syss] = systime.split(/[- :]/);
  38. if (that.isSubMatch(sysy,y) && that.isSubMatch(sysM,M) && that.isSubMatch(sysd,d) &&
  39. that.isSubMatch(sysh,h) && that.isSubMatch(sysm,m) && that.isSubMatch(syss,s)) {
  40. return true;
  41. }
  42. //console.log(selftime,systime,"not match");
  43. return false;
  44. };
  45. this.execJob = function* (cur_time_str) {
  46. for (var i = 0; i < that.job_list.length; ++i) {
  47. var [time_str,fn] = that.job_list[i];
  48. if (that.isMatch(cur_time_str,time_str)) {
  49. try {
  50. co(fn(cur_time_str));
  51. } catch (e) {
  52. console.log(e);
  53. F.addErrLogs(["execJob err:",e.stack]);
  54. }
  55. }
  56. }
  57. };
  58. var cur_time = null; // 计数时间 秒,为了保证每秒都执行
  59. var sys_time = null; // 系统时间 秒,为了保证跟上系统时间
  60. this.start = function() {
  61. var genfn = function* () {
  62. yield mgr_map.dbTimer.revertLastRunningJob();
  63. while(1) {
  64. yield F.sleep(1000);
  65. if (cur_time == null) cur_time = parseInt(new Date().getTime()/1000);
  66. sys_time = parseInt(new Date().getTime()/1000);
  67. //F.addErrLogs(["exe job:",cur_time,sys_time]);
  68. while (cur_time <= sys_time) {
  69. var runtime = new Date();
  70. runtime.setTime(cur_time*1000);
  71. var cur_time_str = runtime.format("yyyy-MM-dd HH:mm:ss");
  72. //F.addErrLogs(["exe job cur_time_str:",cur_time_str]);
  73. co(that.execJob(cur_time_str));
  74. cur_time = cur_time + 1;
  75. }
  76. }
  77. };
  78. co(genfn());
  79. }
  80. };
  81. module.exports = cronJobMgr;