-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·61 lines (47 loc) · 1.75 KB
/
server.js
File metadata and controls
executable file
·61 lines (47 loc) · 1.75 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
#!/usr/bin/env node
import fs from 'fs';
import Branch from 'branch';
const services = new Branch('services');
const main = new Branch('main');
main.onStart = async () => console.log('ASYNC START GRRR')
main.once('stop', ()=>console.log('Main scene node got stoppppp...'))
services.watch('create', '/services/main/*', (x)=>console.info('[CREATE] new node in main scene', x))
const uppercase = new Branch('uppercase');
const tee = new Branch('tee');
services.create(main);
services.create(uppercase);
services.create(tee);
const mainInput = new Branch('mainInput');
main.create(mainInput)
await services.load();
await services.start();
setTimeout(() => { },3_000) // TEST CTRL-C
// Example of cleanup task
const cleanup = () => {
console.log('Cleaning up resources...');
services.stop();
fs.appendFileSync('shutdown.log', `Shutting down at ${new Date().toISOString()}\n`);
};
// Listen for the exit signals
process.on('SIGINT', () => {
console.log('Received SIGINT (Ctrl+C). Graceful shutdown...');
cleanup();
process.exit(0); // Exit the process after cleanup
});
process.on('SIGTERM', () => {
console.log('Received SIGTERM. Graceful shutdown...');
cleanup();
process.exit(0); // Exit the process after cleanup
});
// Handle uncaught exceptions gracefully
process.on('uncaughtException', (err) => {
console.error('Unhandled exception:', err);
cleanup();
process.exit(1); // Exit with a non-zero status on uncaught exceptions
});
// Handle unhandled promise rejections (important for async tasks)
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled rejection at', promise, 'reason:', reason);
cleanup();
process.exit(1); // Exit with a non-zero status on unhandled promise rejections
});