1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var utils = require('./util/utils');
|
11 | var logger = require('myhero-logger').getLogger('myhero', __filename);
|
12 | var EventEmitter = require('events').EventEmitter;
|
13 | var events = require('./util/events');
|
14 | var appUtil = require('./util/appUtil');
|
15 | var Constants = require('./util/constants');
|
16 | var appManager = require('./common/manager/appManager');
|
17 | var fs = require('fs');
|
18 | var path = require('path');
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | var Application = module.exports = {};
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | var STATE_INITED = 1;
|
31 | var STATE_START = 2;
|
32 | var STATE_STARTED = 3;
|
33 | var STATE_STOPED = 4;
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | Application.init = function (opts) {
|
41 | opts = opts || {};
|
42 | this.loaded = [];
|
43 | this.components = {};
|
44 | this.settings = {};
|
45 | var base = opts.base || path.dirname(require.main.filename);
|
46 | this.set(Constants.RESERVED.BASE, base, true);
|
47 | this.event = new EventEmitter();
|
48 |
|
49 |
|
50 | this.serverId = null;
|
51 | this.serverType = null;
|
52 | this.curServer = null;
|
53 | this.startTime = null;
|
54 |
|
55 |
|
56 | this.master = null;
|
57 | this.servers = {};
|
58 | this.serverTypeMaps = {};
|
59 | this.serverTypes = [];
|
60 | this.lifecycleCbs = {};
|
61 | this.clusterSeq = {};
|
62 |
|
63 | appUtil.defaultConfiguration(this);
|
64 |
|
65 | this.state = STATE_INITED;
|
66 | logger.info('application inited: %j', this.getServerId());
|
67 | };
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | Application.getBase = function () {
|
81 | return this.get(Constants.RESERVED.BASE);
|
82 | };
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | Application.require = function (ph) {
|
92 | return require(path.join(Application.getBase(), ph));
|
93 | };
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | Application.configureLogger = function (logger) {
|
103 | if (process.env.POMELO_LOGGER !== 'off') {
|
104 | var base = this.getBase();
|
105 | var env = this.get(Constants.RESERVED.ENV);
|
106 | var originPath = path.join(base, Constants.FILEPATH.LOG);
|
107 | var presentPath = path.join(base, Constants.FILEPATH.CONFIG_DIR, env, path.basename(Constants.FILEPATH.LOG));
|
108 | if (fs.existsSync(originPath)) {
|
109 | logger.configure(originPath, {serverId: this.serverId, base: base});
|
110 | } else if (fs.existsSync(presentPath)) {
|
111 | logger.configure(presentPath, {serverId: this.serverId, base: base});
|
112 | } else {
|
113 | logger.error('logger file path configuration is error.');
|
114 | }
|
115 | }
|
116 | };
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | Application.filter = function (filter) {
|
126 | this.before(filter);
|
127 | this.after(filter);
|
128 | };
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | Application.before = function (bf) {
|
137 | addFilter(this, Constants.KEYWORDS.BEFORE_FILTER, bf);
|
138 | };
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | Application.after = function (af) {
|
147 | addFilter(this, Constants.KEYWORDS.AFTER_FILTER, af);
|
148 | };
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | Application.globalFilter = function (filter) {
|
158 | this.globalBefore(filter);
|
159 | this.globalAfter(filter);
|
160 | };
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | Application.globalBefore = function (bf) {
|
169 | addFilter(this, Constants.KEYWORDS.GLOBAL_BEFORE_FILTER, bf);
|
170 | };
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | Application.globalAfter = function (af) {
|
179 | addFilter(this, Constants.KEYWORDS.GLOBAL_AFTER_FILTER, af);
|
180 | };
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | Application.rpcBefore = function (bf) {
|
189 | addFilter(this, Constants.KEYWORDS.RPC_BEFORE_FILTER, bf);
|
190 | };
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | Application.rpcAfter = function (af) {
|
199 | addFilter(this, Constants.KEYWORDS.RPC_AFTER_FILTER, af);
|
200 | };
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 | Application.rpcFilter = function (filter) {
|
210 | this.rpcBefore(filter);
|
211 | this.rpcAfter(filter);
|
212 | };
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 | Application.load = function (name, component, opts) {
|
224 | if (typeof name !== 'string') {
|
225 | opts = component;
|
226 | component = name;
|
227 | name = null;
|
228 | if (typeof component.name === 'string') {
|
229 | name = component.name;
|
230 | }
|
231 | }
|
232 |
|
233 | if (typeof component === 'function') {
|
234 | component = component(this, opts);
|
235 | }
|
236 |
|
237 | if (!name && typeof component.name === 'string') {
|
238 | name = component.name;
|
239 | }
|
240 |
|
241 | if (name && this.components[name]) {
|
242 |
|
243 | logger.warn('ignore duplicate component: %j', name);
|
244 | return;
|
245 | }
|
246 |
|
247 | this.loaded.push(component);
|
248 | if (name) {
|
249 |
|
250 | this.components[name] = component;
|
251 | }
|
252 |
|
253 | return this;
|
254 | };
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 | Application.loadConfigBaseApp = function (key, val, reload) {
|
266 | var self = this;
|
267 | var env = this.get(Constants.RESERVED.ENV);
|
268 | var originPath = path.join(Application.getBase(), val);
|
269 | var presentPath = path.join(Application.getBase(), Constants.FILEPATH.CONFIG_DIR, env, path.basename(val));
|
270 | var realPath;
|
271 | if (fs.existsSync(originPath)) {
|
272 | realPath = originPath;
|
273 | var file = require(originPath);
|
274 | if (file[env]) {
|
275 | file = file[env];
|
276 | }
|
277 | this.set(key, file);
|
278 | } else if (fs.existsSync(presentPath)) {
|
279 | realPath = presentPath;
|
280 | var pfile = require(presentPath);
|
281 | this.set(key, pfile);
|
282 | } else {
|
283 | logger.error('invalid configuration with file path: %s', key);
|
284 | }
|
285 |
|
286 | if (!!realPath && !!reload) {
|
287 | fs.watch(realPath, function (event, filename) {
|
288 | if (event === 'change') {
|
289 | delete require.cache[require.resolve(realPath)];
|
290 | self.loadConfigBaseApp(key, val);
|
291 | }
|
292 | });
|
293 | }
|
294 | };
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 | Application.loadConfig = function (key, val) {
|
305 | var env = this.get(Constants.RESERVED.ENV);
|
306 | val = require(val);
|
307 | if (val[env]) {
|
308 | val = val[env];
|
309 | }
|
310 | this.set(key, val);
|
311 | };
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 | Application.route = function (serverType, routeFunc) {
|
332 | var routes = this.get(Constants.KEYWORDS.ROUTE);
|
333 | if (!routes) {
|
334 | routes = {};
|
335 | this.set(Constants.KEYWORDS.ROUTE, routes);
|
336 | }
|
337 | routes[serverType] = routeFunc;
|
338 | return this;
|
339 | };
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 | Application.beforeStopHook = function (fun) {
|
349 | logger.warn('this method was deprecated in pomelo 0.8');
|
350 | if (!!fun && typeof fun === 'function') {
|
351 | this.set(Constants.KEYWORDS.BEFORE_STOP_HOOK, fun);
|
352 | }
|
353 | };
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 | Application.start = function (cb) {
|
362 | this.startTime = Date.now();
|
363 | if (this.state > STATE_INITED) {
|
364 | utils.invokeCallback(cb, new Error('application has already start.'));
|
365 | return;
|
366 | }
|
367 |
|
368 | var self = this;
|
369 | appUtil.startByType(self, function () {
|
370 | appUtil.loadDefaultComponents(self);
|
371 | var startUp = function () {
|
372 | appUtil.optComponents(self.loaded, Constants.RESERVED.START, function (err) {
|
373 | self.state = STATE_START;
|
374 | if (err) {
|
375 | utils.invokeCallback(cb, err);
|
376 | } else {
|
377 | logger.info('%j enter after start...', self.getServerId());
|
378 | self.afterStart(cb);
|
379 | }
|
380 | });
|
381 | };
|
382 | var beforeFun = self.lifecycleCbs[Constants.LIFECYCLE.BEFORE_STARTUP];
|
383 | if (!!beforeFun) {
|
384 | beforeFun.call(null, self, startUp);
|
385 | } else {
|
386 | startUp();
|
387 | }
|
388 | });
|
389 | };
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 | Application.afterStart = function (cb) {
|
398 | if (this.state !== STATE_START) {
|
399 | utils.invokeCallback(cb, new Error('application is not running now.'));
|
400 | return;
|
401 | }
|
402 |
|
403 | var afterFun = this.lifecycleCbs[Constants.LIFECYCLE.AFTER_STARTUP];
|
404 | var self = this;
|
405 | appUtil.optComponents(this.loaded, Constants.RESERVED.AFTER_START, function (err) {
|
406 | self.state = STATE_STARTED;
|
407 | var id = self.getServerId();
|
408 | if (!err) {
|
409 | logger.info('%j finish start', id);
|
410 | }
|
411 | if (!!afterFun) {
|
412 | afterFun.call(null, self, function () {
|
413 | utils.invokeCallback(cb, err);
|
414 | });
|
415 | } else {
|
416 | utils.invokeCallback(cb, err);
|
417 | }
|
418 | var usedTime = Date.now() - self.startTime;
|
419 | logger.info('%j startup in %s ms', id, usedTime);
|
420 | self.event.emit(events.START_SERVER, id);
|
421 | });
|
422 | };
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 | Application.stop = function (force) {
|
430 | if (this.state > STATE_STARTED) {
|
431 | logger.warn('[pomelo application] application is not running now.');
|
432 | return;
|
433 | }
|
434 | this.state = STATE_STOPED;
|
435 | var self = this;
|
436 |
|
437 | this.stopTimer = setTimeout(function () {
|
438 | process.exit(0);
|
439 | }, Constants.TIME.TIME_WAIT_STOP);
|
440 |
|
441 | var cancelShutDownTimer = function () {
|
442 | if (!!self.stopTimer) {
|
443 | clearTimeout(self.stopTimer);
|
444 | }
|
445 | };
|
446 | var shutDown = function () {
|
447 | appUtil.stopComps(self.loaded, 0, force, function () {
|
448 | cancelShutDownTimer();
|
449 | if (force) {
|
450 | process.exit(0);
|
451 | }
|
452 | });
|
453 | };
|
454 | var fun = this.get(Constants.KEYWORDS.BEFORE_STOP_HOOK);
|
455 | var stopFun = this.lifecycleCbs[Constants.LIFECYCLE.BEFORE_SHUTDOWN];
|
456 | if (!!stopFun) {
|
457 | stopFun.call(null, this, shutDown, cancelShutDownTimer);
|
458 | } else if (!!fun) {
|
459 | utils.invokeCallback(fun, self, shutDown, cancelShutDownTimer);
|
460 | } else {
|
461 | shutDown();
|
462 | }
|
463 | };
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 |
|
480 |
|
481 |
|
482 |
|
483 |
|
484 | Application.set = function (setting, val, attach) {
|
485 | if (arguments.length === 1) {
|
486 | return this.settings[setting];
|
487 | }
|
488 | this.settings[setting] = val;
|
489 | if (attach) {
|
490 | this[setting] = val;
|
491 | }
|
492 | return this;
|
493 | };
|
494 |
|
495 |
|
496 |
|
497 |
|
498 |
|
499 |
|
500 |
|
501 |
|
502 | Application.get = function (setting) {
|
503 | return this.settings[setting];
|
504 | };
|
505 |
|
506 |
|
507 |
|
508 |
|
509 |
|
510 |
|
511 |
|
512 |
|
513 | Application.enabled = function (setting) {
|
514 | return !!this.get(setting);
|
515 | };
|
516 |
|
517 |
|
518 |
|
519 |
|
520 |
|
521 |
|
522 |
|
523 |
|
524 | Application.disabled = function (setting) {
|
525 | return !this.get(setting);
|
526 | };
|
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 | Application.enable = function (setting) {
|
536 | return this.set(setting, true);
|
537 | };
|
538 |
|
539 |
|
540 |
|
541 |
|
542 |
|
543 |
|
544 |
|
545 |
|
546 | Application.disable = function (setting) {
|
547 | return this.set(setting, false);
|
548 | };
|
549 |
|
550 |
|
551 |
|
552 |
|
553 |
|
554 |
|
555 |
|
556 |
|
557 |
|
558 |
|
559 |
|
560 |
|
561 |
|
562 |
|
563 |
|
564 |
|
565 |
|
566 |
|
567 |
|
568 |
|
569 |
|
570 |
|
571 |
|
572 |
|
573 |
|
574 |
|
575 |
|
576 | Application.configure = function (env, type, fn) {
|
577 | var args = [].slice.call(arguments);
|
578 | fn = args.pop();
|
579 | env = type = Constants.RESERVED.ALL;
|
580 |
|
581 | if (args.length > 0) {
|
582 | env = args[0];
|
583 | }
|
584 | if (args.length > 1) {
|
585 | type = args[1];
|
586 | }
|
587 |
|
588 | if (env === Constants.RESERVED.ALL || contains(this.settings.env, env)) {
|
589 | if (type === Constants.RESERVED.ALL || contains(this.settings.serverType, type)) {
|
590 | fn.call(this);
|
591 | }
|
592 | }
|
593 | return this;
|
594 | };
|
595 |
|
596 |
|
597 |
|
598 |
|
599 |
|
600 |
|
601 |
|
602 |
|
603 |
|
604 | Application.registerAdmin = function (moduleId, module, opts) {
|
605 | var modules = this.get(Constants.KEYWORDS.MODULE);
|
606 | if (!modules) {
|
607 | modules = {};
|
608 | this.set(Constants.KEYWORDS.MODULE, modules);
|
609 | }
|
610 |
|
611 | if (typeof moduleId !== 'string') {
|
612 | opts = module;
|
613 | module = moduleId;
|
614 | if (module) {
|
615 | moduleId = module.moduleId;
|
616 | }
|
617 | }
|
618 |
|
619 | if (!moduleId) {
|
620 | return;
|
621 | }
|
622 |
|
623 | modules[moduleId] = {
|
624 | moduleId: moduleId,
|
625 | module: module,
|
626 | opts: opts
|
627 | };
|
628 | };
|
629 |
|
630 |
|
631 |
|
632 |
|
633 |
|
634 |
|
635 |
|
636 |
|
637 | Application.use = function (plugin, opts) {
|
638 | if (!plugin.components) {
|
639 | logger.error('invalid components, no components exist');
|
640 | return;
|
641 | }
|
642 |
|
643 | var self = this;
|
644 | opts = opts || {};
|
645 | var dir = path.dirname(plugin.components);
|
646 |
|
647 | if (!fs.existsSync(plugin.components)) {
|
648 | logger.error('fail to find components, find path: %s', plugin.components);
|
649 | return;
|
650 | }
|
651 |
|
652 | fs.readdirSync(plugin.components).forEach(function (filename) {
|
653 | if (!/\.js$/.test(filename)) {
|
654 | return;
|
655 | }
|
656 | var name = path.basename(filename, '.js');
|
657 | var param = opts[name] || {};
|
658 | var absolutePath = path.join(dir, Constants.DIR.COMPONENT, filename);
|
659 | if (!fs.existsSync(absolutePath)) {
|
660 | logger.error('component %s not exist at %s', name, absolutePath);
|
661 | } else {
|
662 | self.load(require(absolutePath), param);
|
663 | }
|
664 | });
|
665 |
|
666 |
|
667 | if (!plugin.events) {
|
668 | return;
|
669 | } else {
|
670 | if (!fs.existsSync(plugin.events)) {
|
671 | logger.error('fail to find events, find path: %s', plugin.events);
|
672 | return;
|
673 | }
|
674 |
|
675 | fs.readdirSync(plugin.events).forEach(function (filename) {
|
676 | if (!/\.js$/.test(filename)) {
|
677 | return;
|
678 | }
|
679 | var absolutePath = path.join(dir, Constants.DIR.EVENT, filename);
|
680 | if (!fs.existsSync(absolutePath)) {
|
681 | logger.error('events %s not exist at %s', filename, absolutePath);
|
682 | } else {
|
683 | bindEvents(require(absolutePath), self);
|
684 | }
|
685 | });
|
686 | }
|
687 | };
|
688 |
|
689 |
|
690 |
|
691 |
|
692 |
|
693 |
|
694 |
|
695 |
|
696 |
|
697 |
|
698 |
|
699 | Application.transaction = function (name, conditions, handlers, retry) {
|
700 | appManager.transaction(name, conditions, handlers, retry);
|
701 | };
|
702 |
|
703 |
|
704 |
|
705 |
|
706 |
|
707 |
|
708 |
|
709 | Application.getMaster = function () {
|
710 | return this.master;
|
711 | };
|
712 |
|
713 |
|
714 |
|
715 |
|
716 |
|
717 |
|
718 |
|
719 | Application.getCurServer = function () {
|
720 | return this.curServer;
|
721 | };
|
722 |
|
723 |
|
724 |
|
725 |
|
726 |
|
727 |
|
728 |
|
729 | Application.getServerId = function () {
|
730 | return this.serverId;
|
731 | };
|
732 |
|
733 |
|
734 |
|
735 |
|
736 |
|
737 |
|
738 |
|
739 | Application.getServerType = function () {
|
740 | return this.serverType;
|
741 | };
|
742 |
|
743 |
|
744 |
|
745 |
|
746 |
|
747 |
|
748 |
|
749 | Application.getServers = function () {
|
750 | return this.servers;
|
751 | };
|
752 |
|
753 |
|
754 |
|
755 |
|
756 |
|
757 |
|
758 |
|
759 | Application.getServersFromConfig = function () {
|
760 | return this.get(Constants.KEYWORDS.SERVER_MAP);
|
761 | };
|
762 |
|
763 |
|
764 |
|
765 |
|
766 |
|
767 |
|
768 |
|
769 | Application.getServerTypes = function () {
|
770 | return this.serverTypes;
|
771 | };
|
772 |
|
773 |
|
774 |
|
775 |
|
776 |
|
777 |
|
778 |
|
779 |
|
780 | Application.getServerById = function (serverId) {
|
781 | return this.servers[serverId];
|
782 | };
|
783 |
|
784 |
|
785 |
|
786 |
|
787 |
|
788 |
|
789 |
|
790 |
|
791 |
|
792 | Application.getServerFromConfig = function (serverId) {
|
793 | return this.get(Constants.KEYWORDS.SERVER_MAP)[serverId];
|
794 | };
|
795 |
|
796 |
|
797 |
|
798 |
|
799 |
|
800 |
|
801 |
|
802 |
|
803 | Application.getServersByType = function (serverType) {
|
804 | return this.serverTypeMaps[serverType];
|
805 | };
|
806 |
|
807 |
|
808 |
|
809 |
|
810 |
|
811 |
|
812 |
|
813 |
|
814 |
|
815 |
|
816 | Application.isFrontend = function (server) {
|
817 | server = server || this.getCurServer();
|
818 | return !!server && server.frontend === 'true';
|
819 | };
|
820 |
|
821 |
|
822 |
|
823 |
|
824 |
|
825 |
|
826 |
|
827 |
|
828 |
|
829 | Application.isBackend = function (server) {
|
830 | server = server || this.getCurServer();
|
831 | return !!server && !server.frontend;
|
832 | };
|
833 |
|
834 |
|
835 |
|
836 |
|
837 |
|
838 |
|
839 |
|
840 | Application.isMaster = function () {
|
841 | return this.serverType === Constants.RESERVED.MASTER;
|
842 | };
|
843 |
|
844 |
|
845 |
|
846 |
|
847 |
|
848 |
|
849 |
|
850 | Application.addServers = function (servers) {
|
851 | if (!servers || !servers.length) {
|
852 | return;
|
853 | }
|
854 |
|
855 | var item, slist;
|
856 | for (var i = 0, l = servers.length; i < l; i++) {
|
857 | item = servers[i];
|
858 |
|
859 | this.servers[item.id] = item;
|
860 |
|
861 |
|
862 | slist = this.serverTypeMaps[item.serverType];
|
863 | if (!slist) {
|
864 | this.serverTypeMaps[item.serverType] = slist = [];
|
865 | }
|
866 | replaceServer(slist, item);
|
867 |
|
868 |
|
869 | if (this.serverTypes.indexOf(item.serverType) < 0) {
|
870 | this.serverTypes.push(item.serverType);
|
871 | }
|
872 | }
|
873 | this.event.emit(events.ADD_SERVERS, servers);
|
874 | };
|
875 |
|
876 |
|
877 |
|
878 |
|
879 |
|
880 |
|
881 |
|
882 | Application.removeServers = function (ids) {
|
883 | if (!ids || !ids.length) {
|
884 | return;
|
885 | }
|
886 |
|
887 | var id, item, slist;
|
888 | for (var i = 0, l = ids.length; i < l; i++) {
|
889 | id = ids[i];
|
890 | item = this.servers[id];
|
891 | if (!item) {
|
892 | continue;
|
893 | }
|
894 |
|
895 | delete this.servers[id];
|
896 |
|
897 |
|
898 | slist = this.serverTypeMaps[item.serverType];
|
899 | removeServer(slist, id);
|
900 |
|
901 | }
|
902 | this.event.emit(events.REMOVE_SERVERS, ids);
|
903 | };
|
904 |
|
905 |
|
906 |
|
907 |
|
908 |
|
909 |
|
910 |
|
911 | Application.replaceServers = function (servers) {
|
912 | if (!servers) {
|
913 | return;
|
914 | }
|
915 |
|
916 | this.servers = servers;
|
917 | this.serverTypeMaps = {};
|
918 | this.serverTypes = [];
|
919 | var serverArray = [];
|
920 | for (var id in servers) {
|
921 | var server = servers[id];
|
922 | var serverType = server[Constants.RESERVED.SERVER_TYPE];
|
923 | var slist = this.serverTypeMaps[serverType];
|
924 | if (!slist) {
|
925 | this.serverTypeMaps[serverType] = slist = [];
|
926 | }
|
927 | this.serverTypeMaps[serverType].push(server);
|
928 |
|
929 | if (this.serverTypes.indexOf(serverType) < 0) {
|
930 | this.serverTypes.push(serverType);
|
931 | }
|
932 | serverArray.push(server);
|
933 | }
|
934 | this.event.emit(events.REPLACE_SERVERS, serverArray);
|
935 | };
|
936 |
|
937 |
|
938 |
|
939 |
|
940 |
|
941 |
|
942 |
|
943 | Application.addCrons = function (crons) {
|
944 | if (!crons || !crons.length) {
|
945 | logger.warn('crons is not defined.');
|
946 | return;
|
947 | }
|
948 | this.event.emit(events.ADD_CRONS, crons);
|
949 | };
|
950 |
|
951 |
|
952 |
|
953 |
|
954 |
|
955 |
|
956 |
|
957 | Application.removeCrons = function (crons) {
|
958 | if (!crons || !crons.length) {
|
959 | logger.warn('ids is not defined.');
|
960 | return;
|
961 | }
|
962 | this.event.emit(events.REMOVE_CRONS, crons);
|
963 | };
|
964 |
|
965 | var replaceServer = function (slist, serverInfo) {
|
966 | for (var i = 0, l = slist.length; i < l; i++) {
|
967 | if (slist[i].id === serverInfo.id) {
|
968 | slist[i] = serverInfo;
|
969 | return;
|
970 | }
|
971 | }
|
972 | slist.push(serverInfo);
|
973 | };
|
974 |
|
975 | var removeServer = function (slist, id) {
|
976 | if (!slist || !slist.length) {
|
977 | return;
|
978 | }
|
979 |
|
980 | for (var i = 0, l = slist.length; i < l; i++) {
|
981 | if (slist[i].id === id) {
|
982 | slist.splice(i, 1);
|
983 | return;
|
984 | }
|
985 | }
|
986 | };
|
987 |
|
988 | var contains = function (str, settings) {
|
989 | if (!settings) {
|
990 | return false;
|
991 | }
|
992 |
|
993 | var ts = settings.split("|");
|
994 | for (var i = 0, l = ts.length; i < l; i++) {
|
995 | if (str === ts[i]) {
|
996 | return true;
|
997 | }
|
998 | }
|
999 | return false;
|
1000 | };
|
1001 |
|
1002 | var bindEvents = function (Event, app) {
|
1003 | var emethods = new Event(app);
|
1004 | for (var m in emethods) {
|
1005 | if (typeof emethods[m] === 'function') {
|
1006 | app.event.on(m, emethods[m].bind(emethods));
|
1007 | }
|
1008 | }
|
1009 | };
|
1010 |
|
1011 | var addFilter = function (app, type, filter) {
|
1012 | var filters = app.get(type);
|
1013 | if (!filters) {
|
1014 | filters = [];
|
1015 | app.set(type, filters);
|
1016 | }
|
1017 | filters.push(filter);
|
1018 | };
|