@futdevpro/nts-dynamo 1.6.18 → 1.6.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,12 @@
1
1
 
2
2
  import Mongoose = require("mongoose");
3
3
  import Express = require("express");
4
+ import * as Http from 'http';
4
5
  import * as Https from 'https';
5
6
  import * as FileSystem from 'fs';
6
7
  import * as BodyParser from 'body-parser';
7
8
 
8
- import { Dynamo_Log } from '@futdevpro/fsm-dynamo';
9
+ import { delay, Dynamo_Array, Dynamo_Error, Dynamo_Log, second } from '@futdevpro/fsm-dynamo';
9
10
 
10
11
  import { DynamoNTS_SingletonService } from '../base/singleton.service';
11
12
  import { DynamoNTS_RouteSecurity } from '../../_enums/route-security.enum';
@@ -177,6 +178,15 @@ import { DynamoNTS_EndpointParams } from '../../_models/control-models/endpoint-
177
178
  export abstract class DynamoNTS_App extends DynamoNTS_SingletonService {
178
179
 
179
180
  started: boolean = false;
181
+ private initDone: boolean = false;
182
+ private haveMongoose: boolean = false;
183
+ private mongooseStarted: boolean = false;
184
+ private haveHttpServer: boolean = false;
185
+ private httpServerStarted: boolean = false;
186
+ private haveHttpsServer: boolean = false;
187
+ private httpsServerStarted: boolean = false;
188
+ private constructErrors: (Error | Dynamo_Error)[] = [];
189
+
180
190
  get serverName(): string { return this.params.name; }
181
191
 
182
192
  private _params: DynamoNTS_AppParams;
@@ -196,6 +206,9 @@ export abstract class DynamoNTS_App extends DynamoNTS_SingletonService {
196
206
  protected openExpress: Express.Application;
197
207
  private secureExpress: Express.Application;
198
208
  protected httpsServer: Https.Server;
209
+ protected httpServer: Http.Server;
210
+
211
+ private globalService: DynamoNTS_GlobalService;
199
212
 
200
213
  private _routingModules: DynamoNTS_RoutingModule[] = [];
201
214
  /* protected get routingModules(): DynamoNTS_RoutingModule[] { return this._routingModules; } */
@@ -217,6 +230,7 @@ export abstract class DynamoNTS_App extends DynamoNTS_SingletonService {
217
230
  this.overrideDynamoNTSGlobalSettings();
218
231
  }
219
232
 
233
+ this.globalService = DynamoNTS_GlobalService.getInstance();
220
234
  DynamoNTS_GlobalService.setServices(this.getGlobalServiceCollection());
221
235
  DynamoNTS_GlobalService.setParams(this.params);
222
236
 
@@ -273,21 +287,119 @@ export abstract class DynamoNTS_App extends DynamoNTS_SingletonService {
273
287
  this.postProcess();
274
288
  }
275
289
 
276
- this.started = true;
290
+ this.initDone = true;
291
+ this.ready();
277
292
  } catch (error) {
293
+ this.constructErrors.push(error);
278
294
  Dynamo_Log.error('\nApplication start failed.\n', error);
279
295
  }
280
296
  }
281
297
 
