|
@@ -218,10 +218,38 @@ app.post('/settings', (req, res) => {
|
|
|
|
|
|
|
|
cleanupLogs();
|
|
cleanupLogs();
|
|
|
mqttService.connect();
|
|
mqttService.connect();
|
|
|
-startScheduler(mqttService);
|
|
|
|
|
|
|
+const schedulerTask = startScheduler(mqttService);
|
|
|
|
|
|
|
|
const config = getConfig();
|
|
const config = getConfig();
|
|
|
const port = Number(process.env.PORT || config.server_port || 3000);
|
|
const port = Number(process.env.PORT || config.server_port || 3000);
|
|
|
-app.listen(port, () => {
|
|
|
|
|
|
|
+const server = app.listen(port, () => {
|
|
|
console.log(`Office Light running at http://localhost:${port}`);
|
|
console.log(`Office Light running at http://localhost:${port}`);
|
|
|
});
|
|
});
|
|
|
|
|
+
|
|
|
|
|
+let shuttingDown = false;
|
|
|
|
|
+
|
|
|
|
|
+async function shutdown(signal) {
|
|
|
|
|
+ if (shuttingDown) return;
|
|
|
|
|
+ shuttingDown = true;
|
|
|
|
|
+ console.log(`Received ${signal}, shutting down...`);
|
|
|
|
|
+
|
|
|
|
|
+ schedulerTask.stop();
|
|
|
|
|
+
|
|
|
|
|
+ await new Promise((resolve) => {
|
|
|
|
|
+ server.close(resolve);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ await mqttService.close();
|
|
|
|
|
+ db.close();
|
|
|
|
|
+ process.exit(0);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+process.on('SIGTERM', () => shutdown('SIGTERM').catch((error) => {
|
|
|
|
|
+ console.error('Shutdown failed:', error);
|
|
|
|
|
+ process.exit(1);
|
|
|
|
|
+}));
|
|
|
|
|
+
|
|
|
|
|
+process.on('SIGINT', () => shutdown('SIGINT').catch((error) => {
|
|
|
|
|
+ console.error('Shutdown failed:', error);
|
|
|
|
|
+ process.exit(1);
|
|
|
|
|
+}));
|