|
| 1 | +import fs from 'node:fs'; |
| 2 | +import os from 'node:os'; |
| 3 | + |
| 4 | + |
| 5 | +//Constants |
| 6 | +const signalTestTarget = os.platform() === 'win32' |
| 7 | + ? 'E:/TMP/signal-test-windows.txt' |
| 8 | + : '/root/server/signal-test-docker.txt'; |
| 9 | + |
| 10 | +const writeStdout = (message: string) => { |
| 11 | + process.stdout.write(message + '\n'); |
| 12 | +} |
| 13 | +const writePadding = () => { |
| 14 | + process.stdout.write('.\n'.repeat(10)); |
| 15 | +} |
| 16 | + |
| 17 | + |
| 18 | +//Proof of life |
| 19 | +console.log('NodeJS:', process.version); |
| 20 | +writeStdout('Starting...'); |
| 21 | +let counter = 0; |
| 22 | +let isAwaitingShutdown = false; |
| 23 | +setInterval(() => { |
| 24 | + counter++; |
| 25 | + const icon = isAwaitingShutdown ? '⏳' : '✅'; |
| 26 | + writeStdout(icon + ' tick: ' + counter); |
| 27 | +}, 250); |
| 28 | + |
| 29 | + |
| 30 | +//Signal management |
| 31 | +// const listeners: NodeJS.SignalsListener[] = []; |
| 32 | +// const addSignalListener = (signal: string, listener: NodeJS.SignalsListener) => { |
| 33 | +// process.on(signal, listener); |
| 34 | +// listeners.push(listener); |
| 35 | +// } |
| 36 | +// const removeSignalListeners = () => { |
| 37 | +// for (const listener of listeners) { |
| 38 | +// process.removeListener('SIGINT', listener); |
| 39 | +// process.removeListener('SIGTERM', listener); |
| 40 | +// process.removeListener('SIGHUP', listener); |
| 41 | +// } |
| 42 | +// } |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +//Graceful shutdown |
| 47 | +const immediateButGracefulShutdown: NodeJS.SignalsListener = (signal: string) => { |
| 48 | + if (isAwaitingShutdown) { |
| 49 | + writeStdout(`\n⚠️ got '${signal}' but already shutting down.\n`); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + isAwaitingShutdown = true; |
| 54 | + const randId = Math.random().toString(36).substring(2, 15); |
| 55 | + const msg = [ |
| 56 | + '-'.repeat(20), |
| 57 | + 'Signal received: ' + signal, |
| 58 | + 'Time: ' + new Date().toISOString(), |
| 59 | + 'Rand: ' + randId, |
| 60 | + 'Counter: ' + counter, |
| 61 | + '-'.repeat(20), |
| 62 | + ].join('\n'); |
| 63 | + writeStdout(msg); |
| 64 | + |
| 65 | + //Trying to write a file |
| 66 | + try { |
| 67 | + fs.writeFileSync(signalTestTarget, msg + '\n'); |
| 68 | + } catch (error) { |
| 69 | + writeStdout('Error writing file: ' + (error as any).message); |
| 70 | + } |
| 71 | + |
| 72 | + //Delaying the shutdown |
| 73 | + writeStdout('Delaying...'); |
| 74 | + setTimeout(() => { |
| 75 | + writeStdout('time to die'); |
| 76 | + //Trying to write a file |
| 77 | + try { |
| 78 | + fs.appendFileSync(signalTestTarget, `Dead: ${randId}\n`); |
| 79 | + } catch (error) { |
| 80 | + writeStdout('Error writing file: ' + (error as any).message); |
| 81 | + } |
| 82 | + // process.removeAllListeners(signal); |
| 83 | + // writeStdout('sending SIGKILL.'); |
| 84 | + // process.kill(process.pid, 'SIGKILL'); |
| 85 | + writePadding(); |
| 86 | + console.log('count', process.listenerCount('SIGINT')); |
| 87 | + process.removeListener('SIGINT', immediateButGracefulShutdown); |
| 88 | + process.removeListener('SIGTERM', immediateButGracefulShutdown); |
| 89 | + process.removeListener('SIGHUP', immediateButGracefulShutdown); |
| 90 | + console.log('count', process.listenerCount('SIGINT')); |
| 91 | + process.exit(0); |
| 92 | + }, 1500); |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +//Listening to signals |
| 99 | +try { |
| 100 | + process.on('SIGINT', immediateButGracefulShutdown); |
| 101 | + process.on('SIGTERM', immediateButGracefulShutdown); |
| 102 | + process.on('SIGHUP', immediateButGracefulShutdown); |
| 103 | +} catch (error) { |
| 104 | + writeStdout('Error listening to signals: ' + (error as any).message); |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +// setTimeout(() => { |
| 110 | +// console.log('dying'); |
| 111 | +// process.exit(0); |
| 112 | +// }, 5000); |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +// //Setting up command handler |
| 118 | +// process.stdin.on('data', (data) => { |
| 119 | +// const cmd = data.toString().toLowerCase().trim(); |
| 120 | +// if (data.toString() === '\n') { |
| 121 | +// console.log('='.repeat(20)); |
| 122 | +// } else if (cmd === 'k') { |
| 123 | +// writeStdout('killed!'); |
| 124 | +// process.exit(0); |
| 125 | +// } else if (cmd === 'c') { |
| 126 | +// throw new Error('crashed!'); |
| 127 | +// } else if (cmd === 't') { |
| 128 | +// process.emit('SIGKILL', 'SIGKILL'); |
| 129 | +// } |
| 130 | +// }); |
| 131 | +// process.stdin.resume(); |
| 132 | + |
| 133 | +//yay |
| 134 | +writeStdout('All ready: ' + process.pid); |
| 135 | + |
| 136 | +// console.dir(os.networkInterfaces()); |
| 137 | +// console.dir(os.userInfo()); |
| 138 | + |
| 139 | +// console.log('Starting...x'); |
| 140 | +// setInterval(() => { |
| 141 | +// console.log('Alive'); |
| 142 | +// }, 250); |
| 143 | +// setTimeout(() => { |
| 144 | +// console.log('Exiting...'); |
| 145 | +// process.exitCode = 3; |
| 146 | +// }, 2000); |
| 147 | +// setImmediate(() => { |
| 148 | +// console.log('Running...'); |
| 149 | +// }); |
| 150 | + |
| 151 | +// process.on('beforeExit', (code) => { |
| 152 | +// console.log('beforeExit', code); |
| 153 | +// }); |
| 154 | + |
| 155 | +/* |
| 156 | +
|
| 157 | +ctrl+c -> SIGINT |
| 158 | +taskkill -f -pid 122636 -> nothing |
| 159 | +closing window -> SIGHUP |
| 160 | +
|
| 161 | +
|
| 162 | +
|
| 163 | +
|
| 164 | +./scripts/test_build.sh $TXDEV_FXSERVER_PATH |
| 165 | +
|
| 166 | +
|
| 167 | +
|
| 168 | +node alpine/opt/cfx-server/citizen/system_resources/monitor/core/index.js |
| 169 | +
|
| 170 | +
|
| 171 | +
|
| 172 | +TARGET_PATH=/c/Users/tabarra/Desktop/PROGRAMMING/fxserver-container/server/alpine/opt/cfx-server/citizen/system_resources/monitor |
| 173 | +rm -rf "${TARGET_PATH}/core" |
| 174 | +mkdir -p $TARGET_PATH |
| 175 | +cp -r ../dist/core $TARGET_PATH |
| 176 | +ls -la "${TARGET_PATH}/core" |
| 177 | +*/ |
0 commit comments