mirror of
https://github.com/legop3/MultiRoombaRover.git
synced 2026-09-16 09:31:20 -04:00
gahew
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
// server Control Service
|
||||
// Purpose: Defines the server Control Service module and the helpers/state used by this service unit.
|
||||
// Scope: Keeps runtime behavior unchanged while isolating responsibilities into a clear module boundary.
|
||||
const { spawn } = require('child_process');
|
||||
const io = require('../../globals/io');
|
||||
const logger = require('../../globals/logger').child('serverControlService');
|
||||
const { isAdmin } = require('../roleService');
|
||||
const { sendAlert } = require('../alertService');
|
||||
|
||||
const ALERT_COLOR = '#ff5722';
|
||||
let rebootPending = false;
|
||||
|
||||
function scheduleSystemReboot() {
|
||||
if (rebootPending) {
|
||||
throw new Error('Server reboot already pending');
|
||||
}
|
||||
rebootPending = true;
|
||||
|
||||
setTimeout(() => {
|
||||
logger.warn('Issuing system reboot command');
|
||||
try {
|
||||
const child = spawn('systemctl', ['reboot'], {
|
||||
detached: true,
|
||||
stdio: 'ignore',
|
||||
});
|
||||
child.unref();
|
||||
} catch (err) {
|
||||
rebootPending = false;
|
||||
logger.error('Server reboot command failed', err.message);
|
||||
}
|
||||
}, 400);
|
||||
}
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
socket.on('server:reboot', (_, cb = () => {}) => {
|
||||
if (!isAdmin(socket)) {
|
||||
cb({ error: 'Not authorized' });
|
||||
return;
|
||||
}
|
||||
try {
|
||||
scheduleSystemReboot();
|
||||
const who = socket?.data?.user?.username || socket.id;
|
||||
logger.warn('Server reboot requested', { by: who });
|
||||
sendAlert({
|
||||
color: ALERT_COLOR,
|
||||
title: 'Server Reboot',
|
||||
message: `Reboot requested by ${who}`,
|
||||
});
|
||||
cb({ success: true });
|
||||
} catch (err) {
|
||||
cb({ error: err.message });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user