@nxtedition/lib 17.0.0 → 17.1.0

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.
Files changed (3) hide show
  1. package/app.js +12 -2
  2. package/package.json +2 -1
  3. package/timers.js +92 -0
package/app.js CHANGED
@@ -118,6 +118,10 @@ export function makeApp(appConfig, onTerminate) {
118
118
  {
119
119
  const loggerConfig = { ...appConfig.logger, ...config.logger }
120
120
 
121
+ process.on('uncaughtExceptionMonitor', (err) => {
122
+ logger.fatal({ err }, 'uncaught exception')
123
+ })
124
+
121
125
  logger = createLogger({
122
126
  ...loggerConfig,
123
127
  name: serviceName,
@@ -141,6 +145,8 @@ export function makeApp(appConfig, onTerminate) {
141
145
 
142
146
  terminated = true
143
147
 
148
+ logger.info('terminate')
149
+
144
150
  ac.abort()
145
151
 
146
152
  if (onTerminate) {
@@ -160,11 +166,15 @@ export function makeApp(appConfig, onTerminate) {
160
166
  }
161
167
 
162
168
  setTimeout(() => {
163
- process.exit(0)
169
+ process.abort()
164
170
  }, 10e3).unref()
165
171
  }
166
172
 
167
- process.on('beforeExit', terminate).on('SIGINT', terminate).on('SIGTERM', terminate)
173
+ process
174
+ .on('beforeExit', terminate)
175
+ .on('SIGINT', terminate)
176
+ .on('SIGTERM', terminate)
177
+ .on('uncaughtExceptionMonitor', terminate)
168
178
 
169
179
  logger.debug({ data: JSON.stringify(config, null, 2) }, 'config')
170
180
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "17.0.0",
3
+ "version": "17.1.0",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
@@ -20,6 +20,7 @@
20
20
  "logger.js",
21
21
  "mime.js",
22
22
  "proxy.js",
23
+ "timers.js",
23
24
  "trace.js",
24
25
  "weakCache.js",
25
26
  "couch.js",
package/timers.js ADDED
@@ -0,0 +1,92 @@
1
+ let fastNow = Date.now()
2
+ let fastNowTimeout
3
+
4
+ const fastTimers = []
5
+
6
+ function onTimeout() {
7
+ fastNow = Date.now()
8
+
9
+ let len = fastTimers.length
10
+ let idx = 0
11
+ while (idx < len) {
12
+ const timer = fastTimers[idx]
13
+
14
+ if (timer.state === 0) {
15
+ timer.state = fastNow + timer.delay
16
+ } else if (timer.state > 0 && fastNow >= timer.state) {
17
+ timer.state = -1
18
+ timer.callback(timer.opaque)
19
+ }
20
+
21
+ if (timer.state === -1) {
22
+ timer.state = -2
23
+ if (idx !== len - 1) {
24
+ fastTimers[idx] = fastTimers.pop()
25
+ } else {
26
+ fastTimers.pop()
27
+ }
28
+ len -= 1
29
+ } else {
30
+ idx += 1
31
+ }
32
+ }
33
+
34
+ if (fastTimers.length > 0) {
35
+ refreshTimeout()
36
+ }
37
+ }
38
+
39
+ function refreshTimeout() {
40
+ if (fastNowTimeout && fastNowTimeout.refresh) {
41
+ fastNowTimeout.refresh()
42
+ } else {
43
+ clearTimeout(fastNowTimeout)
44
+ fastNowTimeout = setTimeout(onTimeout, 1e3)
45
+ if (fastNowTimeout.unref) {
46
+ fastNowTimeout.unref()
47
+ }
48
+ }
49
+ }
50
+
51
+ class Timeout {
52
+ constructor(callback, delay, opaque) {
53
+ this.callback = callback
54
+ this.delay = delay
55
+ this.opaque = opaque
56
+
57
+ // -2 not in timer list
58
+ // -1 in timer list but inactive
59
+ // 0 in timer list waiting for time
60
+ // > 0 in timer list waiting for time to expire
61
+ this.state = -2
62
+
63
+ this.refresh()
64
+ }
65
+
66
+ refresh() {
67
+ if (this.state === -2) {
68
+ fastTimers.push(this)
69
+ if (!fastNowTimeout || fastTimers.length === 1) {
70
+ refreshTimeout()
71
+ }
72
+ }
73
+
74
+ this.state = 0
75
+ }
76
+
77
+ clear() {
78
+ this.state = -1
79
+ }
80
+ }
81
+
82
+ export function setTimeout(callback, delay, opaque) {
83
+ return delay < 1e3 ? setTimeout(callback, delay, opaque) : new Timeout(callback, delay, opaque)
84
+ }
85
+
86
+ export function clearTimeout(timeout) {
87
+ if (timeout instanceof Timeout) {
88
+ timeout.clear()
89
+ } else {
90
+ clearTimeout(timeout)
91
+ }
92
+ }