-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
397 lines (274 loc) · 10.1 KB
/
Copy pathindex.js
File metadata and controls
397 lines (274 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// ------------------------------------ PART 1 -----------------------------------
console.log("start");
process.on("exit", (code) => {
console.log("exit called with code: ",code)
});
console.log("End");
// Output:
// start
// End
// exit called with code: 0
// Process simply is the code which runs in the OS
// Process in Node js is the program in Node js which runs in the OS
// Process is the instance of Events in Node js so you can listen to all the events (even error ones) [-- PART 2]
// Process is a global object
// process.exit is run by Event Loop when the call stack is empty and even all the event loop queue is empty, but there's a catch
// process.exit can also be called on its own when there's an error event like uncaughtException
// exit returns with an exit code which tells the status of the how the process ended e.g. 0 means a success while 1 means a fatal error, there are many
// you can change the exit codes
// while listening to process.exit you can't run any async function, as exit is synchronous [--PART 2]
// ------------------------------------ PART 2 -----------------------------------
console.log("start");
setTimeout(() => {
console.log("after 5 sec")
},5000);
process.on("exit", (code) => {
console.log("exit called with code: ",code)
});
console.log("End");
// Output:
// start
// End
// after 5 sec
// exit called with code: 0
// process.exit is run by Event Loop when the call stack is empty and even all the event loop queue
// ** but **
console.log("start");
process.on("exit", (code) => {
console.log("exit called with code: ",code);
setTimeout(() => {
console.log("after 5 sec")
},5000);
});
console.log("End");
// Output:
// start
// End
// exit called with code: 0
// exit will not run any async function inside exit listener as it ends synchronously
// if you want to do some async operation e.g. log error to another server and then exit, this can't be done in exit listener
// ------------------------------------ PART 3 -----------------------------------
console.log("start");
process.on("exit", (code) => {
console.log("exit called with code: ",code);
});
process.on("beforeExit",() => {
console.log("before exit called");
})
console.log("End");
// Output:
// start
// End
// before exit called
// beforeExit is another event the process can listen and this is run by event loop once the call stack is empty
// then the exit is called
// best part is you can run async function inside beforeExit listener
console.log("start");
process.on("exit", (code) => {
console.log("exit called with code: ",code);
});
process.on("beforeExit",() => {
console.log("before exit called");
setTimeout(() => {
console.log("after 5 sec");
},5000);
})
console.log("End");
// Output:
// start
// End
// before exit called
// after 5 sec
// before exit called
// after 5 sec
// before exit called
// and so on
// async operation is achieved inside beforeExit but there's a catch,
// in the output the setTimeout is called again and again and goes in an infinite loop
// because every time the event loop checks for call stack and triggers beforeExit, then we run an async function
// when the async function is resolved, event loop will check for call stack and queues and triggers beforeExit if empty, then we run an async function
// and it goes on infinitely
// so call process.exit(0) inside async function of beforeExit listener
console.log("start");
process.on("exit", (code) => {
console.log("exit called with code: ",code);
});
process.on("beforeExit",() => {
console.log("before exit called");
setTimeout(() => {
console.log("after 5 sec");
process.exit(0);
},5000);
})
console.log("End");
// Output:
// start
// End
// before exit called
// after 5 sec
// exit called with code: 0
// Now you have it
// but again there's a catch
// you can't listen to beforeExit if process.exit() is called explicitly or uncaught error event is triggered
console.log("start");
process.on("exit", (code) => {
console.log("exit called with code: ",code);
});
process.on("beforeExit",() => {
console.log("before exit called");
setTimeout(() => {
console.log("after 5 sec");
},5000);
})
console.log("End");
process.exit();
// Output:
// start
// End
// exit called with code: 0
// as mentioned beforeExit is not triggered in this case
// ------------------------------------ PART 4 -----------------------------------
console.log("start");
process.on("exit", (code) => {
console.log("exit called with code: ",code);
});
const obj = {};
obj.something.toString();
console.log("End");
// Output:
// start
// exit called with code: 1
// /Users/halfcute/Desktop/backend/index.js:11
// obj.something.toString();
// ^
// TypeError: Cannot read property 'toString' of undefined
// at Object.<anonymous> (/Users/halfcute/Desktop/backend/index.js:11:15)
// at Module._compile (internal/modules/cjs/loader.js:1085:14)
// at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
// at Module.load (internal/modules/cjs/loader.js:950:32)
// at Function.Module._load (internal/modules/cjs/loader.js:790:12)
// at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
// at internal/main/run_main_module.js:17:47
// let's talk about another event called uncaughtException which is triggered here
// you can listen to uncaughtException, also uncaughtException calls process.exit() internally as you can see
console.log("start");
process.on("exit", (code) => {
console.log("exit called with code: ",code);
});
process.on("uncaughtException",(err) => {
console.log("error encountered");
console.log(err.stack);
setTimeout(() => {
console.log("after 5 sec");
},5000);
})
const obj = {};
obj.something.toString();
console.log("End");
// Output:
// start
// error encountered
// TypeError: Cannot read property 'toString' of undefined
// at Object.<anonymous> (/Users/halfcute/Desktop/backend/index.js:17:15)
// at Module._compile (internal/modules/cjs/loader.js:1085:14)
// at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
// at Module.load (internal/modules/cjs/loader.js:950:32)
// at Function.Module._load (internal/modules/cjs/loader.js:790:12)
// at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
// at internal/main/run_main_module.js:17:47
// after 5 sec
// exit called with code: 0
// uncaughtException can run async function as well
// there is one issue in the above code, the exit code which we are recieveing is 0 meaaning success, by default, but since we are listening to an error event
// we should send 1 as exitcode meaning fatal error
console.log("start");
process.on("exit", (code) => {
console.log("exit called with code: ",code);
});
process.on("uncaughtException",(err) => {
console.log("error encountered");
console.log(err.stack);
process.exitCode = 1;
})
const obj = {};
obj.something.toString();
console.log("End");
// Output:
// start
// error encountered
// TypeError: Cannot read property 'toString' of undefined
// at Object.<anonymous> (/Users/halfcute/Desktop/backend/index.js:17:15)
// at Module._compile (internal/modules/cjs/loader.js:1085:14)
// at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
// at Module.load (internal/modules/cjs/loader.js:950:32)
// at Function.Module._load (internal/modules/cjs/loader.js:790:12)
// at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
// at internal/main/run_main_module.js:17:47
// exit called with code: 1
// ------------------------------------ PART 5 -----------------------------------
// Let's talk about Signals
// Signals are recieved by Node js as event
// Two signals which you should know about is SIGINT and SIGTERM
// let's dive into SIGINT
console.log("start");
console.log(process.pid); // the id of the current process
setTimeout(() => {
console.log("after 10 sec");
}, 10000);
process.on("SIGINT",() => {
console.log("SIGINT recieved");
})
console.log("End");
// Output:
// start
// 1671
// End
// ^CSIGINT recieved
// after 10 sec
// SIGINT is emitted when you end the server, here pressing ctrl+c in the terminal to exit the server, but it will wait for all async operation to complete that are in event loop
// and will then end the process
// you can also run async operation inside SIGINT listener, for e.g. you can log the error to another server and exit
console.log("start");
console.log(process.pid);
setTimeout(() => {
console.log("after 10 sec");
}, 10000);
console.log("End");
// Output:
// start
// 1922
// End
// Terminated: 15
// SIGTERM is another event which is emitted when you kill the process in the terminal by saying, kill <process.pid>
// we are not listening to SIGTERM event above and upon killing the process the entire Node Js process exits
// Here's a very important point from Node JS official DOCS
// 'SIGTERM' and 'SIGINT' have default handlers on non-Windows platforms that reset the terminal mode before exiting with code
// 128 + signal number. If one of these signals has a listener installed, its default behavior will be removed (Node.js will no longer exit).
// This means that once you have a listener in your process for SIGTERM it won't terminate the process abruptly
// but will wait for call stack to be empty and will then exit
// You can also have have async opeartion inside SIGTERM listener
console.log("start");
console.log(process.pid);
setTimeout(() => {
console.log("after 10 sec");
}, 10000);
process.on("SIGTERM",() => {
console.log("SIGTERM recieved");
setTimeout(() => {
console.log("after 20 sec");
}, 20000);
})
console.log("End");
// Output:
// start
// 1985
// End
// SIGTERM recieved
// after 10 sec
// after 20 sec
// on having a SIGTERM listener now we have altered it's default behaviour
// So if you have not provided any listener to SIGINT or SIGTERM it will
// 1. end process
// 2. will not care for event loop, even if any task is remaining
// 3. therefore will not call exit, even if you have an exit listener
// It's a good practise to have listener for SIGINT and SIGTERM