Skip to content

Commit

Permalink
Provide a recovery process in case of inaccessibility of project files
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeiown committed Sep 25, 2024
1 parent 2dfad9f commit dc44f57
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 14 deletions.
15 changes: 11 additions & 4 deletions modules/messageLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ https://github.com/sergeiown/Alert_Server/blob/main/LICENSE */

'use strict';

const { exec } = require('child_process');
const fs = require('fs');
const path = require('path');
const os = require('os');
Expand Down Expand Up @@ -40,12 +41,18 @@ try {
second: '2-digit',
})
.replace(/,\s*/g, ',');
const logMessage = `${currentDateTime},An error occurred while reading or parsing ${messagesPath}`;
const logFilePath = path.join(process.env.TEMP, 'alertserver_log.csv');
const logMessage = `${currentDateTime},File not found: ${messagesPath}. Starting the recovery process.`;
const logFilePath = path.join(process.cwd(), 'event.log');
const recoveryBatPath = path.join(process.cwd(), 'start_recovery.bat');
fs.appendFileSync(logFilePath, logMessage + os.EOL, 'utf-8');
console.error(logMessage);
messages = {};
process.exit();

exec(`start cmd /c "${recoveryBatPath}"`, (execError) => {
if (execError) {
console.error(execError.message);
return;
}
});
}

module.exports = messages;
29 changes: 20 additions & 9 deletions modules/systemEventAndErrorHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,32 @@ const formatStackTrace = (stack) => {

const handleExceptionAndRestart = () => {
const batFilePath = path.join(__dirname, '..', 'start_alertserver_hidden.bat');
const recoveryBatPath = path.join(__dirname, '..', 'start_recovery.bat');

process.on('uncaughtException', (error) => {
logEvent(messages.msg_50);
logEvent(error.message);
formatStackTrace(error.stack).forEach((line) => logEvent(line));

checkRestartFrequency();
writeRestartTimestamp();

exec(`cmd /c "${batFilePath}"`, (error) => {
if (error) {
logEvent(messages.msg_56);
return;
}
});
if (error.code === 'ENOENT') {
logEvent(`File not found: ${error.path}. Starting the recovery process.`);
exec(`start cmd /c "${recoveryBatPath}"`, (execError) => {
if (execError) {
logEvent(execError.message);
return;
}
});
} else {
checkRestartFrequency();
writeRestartTimestamp();

exec(`cmd /c "${batFilePath}"`, (error) => {
if (error) {
logEvent(messages.msg_56);
return;
}
});
}
});
};

Expand Down
1 change: 1 addition & 0 deletions start_dependencies_installer.bat
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
:: https://github.com/sergeiown/Alert_Server/blob/main/LICENSE

@echo off
title Dependencies installation

for /f "delims=" %%i in ('where npm') do set "npmPath=%%i"

Expand Down
1 change: 1 addition & 0 deletions start_node_js_installer.bat
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
:: https://github.com/sergeiown/Alert_Server/blob/main/LICENSE

@echo off
title NodeJS installation

where node > nul 2>nul

Expand Down
71 changes: 71 additions & 0 deletions start_recovery.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
:: Copyright (c) 2024 Serhii I. Myshko
:: https://github.com/sergeiown/Alert_Server/blob/main/LICENSE

@echo off
title Alert Server Recovery

cls & echo Starting the recovery process...
echo. & timeout /t 2 /nobreak > nul

taskkill /f /im node.exe >nul 2>nul

set "installationDir=%USERPROFILE%\Documents\Alert_Server"
set "tempDir=%USERPROFILE%\Documents\Temp_Alert_Server"
cd /d "%installationDir%"

for /f "delims=" %%i in ('where git') do set "gitPath=%%i"

if not defined gitPath (
echo Error: Git is not installed or not found in PATH.
echo Attempting to use a fallback path...
set "gitPath=%ProgramW6432%\Git\bin\git.exe"
)

set "githubUrl=https://github.com/sergeiown/Alert_Server"

if exist "%tempDir%" (
echo Temporary directory exists. Cleaning it up...
rmdir /s /q "%tempDir%"
)
mkdir "%tempDir%"

echo Cloning the project from GitHub...
"%gitPath%" clone "%githubUrl%" "%tempDir%"

if %errorlevel% neq 0 (
echo Error: Failed to clone the project.
pause & exit /b 1
)

echo Moving files from temporary directory to installation directory...
xcopy /e /y /q "%tempDir%\*" "%installationDir%\"

if %errorlevel% neq 0 (
echo Error: Failed to move files.
pause & exit /b 1
)

rmdir /s /q "%tempDir%"

echo. & timeout /t 2 /nobreak > nul

call "%installationDir%\start_dependencies_installer.bat"

echo. & timeout /t 2 /nobreak > nul

set sourceFile=%installationDir%\resources\images\tray.ico
set destFile=%installationDir%\node_modules\trayicon\rsrcs\default.ico

if exist "%destFile%" (
del "%destFile%"
)
copy "%sourceFile%" "%destFile%"

echo. & echo Recovery is complete.
timeout /t 2 /nobreak > nul
echo. & echo The settings are set to the default values.
echo. & timeout /t 5 /nobreak > nul

start /min "" powershell -WindowStyle Hidden -Command "& { $timestamp = Get-Date -Format 'dd.MM.yyyy HH:mm:ss'; Write-Output \"[$timestamp]\" | Out-File -FilePath 'error.log' -Append -Encoding utf8; node index.js 2>> error.log }"

exit /b
1 change: 1 addition & 0 deletions start_uninstall.bat
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
:: https://github.com/sergeiown/Alert_Server/blob/main/LICENSE

@echo off
title Alert Server Uninstall

taskkill /f /im "node.exe" >nul 2>nul

Expand Down
3 changes: 2 additions & 1 deletion stop_alertserver.bat
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
:: https://github.com/sergeiown/Alert_Server/blob/main/LICENSE

@echo off
title Alert Server Shut down

taskkill /f /im node.exe >nul 2>nul

echo Alert update server is stopped.
echo Alert Server is stopped.
pause

0 comments on commit dc44f57

Please sign in to comment.