1 |
|
2 | var async, red, delegateEvent, EventEmitter, ExoComm, N, portReservation, path, ServiceRunner, waitUntil, AppRunner;
|
3 | async = require('async');
|
4 | red = require('chalk').red;
|
5 | delegateEvent = require('rails-delegate').delegateEvent;
|
6 | EventEmitter = require('events').EventEmitter;
|
7 | ExoComm = require('exocomm-dev');
|
8 | N = require('nitroglycerin');
|
9 | portReservation = require('port-reservation');
|
10 | path = require('path');
|
11 | ServiceRunner = require('./service-runner');
|
12 | waitUntil = require('wait').waitUntil;
|
13 | AppRunner = (function(superclass){
|
14 | var prototype = extend$((import$(AppRunner, superclass).displayName = 'AppRunner', AppRunner), superclass).prototype, constructor = AppRunner;
|
15 | function AppRunner(appConfig){
|
16 | this.appConfig = appConfig;
|
17 | }
|
18 | prototype.startExocomm = function(done){
|
19 | var this$ = this;
|
20 | return portReservation.getPort(N(function(exocommPort){
|
21 | var x$;
|
22 | this$.exocommPort = exocommPort;
|
23 | x$ = this$.exocomm = new ExoComm;
|
24 | x$.on('listening', function(port){
|
25 | return this$.emit('exocomm-online', port);
|
26 | });
|
27 | x$.listen(this$.exocommPort);
|
28 | return delegateEvent('error', 'routing-setup', 'message', {
|
29 | from: this$.exocomm,
|
30 | to: this$
|
31 | });
|
32 | }));
|
33 | };
|
34 | prototype.startServices = function(){
|
35 | var this$ = this;
|
36 | return waitUntil(function(){
|
37 | return this$.exocommPort;
|
38 | }, 1, function(){
|
39 | var names, i$, len$, name, serviceDir, x$, _, runner;
|
40 | names = Object.keys(this$.appConfig.services);
|
41 | this$.runners = {};
|
42 | for (i$ = 0, len$ = names.length; i$ < len$; ++i$) {
|
43 | name = names[i$];
|
44 | serviceDir = path.join(process.cwd(), this$.appConfig.services[name].location);
|
45 | x$ = this$.runners[name] = new ServiceRunner(name, {
|
46 | root: serviceDir,
|
47 | EXOCOMM_PORT: this$.exocommPort
|
48 | });
|
49 | x$.on('online', fn$);
|
50 | x$.on('output', fn1$);
|
51 | }
|
52 | return async.parallel((function(){
|
53 | var ref$, results$ = [];
|
54 | for (_ in ref$ = this.runners) {
|
55 | runner = ref$[_];
|
56 | results$.push(runner.start);
|
57 | }
|
58 | return results$;
|
59 | }.call(this$)), function(err){
|
60 | return this$.emit('all-services-online');
|
61 | });
|
62 | function fn$(name){
|
63 | return this$.emit('service-online', name);
|
64 | }
|
65 | function fn1$(data){
|
66 | return this$.emit('output', data);
|
67 | }
|
68 | });
|
69 | };
|
70 | prototype.portFor = function(serviceName){
|
71 | return this.runners[serviceName].config.EXORELAY_PORT;
|
72 | };
|
73 | prototype.sendServiceConfiguration = function(){
|
74 | var config, res$, serviceName, ref$, serviceData, runner;
|
75 | res$ = [];
|
76 | for (serviceName in ref$ = this.appConfig.services) {
|
77 | serviceData = ref$[serviceName];
|
78 | runner = this.runners[serviceName];
|
79 | res$.push({
|
80 | name: serviceName,
|
81 | host: 'localhost',
|
82 | port: runner.config.EXORELAY_PORT,
|
83 | sends: runner.serviceConfig.messages.sends,
|
84 | receives: runner.serviceConfig.messages.receives
|
85 | });
|
86 | }
|
87 | config = res$;
|
88 | this.exocomm.setServices(config);
|
89 | return this.emit('routing-done');
|
90 | };
|
91 | return AppRunner;
|
92 | }(EventEmitter));
|
93 | module.exports = AppRunner;
|
94 | function extend$(sub, sup){
|
95 | function fun(){} fun.prototype = (sub.superclass = sup).prototype;
|
96 | (sub.prototype = new fun).constructor = sub;
|
97 | if (typeof sup.extended == 'function') sup.extended(sub);
|
98 | return sub;
|
99 | }
|
100 | function import$(obj, src){
|
101 | var own = {}.hasOwnProperty;
|
102 | for (var key in src) if (own.call(src, key)) obj[key] = src[key];
|
103 | return obj;
|
104 | } |
\ | No newline at end of file |