298
+ async ready(timeout: number = 4 * second): Promise<void> {
299
+ return new Promise(async (resolve, reject) => {
300
+ let ready: boolean = false;
301
+ const start: number = +new Date();
302
+
303
+ if (this.constructErrors.length) {
304
+ reject({
305
+ message: 'Application start failed. TIMEOUT',
306
+ errors: this.constructErrors
307
+ });
308
+ }
309
+
310
+ while (!ready && +new Date() - start < timeout) {
311
+ await delay(100);
312
+
313
+ if (this.initDone) {
314
+ ready = (
315
+ this.haveMongoose ? this.mongooseStarted : true &&
316
+ this.haveHttpServer ? this.httpServerStarted : true &&
317
+ this.haveHttpsServer ? this.httpsServerStarted : true
318
+ );
319
+ }
320
+ }
321
+
322
+ if (this.constructErrors.length) {
323
+ reject({
324
+ message: 'Application start failed. TIMEOUT',
325
+ errors: this.constructErrors
326
+ });
327
+ }
328
+
329
+ if (ready) {
330
+ this.started = true;
331
+
332
+ resolve();
333
+ } else {
334
+ this.started = false;
335
+
336
+ let msg: string = 'Application start failed. TIMEOUT';
337
+ if (this.haveMongoose && !this.mongooseStarted) {
338
+ msg += '\nMongoose start failed.';
339
+ }
340
+ if (this.haveHttpServer && !this.httpServerStarted) {
341
+ msg += '\nHTTP Server start failed.';
342
+ }
343
+ if (this.haveHttpsServer && !this.httpsServerStarted) {
344
+ msg += '\nHTTPS Server start failed.';
345
+ }
346
+
347
+
348
+
349
+ reject({
350
+ message: 'Application start failed. TIMEOUT',
351
+ errors: this.constructErrors
352
+ });
353
+ }
354
+ });
355
+ }
356
+
357
+ async stop(): Promise<void> {
358
+ if (this.started) {
359
+
360
+ if (this.haveHttpServer) {
361
+ await new Promise((resolve) => {
362
+ this.httpServer.close(resolve);
363
+ });
364
+ }
365
+ if (this.haveHttpsServer) {
366
+ await new Promise((resolve) => {
367
+ this.httpsServer.close(resolve);
368
+ });
369
+ }
370
+ if (this.haveMongoose) {
371
+ await Dynamo_Array.asyncForEach(Object.keys(this.mongoose.models), async (modelName) => {
372
+ await this.mongoose.deleteModel(modelName);
373
+ });
374
+ await this.mongoose.disconnect();
375
+ }
376
+
377
+ this.initDone = false;
378
+ this.haveHttpServer = false;
379
+ this.haveHttpsServer = false;
380
+ this.haveMongoose = false;
381
+ this.mongooseStarted = false;
382
+ this.httpServerStarted = false;
383
+ this.httpsServerStarted = false;
384
+ this.started = false;
385
+
386
+ Dynamo_Log.success('\nServer stopped.\n');
387
+ }
388
+ }
389
+
282
390
  /**
283
391
  *
284
392
  */
285
393
  private startDB(): void {
394
+ this.haveMongoose = true;
286
395
  this.mongoose.connection
287
396
  .on('error', (error) => {
397
+ this.mongooseStarted = false;
398
+ this.constructErrors.push(error);
288
399
  Dynamo_Log.error('\nUnable to connect to MongoDB server, ERROR: ', error);
289
400
  })
290
401
  .once('open', () => {
402
+ this.mongooseStarted = true;
291
403
  Dynamo_Log.success('\nConnected to MongoDB\n');
292
404
  });
293
405
 
@@ -403,11 +515,15 @@ export abstract class DynamoNTS_App extends DynamoNTS_SingletonService {
403
515
  private async startExpresses(): Promise<void> {
404
516
  try {
405
517
  if (this._security && this._security !== DynamoNTS_RouteSecurity.open) {
518
+ this.haveHttpsServer = true;
406
519
  this.httpsServer
407
520
  .listen(this._ports.httpsPort, 'localhost', 0, () => {
521
+ this.httpsServerStarted = true;
408
522
  Dynamo_Log.success(`\nHTTPS (secure) server is listening on port ${this._ports.httpsPort}`);
409
523
  })
410
524
  .on('error', (error) => {
525
+ this.httpsServerStarted = false;
526
+ this.constructErrors.push(error);
411
527
  Dynamo_Log.error(`\nHTTPS (secure) server ERROR`, error);
412
528
  })
413
529
  .on('uncaughtException', (ex) => {
@@ -416,11 +532,15 @@ export abstract class DynamoNTS_App extends DynamoNTS_SingletonService {
416
532
  }
417
533
 
418
534
  if (this._security && this._security !== DynamoNTS_RouteSecurity.secure) {
419
- this.openExpress
535
+ this.haveHttpServer = true;
536
+ this.httpServer = this.openExpress
420
537
  .listen(this._ports.httpPort, () => {
538
+ this.httpServerStarted = true;
421
539
  Dynamo_Log.success(`\nHTTP (-open-) server is listening on port ${this._ports.httpPort}`);
422
540
  })
423
541
  .on('error', (error) => {
542
+ this.httpServerStarted = false;
543
+ this.constructErrors.push(error);
424
544
  Dynamo_Log.error(`\nHTTP (-open-) server ERROR`, error);
425
545
  })
426
546
  .on('uncaughtException', (ex) => {