diff --git a/modules/messageLoader.js b/modules/messageLoader.js index 441f474..0819d08 100644 --- a/modules/messageLoader.js +++ b/modules/messageLoader.js @@ -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'); @@ -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; diff --git a/modules/systemEventAndErrorHandler.js b/modules/systemEventAndErrorHandler.js index 24bb527..dd09f85 100644 --- a/modules/systemEventAndErrorHandler.js +++ b/modules/systemEventAndErrorHandler.js @@ -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; + } + }); + } }); }; diff --git a/start_dependencies_installer.bat b/start_dependencies_installer.bat index 02c2837..206ca86 100644 --- a/start_dependencies_installer.bat +++ b/start_dependencies_installer.bat @@ -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" diff --git a/start_node_js_installer.bat b/start_node_js_installer.bat index 95141cc..454e2c5 100644 --- a/start_node_js_installer.bat +++ b/start_node_js_installer.bat @@ -2,6 +2,7 @@ :: https://github.com/sergeiown/Alert_Server/blob/main/LICENSE @echo off +title NodeJS installation where node > nul 2>nul diff --git a/start_recovery.bat b/start_recovery.bat new file mode 100644 index 0000000..3ba655d --- /dev/null +++ b/start_recovery.bat @@ -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 diff --git a/start_uninstall.bat b/start_uninstall.bat index 92970a9..8592a12 100644 --- a/start_uninstall.bat +++ b/start_uninstall.bat @@ -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 diff --git a/stop_alertserver.bat b/stop_alertserver.bat index 25eb05a..edd5a2d 100644 --- a/stop_alertserver.bat +++ b/stop_alertserver.bat @@ -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 \ No newline at end of file