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