|
| 1 | +import process from 'node:process' |
1 | 2 | import { find, isEmpty, difference, flatten } from 'lodash-es'
|
2 | 3 | import got from 'got'
|
3 | 4 | import delay from 'delay'
|
4 | 5 | import merge from 'deepmerge'
|
5 | 6 | import { Enums } from 'atem-connection'
|
6 | 7 |
|
7 |
| -import beforeShutdown from './beforeShutdown.js' |
| 8 | +import exitHook from 'exit-hook' |
| 9 | +// import beforeShutdown from './beforeShutdown.js' |
8 | 10 | import { ControllerWebServer } from './ControllerWebServer.js'
|
9 | 11 | import { ControllerAtem } from './ControllerAtem.js'
|
10 | 12 | import { ControllerMidi } from './ControllerMidi.js'
|
@@ -36,19 +38,16 @@ export class ControlThemAll {
|
36 | 38 |
|
37 | 39 | async setup() {
|
38 | 40 | console.log('Setting up ControlThemAll Backend')
|
39 |
| - // // Do something when app is closing |
40 |
| - // process.on('exit', this.exitHandler.bind(this, { exit: true })) |
41 |
| - // // catches ctrl+c event |
42 |
| - // process.on('SIGINT', this.exitHandler.bind(this, {cleanup: true})); |
43 |
| - // // Catches "kill pid" (for example: nodemon restart) |
44 |
| - // process.on('SIGUSR1', this.exitHandler.bind(this, {exit: true})); |
45 |
| - // process.on('SIGUSR2', this.exitHandler.bind(this, {exit: true, cleanup: true})); |
46 |
| - // // Catches uncaught exceptions |
47 |
| - // process.on('uncaughtException', this.exitHandler.bind(this, {exit: true})); |
48 |
| - // process.on('uncaughtException', (reason) => console.log('UNHANDLED EXCEPTION:', reason)) |
49 |
| - // process.on('unhandledRejection', reason => console.log('UNHANDLED REJECTION:', reason)); |
50 |
| - |
51 |
| - beforeShutdown(this.exitHandler.bind(this, { cleanup: true, exit: true })) |
| 41 | + |
| 42 | + process.on('exit', (t) => console.log('process.on : exit:', t)) |
| 43 | + process.on('SIGINT', (t) => console.log('process.on : SIGINT:', t)) |
| 44 | + process.on('SIGUSR1', (t) => console.log('process.on : SIGUSR1:', t)) |
| 45 | + process.on('SIGUSR2', (t) => console.log('process.on : SIGUSR2:', t)) |
| 46 | + process.on('uncaughtException', (t) => console.log('process.on : uncaughtException:', t)) |
| 47 | + process.on('uncaughtException', (t) => console.log('process.on : uncaughtException:', t)) |
| 48 | + process.on('unhandledRejection', (t) => console.log('process.on : unhandledRejection:', t)) |
| 49 | + |
| 50 | + exitHook(this.exitHandler.bind(this)) |
52 | 51 |
|
53 | 52 | this.controllerConfig = new ControllerConfig()
|
54 | 53 | this.config = await this.controllerConfig.getConfig()
|
@@ -1092,57 +1091,43 @@ export class ControlThemAll {
|
1092 | 1091 | }
|
1093 | 1092 | }
|
1094 | 1093 |
|
1095 |
| - async exitHandler(options, exitCode) { |
1096 |
| - console.log(`exitHandler with exitCode: ${exitCode || 'NONE'}`) |
1097 |
| - |
1098 |
| - // SetTimeout(error => { |
1099 |
| - // // Force close server after timeout (this is if the cleanUp takes too long) |
1100 |
| - // console.log('gentle took too long exiting hard'); |
1101 |
| - // process.exit(1); |
1102 |
| - // }, 10 * 1000); // 10 seconds |
| 1094 | + exitHandler() { |
| 1095 | + console.log(`running exitHandler for cleanup`) |
1103 | 1096 |
|
1104 |
| - if (options.cleanup) { |
1105 |
| - console.log('ControlThemAll: Doing the cleanup.') |
1106 |
| - |
1107 |
| - if (this.controllerMidi && this.controllerMidi.isConnected()) { |
1108 |
| - this.switchButtonLightOff(flatten(this.config.buttons.map((element) => [element.note]))) |
1109 |
| - this.controllerMidi.updateButtonsViaState(this.config.buttons) |
1110 |
| - this.updateControllerState(this.config.controllers.map((element) => merge(element, { state: 'cc', value: 0 }))) |
1111 |
| - this.controllerMidi.updateControllersViaState(this.config.controllers) |
1112 |
| - } |
1113 |
| - |
1114 |
| - if (this.controllerWebServer) { |
1115 |
| - console.log('before web server disconnect') |
1116 |
| - console.log(await this.controllerWebServer.disconnect()) |
1117 |
| - console.log('after web server disconnect') |
1118 |
| - } |
1119 |
| - |
1120 |
| - if (this.controllerMidi) { |
1121 |
| - console.log('before midi disconnect') |
1122 |
| - console.log(await this.controllerMidi.disconnect()) |
1123 |
| - console.log('after midi disconnect') |
1124 |
| - } |
| 1097 | + if (this.controllerMidi && this.controllerMidi.isConnected()) { |
| 1098 | + this.switchButtonLightOff(flatten(this.config.buttons.map((element) => [element.note]))) |
| 1099 | + this.controllerMidi.updateButtonsViaState(this.config.buttons) |
| 1100 | + this.updateControllerState(this.config.controllers.map((element) => merge(element, { state: 'cc', value: 0 }))) |
| 1101 | + this.controllerMidi.updateControllersViaState(this.config.controllers) |
| 1102 | + } |
1125 | 1103 |
|
1126 |
| - if (this.controllerStreamDeck) { |
1127 |
| - console.log('before stream deck disconnect') |
1128 |
| - console.log(await this.controllerStreamDeck.disconnect()) |
1129 |
| - console.log('after stream deck disconnect') |
1130 |
| - } |
| 1104 | + if (this.controllerWebServer) { |
| 1105 | + console.log('before web server disconnect') |
| 1106 | + this.controllerWebServer.disconnect() |
| 1107 | + delete this.controllerWebServer |
| 1108 | + console.log('after web server disconnect') |
| 1109 | + } |
1131 | 1110 |
|
1132 |
| - if (this.controllerAtem) { |
1133 |
| - console.log('before atem disconnect') |
1134 |
| - console.log(await this.controllerAtem.disconnect()) |
1135 |
| - console.log('after atem disconnect') |
1136 |
| - } |
1137 |
| - // Await this.controllerHotkeys.disconnect() |
| 1111 | + if (this.controllerMidi) { |
| 1112 | + console.log('before midi disconnect') |
| 1113 | + this.controllerMidi.disconnect() |
| 1114 | + delete this.controllerMidi |
| 1115 | + console.log('after midi disconnect') |
| 1116 | + } |
1138 | 1117 |
|
1139 |
| - console.log('ControlThemAll closed, cleaned, and shutting down!') |
| 1118 | + if (this.controllerAtem) { |
| 1119 | + console.log('before atem disconnect') |
| 1120 | + this.controllerAtem.disconnect() |
| 1121 | + delete this.controllerAtem |
| 1122 | + console.log('after atem disconnect') |
1140 | 1123 | }
|
1141 | 1124 |
|
1142 |
| - // if (options.exit) { |
1143 |
| - // console.log('ControlThemAll closed all connections successfully… shuting down…') |
1144 |
| - // process.exit() |
1145 |
| - // } |
| 1125 | + if (this.controllerStreamDeck) { |
| 1126 | + console.log('before stream deck disconnect') |
| 1127 | + this.controllerStreamDeck.disconnect() |
| 1128 | + delete this.controllerStreamDeck |
| 1129 | + console.log('after stream deck disconnect') |
| 1130 | + } |
1146 | 1131 | }
|
1147 | 1132 | }
|
1148 | 1133 |
|
|
0 commit comments