elasticio-sailor-nodejs 3.0.0-dev7.1 → 3.0.0-sailor-proxy-dev2

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.
package/run.js CHANGED
@@ -24,6 +24,7 @@ function prepareSandbox() {
24
24
  }
25
25
 
26
26
  async function putOutToSea(settings, ipc) {
27
+ logger.trace({ settings }, 'putOutToSea called');
27
28
  ipc.send('init:started');
28
29
  const deferred = Q.defer();
29
30
  sailorInit = deferred.promise;
@@ -31,6 +32,7 @@ async function putOutToSea(settings, ipc) {
31
32
 
32
33
  //eslint-disable-next-line no-extra-boolean-cast
33
34
  if (!!settings.HOOK_SHUTDOWN) {
35
+ logger.trace('Running hook shutdown');
34
36
  disconnectRequired = false;
35
37
  //eslint-disable-next-line no-empty-function
36
38
  sailor.reportError = () => {
@@ -50,26 +52,10 @@ async function putOutToSea(settings, ipc) {
50
52
  }
51
53
 
52
54
  await sailor.runHookInit();
53
- await sailor.run();
54
55
  deferred.resolve();
55
56
  ipc.send('init:ended');
56
- }
57
57
 
58
- async function disconnectAndExit() {
59
- if (!disconnectRequired) {
60
- return;
61
- }
62
- disconnectRequired = false;
63
-
64
- try {
65
- logger.info('Disconnecting...');
66
- await sailor.disconnect();
67
- logger.info('Successfully disconnected');
68
- process.exit();
69
- } catch (err) {
70
- logger.error(err, 'Unable to disconnect');
71
- process.exit(-1);
72
- }
58
+ await sailor.run();
73
59
  }
74
60
 
75
61
  async function gracefulShutdown() {
@@ -82,29 +68,51 @@ async function gracefulShutdown() {
82
68
  return;
83
69
  }
84
70
 
85
- // we connect to amqp, create channels, start listen a queue on init and interrupting this process with 'disconnect'
86
- // will lead to undefined behaviour
71
+ // Wait for init to complete before disconnecting
87
72
  logger.trace('Checking/waiting for init before graceful shutdown');
88
73
  await sailorInit;
89
74
  logger.trace('Waited an init before graceful shutdown');
90
75
 
91
- await sailor.scheduleShutdown();
92
- await disconnectAndExit();
76
+ try {
77
+ logger.info('Scheduling shutdown...');
78
+ await sailor.scheduleShutdown();
79
+ logger.info('Finished shutdown. Disconnecting...');
80
+ await sailor.disconnect();
81
+ logger.info('Successfully disconnected');
82
+ process.exit();
83
+ } catch (err) {
84
+ logger.error(err, 'Unable to disconnect');
85
+ process.exit(-1);
86
+ }
93
87
  }
94
88
 
95
89
  async function run(settings, ipc) {
96
90
  prepareSandbox();
97
91
  try {
98
92
  await putOutToSea(settings, ipc);
99
- logger.info('Fully initialized and waiting for messages');
100
93
  } catch (e) {
101
- if (sailor && !sailor.amqpConnection.closed) {
94
+ if (sailor && !sailor.isConnected()) {
102
95
  await sailor.reportError(e);
103
96
  }
104
97
  logger.criticalErrorAndExit('putOutToSea.catch', e);
105
98
  }
106
99
  }
107
100
 
101
+ function addProcessListeners() {
102
+ process.on('SIGTERM', function onSigterm() {
103
+ logger.info('Received SIGTERM');
104
+ gracefulShutdown();
105
+ });
106
+
107
+ process.on('SIGINT', function onSigint() {
108
+ logger.info('Received SIGINT');
109
+ gracefulShutdown();
110
+ });
111
+
112
+ process.on('uncaughtException', logger.criticalErrorAndExit.bind(logger, 'process.uncaughtException'));
113
+ process.on('unhandledRejection', (err) => logger.error(err, 'process.unhandledRejection'));
114
+ }
115
+
108
116
  exports.__test__ = {
109
117
  disconnectOnly: function disconnectOnly() {
110
118
  if (!disconnectRequired) {
@@ -118,22 +126,10 @@ exports.__test__ = {
118
126
  };
119
127
  exports.run = run;
120
128
  exports.putOutToSea = putOutToSea;
129
+ exports.addProcessListeners = addProcessListeners;
121
130
 
122
131
  if (require.main === module || process.mainModule.filename === __filename) {
123
- process.on('SIGTERM', function onSigterm() {
124
- logger.info('Received SIGTERM');
125
- gracefulShutdown();
126
- });
127
-
128
- process.on('SIGINT', function onSigint() {
129
- logger.info('Received SIGINT');
130
- gracefulShutdown();
131
- });
132
-
133
- process.on('uncaughtException', logger.criticalErrorAndExit.bind(logger, 'process.uncaughtException'));
134
- process.on('unhandledRejection', (err) => logger.error(err, 'process.unhandledRejection'));
135
-
132
+ addProcessListeners();
136
133
  const ipc = new IPC();
137
-
138
134
  run(settings.readFrom(process.env), ipc);
139
135
  }
package/run.local.js ADDED
@@ -0,0 +1,16 @@
1
+ const config = require('./config/local.json');
2
+
3
+ function setEnvVars() {
4
+ for (const [key, value] of Object.entries(config)) {
5
+ process.env[key] = value;
6
+ }
7
+ }
8
+ setEnvVars();
9
+
10
+ const { IPC } = require('./lib/ipc');
11
+ const { addProcessListeners, run } = require('./run');
12
+ const settings = require('./lib/settings.js');
13
+ const ipc = new IPC();
14
+
15
+ addProcessListeners();
16
+ run(settings.readFrom(process.env), ipc);
package/tsconfig.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "compilerOptions": {
3
+ "experimentalDecorators": true,
4
+ "emitDecoratorMetadata": true,
5
+ "lib": [
6
+ "es2020"
7
+ ],
8
+ "types": ["node", "mocha"],
9
+ "module": "commonjs",
10
+ "esModuleInterop": true,
11
+ "target": "es2019",
12
+ "noImplicitAny": false,
13
+ "moduleResolution": "node",
14
+ "resolveJsonModule": true,
15
+ "sourceMap": true,
16
+ "outDir": "dist",
17
+ "skipLibCheck": true,
18
+ "typeRoots": ["./node_modules/@types", "./src/@types"]
19
+ },
20
+ "include": [
21
+ "src/**/*"
22
+ ]
23
+ }