cron-svr.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*********************** 对koa.io进行了rpc封装,支持通讯里面用yield ******************/
  2. 'use strict';
  3. var co = require('co');
  4. var koa = require('koa.io');
  5. var rpcServer = require('./libs/rpcserver');
  6. var wordFilter = require('./libs/wordfilter');
  7. var jsonp = require('koa-safe-jsonp');
  8. var router = require('koa-router');
  9. var bodyParser = require('koa-better-body');
  10. var _ = require('underscore');
  11. _.str = require('underscore.string');
  12. _.v = require('validator');
  13. var gzip = require('koa-gzip');//压缩页面
  14. var session = require('koa-generic-session');
  15. var redisStore = require('koa-redis');
  16. var cors = require('koa-cors');
  17. var F = require('./common/function');
  18. var C = require('./config');
  19. var apiPre = C.apiPre;
  20. var modelMgr = require('./manager/model');
  21. var commonMgr = require('./manager/common');
  22. var app = new rpcServer();
  23. var httpapp = app.app;
  24. httpapp.use(bodyParser());
  25. jsonp(httpapp, {
  26. callback: '_callback', // default is 'callback'
  27. limit: 50 // max callback name string length, default is 512
  28. });
  29. httpapp.proxy = true;
  30. httpapp.use(cors({credentials: true}));
  31. httpapp.use(gzip());
  32. httpapp.keys = ['keys', 'keykeys'];
  33. httpapp.use(session({
  34. // 默认采用 memory 方式
  35. store: redisStore({
  36. host:C.redis.host,
  37. db:C.redis.db,
  38. pass: C.redis.options.auth_pass
  39. }),
  40. key: C.session.key,
  41. prefix: C.session.prefix,
  42. rolling:true,//always reset the cookie and sessions, default false
  43. ttl: C.session.ttl*1000//此处单位为毫秒,所以得乘于1000
  44. }));
  45. httpapp.use(router(httpapp));
  46. ///////渲染模板
  47. var render = require('koa-ejs');
  48. render(httpapp, {
  49. root: './view',
  50. layout: 'template',
  51. viewExt: 'html',
  52. cache: false,
  53. debug: true
  54. });
  55. var port = C.cronsvr_port;
  56. app.port = port;
  57. app.listen(port, function () {
  58. console.log('Server listening at port %d', port);
  59. });
  60. app.model_mgr = new modelMgr();
  61. app.common_mgr = new commonMgr(app);
  62. try{
  63. app.common_mgr.mgr_map.cronJobLogic.batchAddJob();
  64. app.common_mgr.mgr_map.cronJob.start();
  65. co(app.common_mgr.mgr_map.im.clearLock());
  66. }catch(e) {
  67. F.addErrLogs(["cron err:", e.stack]);
  68. }