-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexamples-exit-handler.js
More file actions
325 lines (270 loc) · 10.1 KB
/
examples-exit-handler.js
File metadata and controls
325 lines (270 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
/**
* Exit Handler Examples with saveSync()
*
* This file demonstrates how to use saveSync() to ensure data is saved
* before the process exits, even during crashes or interruptions.
*/
const Filejson = require("./app.js");
const fs = require("fs");
const path = require("path");
// Create a test directory
const testDir = path.join(__dirname, "examples-exit-handler");
if (!fs.existsSync(testDir)) {
fs.mkdirSync(testDir, { recursive: true });
}
const testFile = path.join(testDir, "app-state.json");
console.log("=".repeat(60));
console.log("Exit Handler Examples with saveSync()");
console.log("=".repeat(60));
/**
* Example 1: Basic Exit Handler
*
* The most common use case - save data when user presses Ctrl+C
*/
async function example1() {
console.log("\n📝 Example 1: Basic SIGINT Handler (Ctrl+C)");
console.log("-".repeat(60));
const file = new Filejson();
await file.load(testFile, {
appName: "My App",
sessions: 0,
lastRun: null,
});
console.log("✅ App state loaded");
// Register exit handler
const exitHandler = () => {
console.log("\n🛑 Caught interrupt signal (Ctrl+C)");
console.log("💾 Saving application state...");
try {
file.saveSync(); // Synchronous save
console.log("✅ State saved successfully");
} catch (error) {
console.error("❌ Failed to save:", error.message);
}
console.log("👋 Goodbye!\n");
process.exit(0);
};
process.on("SIGINT", exitHandler);
console.log("\n✍️ Simulating application activity...");
file.contents.sessions++;
file.contents.lastRun = new Date().toISOString();
console.log(` Sessions: ${file.contents.sessions}`);
console.log(` Last run: ${file.contents.lastRun}`);
console.log("\n💡 Press Ctrl+C to trigger exit handler and see saveSync() in action");
console.log(" Or wait 5 seconds to continue to next example...\n");
// Wait 5 seconds then cleanup handler
await new Promise((resolve) => setTimeout(resolve, 5000));
process.removeListener("SIGINT", exitHandler);
console.log("✅ Exit handler removed (continuing to next example)");
}
/**
* Example 2: Multiple Exit Signals
*
* Handle different types of termination signals
*/
async function example2() {
console.log("\n📝 Example 2: Handling Multiple Exit Signals");
console.log("-".repeat(60));
const file = new Filejson();
await file.load(testFile, {
data: "important",
counter: 0,
});
console.log("✅ File loaded with multiple exit handlers");
// Shared exit handler function
const gracefulShutdown = (signal) => {
console.log(`\n🛑 Received ${signal} signal`);
console.log("💾 Performing graceful shutdown...");
try {
// Update shutdown info
file.contents.lastShutdown = {
signal: signal,
timestamp: new Date().toISOString(),
};
file.saveSync();
console.log("✅ Data saved successfully");
process.exit(0);
} catch (error) {
console.error("❌ Save failed:", error.message);
process.exit(1);
}
};
// Register multiple signals
const signals = ["SIGINT", "SIGTERM", "SIGHUP"];
signals.forEach((signal) => {
process.on(signal, () => gracefulShutdown(signal));
});
console.log(" Registered handlers for:", signals.join(", "));
// Make some changes
file.contents.counter++;
console.log(` Counter updated: ${file.contents.counter}`);
// Cleanup for next example
await new Promise((resolve) => setTimeout(resolve, 1000));
signals.forEach((signal) => process.removeAllListeners(signal));
console.log("✅ Exit handlers removed");
}
/**
* Example 3: saveSync vs save comparison
*
* Demonstrates why saveSync is necessary for exit handlers
*/
async function example3() {
console.log("\n📝 Example 3: Why saveSync() Is Necessary");
console.log("-".repeat(60));
console.log("\n⚠️ Problem with async save() in exit handlers:");
console.log(" ```javascript");
console.log(" process.on('SIGINT', async () => {");
console.log(" await file.save(); // ❌ May not complete!");
console.log(" process.exit(0);");
console.log(" });");
console.log(" ```");
console.log(" The process exits before async save() completes!");
console.log("\n✅ Solution with saveSync():");
console.log(" ```javascript");
console.log(" process.on('SIGINT', () => {");
console.log(" file.saveSync(); // ✅ Blocks until saved");
console.log(" process.exit(0);");
console.log(" });");
console.log(" ```");
console.log(" Synchronous operation completes before exit!");
console.log("\n📊 Comparison:");
console.log(" ┌────────────────────┬─────────┬──────────────┐");
console.log(" │ Method │ Async │ Exit Handler │");
console.log(" ├────────────────────┼─────────┼──────────────┤");
console.log(" │ save() │ Yes │ ❌ Unreliable│");
console.log(" │ saveSync() │ No │ ✅ Reliable │");
console.log(" └────────────────────┴─────────┴──────────────┘");
}
/**
* Example 4: Production Pattern
*
* Complete production-ready exit handler with error handling
*/
async function example4() {
console.log("\n📝 Example 4: Production-Ready Exit Handler");
console.log("-".repeat(60));
const file = new Filejson({
atomicWrites: true, // Crash-safe writes
saveDelay: 100, // Debounce for performance
});
await file.load(testFile, {
environment: "production",
stats: {
requests: 0,
errors: 0,
uptime: 0,
},
lastSave: null,
});
console.log("✅ Production app initialized");
// Track if we're already shutting down
let isShuttingDown = false;
const shutdown = (signal, exitCode = 0) => {
if (isShuttingDown) {
console.log("⚠️ Already shutting down...");
return;
}
isShuttingDown = true;
console.log(`\n🛑 Shutdown initiated (${signal})`);
try {
// Update final stats
file.contents.stats.uptime = process.uptime();
file.contents.lastSave = new Date().toISOString();
console.log("💾 Saving final state...");
const startTime = Date.now();
file.saveSync();
const duration = Date.now() - startTime;
console.log(`✅ State saved in ${duration}ms`);
console.log(` Requests: ${file.contents.stats.requests}`);
console.log(` Errors: ${file.contents.stats.errors}`);
console.log(` Uptime: ${file.contents.stats.uptime.toFixed(2)}s`);
console.log("👋 Shutdown complete\n");
process.exit(exitCode);
} catch (error) {
console.error("❌ CRITICAL: Failed to save state on exit");
console.error(" Error:", error.message);
process.exit(1);
}
};
// Register handlers
process.on("SIGINT", () => shutdown("SIGINT", 0));
process.on("SIGTERM", () => shutdown("SIGTERM", 0));
// Simulate app activity
console.log("\n✍️ Simulating production activity...");
for (let i = 0; i < 5; i++) {
file.contents.stats.requests += Math.floor(Math.random() * 100);
file.contents.stats.errors += Math.floor(Math.random() * 3);
await new Promise((resolve) => setTimeout(resolve, 200));
}
console.log(` Total requests: ${file.contents.stats.requests}`);
console.log(` Total errors: ${file.contents.stats.errors}`);
console.log("\n✅ Production pattern demonstrated");
// Cleanup
await new Promise((resolve) => setTimeout(resolve, 1000));
process.removeAllListeners("SIGINT");
process.removeAllListeners("SIGTERM");
}
/**
* Example 5: Atomic Writes + saveSync
*
* Demonstrating crash safety even in exit handlers
*/
async function example5() {
console.log("\n📝 Example 5: Crash Safety with Atomic Writes");
console.log("-".repeat(60));
console.log("\n🔒 With atomicWrites: true (default):");
console.log(" Even in exit handlers, saves are atomic:");
console.log(" 1. Write to temp file");
console.log(" 2. Rename atomically");
console.log(" 3. No partial writes possible");
const file = new Filejson({ atomicWrites: true });
await file.load(testFile, { critical: "data" });
console.log("\n✅ Benefits in exit handlers:");
console.log(" • If saveSync() fails partway, original file intact");
console.log(" • No corrupted JSON files");
console.log(" • Production-grade reliability");
file.contents.critical = "updated data";
file.saveSync();
console.log("\n✅ Data saved atomically with saveSync()");
}
/**
* Run all examples
*/
async function runAllExamples() {
try {
await example1();
await example2();
await example3();
await example4();
await example5();
console.log("\n" + "=".repeat(60));
console.log("✅ All examples completed successfully!");
console.log("=".repeat(60));
console.log("\n💡 Key Takeaways:");
console.log(" • Use saveSync() in ALL exit handlers");
console.log(" • Async save() won't complete before process exit");
console.log(" • saveSync() blocks until write completes");
console.log(" • Works with atomic writes for crash safety");
console.log(" • Essential for production applications");
console.log(" • Always wrap in try-catch for error handling");
console.log("\n📚 Best Practice Pattern:");
console.log(" ```javascript");
console.log(" process.on('SIGINT', () => {");
console.log(" try {");
console.log(" file.saveSync();");
console.log(" process.exit(0);");
console.log(" } catch (error) {");
console.log(" console.error(error);");
console.log(" process.exit(1);");
console.log(" }");
console.log(" });");
console.log(" ```\n");
} catch (error) {
console.error("❌ Error running examples:", error);
}
}
// Run examples if this file is executed directly
if (require.main === module) {
runAllExamples();
}
module.exports = { runAllExamples };