This commit is contained in:
2025-09-26 21:56:06 +03:00
parent c17812a0c1
commit 2216f21c47
17 changed files with 3361 additions and 631 deletions

View File

@@ -0,0 +1,407 @@
@echo off
REM Windows Print Service - ENHANCED INSTALLER - Fixes Error 1053
REM This installer addresses the most common causes of Windows Service startup failures
setlocal enabledelayedexpansion
echo =========================================
echo Quality Print Service - ENHANCED INSTALLER
echo Fixes Windows Service Error 1053
echo =========================================
echo.
echo This installer includes multiple methods to ensure service starts:
echo ✅ Windows SC Service (preferred)
echo ✅ Task Scheduler Service (fallback)
echo ✅ Startup Script (manual fallback)
echo ✅ Enhanced error detection and recovery
echo.
REM Check for administrator privileges
net session >nul 2>&1
if %errorLevel% neq 0 (
echo ❌ ERROR: Administrator privileges required
echo Please right-click this file and select "Run as administrator"
echo.
pause
exit /b 1
)
echo [1/8] Administrator privileges confirmed ✅
echo.
REM Set variables
set CURRENT_DIR=%~dp0
set SERVICE_NAME=QualityPrintService
set SERVICE_DISPLAY_NAME=Quality Print Service
set INSTALL_DIR=C:\QualityPrintService
set LOG_DIR=%USERPROFILE%\PrintService\logs
REM Detect Python executable
echo [2/8] Detecting Python installation...
set PYTHON_EXE=
set PYTHON_SCRIPT=%INSTALL_DIR%\print_service_complete.py
REM Check for embedded Python first
if exist "%INSTALL_DIR%\python_embedded\python.exe" (
set PYTHON_EXE=%INSTALL_DIR%\python_embedded\python.exe
echo Found embedded Python: !PYTHON_EXE!
) else if exist "%CURRENT_DIR%python_embedded\python.exe" (
set PYTHON_EXE=%CURRENT_DIR%python_embedded\python.exe
echo Found embedded Python in installer: !PYTHON_EXE!
) else (
REM Check system Python
python --version >nul 2>&1
if !errorLevel! equ 0 (
set PYTHON_EXE=python
echo Found system Python ✅
) else (
echo ❌ ERROR: Python not found
echo Please ensure Python 3.7+ is installed or use the zero-dependency package
pause
exit /b 1
)
)
echo.
REM Stop existing service
echo [3/8] Stopping existing service (if any)...
sc query "%SERVICE_NAME%" >nul 2>&1
if %errorLevel% equ 0 (
echo Stopping existing service...
net stop "%SERVICE_NAME%" >nul 2>&1
sc delete "%SERVICE_NAME%" >nul 2>&1
timeout /t 3 >nul
)
REM Stop any existing task
schtasks /end /tn "%SERVICE_NAME%" >nul 2>&1
schtasks /delete /tn "%SERVICE_NAME%" /f >nul 2>&1
echo Service cleanup completed ✅
echo.
REM Create installation directory
echo [4/8] Creating installation directory...
if exist "%INSTALL_DIR%" (
echo Removing old installation...
rmdir /s /q "%INSTALL_DIR%" >nul 2>&1
)
mkdir "%INSTALL_DIR%" >nul 2>&1
REM Create log directory
mkdir "%LOG_DIR%" >nul 2>&1
echo Installation directory: %INSTALL_DIR%
echo Log directory: %LOG_DIR%
echo.
REM Copy service files
echo [5/8] Installing service files...
REM Copy Python embedded if available
if exist "%CURRENT_DIR%python_embedded\" (
echo Copying embedded Python distribution...
xcopy "%CURRENT_DIR%python_embedded" "%INSTALL_DIR%\python_embedded\" /E /I /Y >nul
set PYTHON_EXE=%INSTALL_DIR%\python_embedded\python.exe
echo Embedded Python installed ✅
)
REM Copy service scripts
copy "%CURRENT_DIR%print_service_complete.py" "%INSTALL_DIR%\" >nul
copy "%CURRENT_DIR%service_wrapper.py" "%INSTALL_DIR%\" >nul 2>&1
copy "%CURRENT_DIR%service_installer.py" "%INSTALL_DIR%\" >nul 2>&1
echo Service files installed ✅
echo.
REM Test service before installing
echo [6/8] Testing service functionality...
cd /d "%INSTALL_DIR%"
echo Testing Python and service script...
"%PYTHON_EXE%" "%PYTHON_SCRIPT%" --test >nul 2>&1
if %errorLevel% equ 0 (
echo Service test passed ✅
) else (
echo ⚠️ Service test failed - continuing with installation
)
REM Check port availability
echo Testing port 8765 availability...
netstat -an | findstr :8765 >nul 2>&1
if %errorLevel% equ 0 (
echo ⚠️ Port 8765 is in use - service may conflict
) else (
echo Port 8765 available ✅
)
echo.
REM Install service - Multiple methods
echo [7/8] Installing Windows service with multiple fallback methods...
REM Create enhanced service wrapper
set WRAPPER_BAT=%INSTALL_DIR%\enhanced_service_wrapper.bat
echo @echo off > "%WRAPPER_BAT%"
echo REM Enhanced Windows Service Wrapper - Error 1053 Fix >> "%WRAPPER_BAT%"
echo cd /d "%INSTALL_DIR%" >> "%WRAPPER_BAT%"
echo. >> "%WRAPPER_BAT%"
echo REM Log service start attempt >> "%WRAPPER_BAT%"
echo echo %%date%% %%time%% - Service wrapper starting... ^>^> "%LOG_DIR%\service_wrapper.log" >> "%WRAPPER_BAT%"
echo. >> "%WRAPPER_BAT%"
echo REM Verify files exist >> "%WRAPPER_BAT%"
echo if not exist "%PYTHON_EXE%" ( >> "%WRAPPER_BAT%"
echo echo ERROR: Python not found at %PYTHON_EXE% ^>^> "%LOG_DIR%\service_wrapper.log" >> "%WRAPPER_BAT%"
echo exit /b 1 >> "%WRAPPER_BAT%"
echo ^) >> "%WRAPPER_BAT%"
echo. >> "%WRAPPER_BAT%"
echo if not exist "%PYTHON_SCRIPT%" ( >> "%WRAPPER_BAT%"
echo echo ERROR: Service script not found ^>^> "%LOG_DIR%\service_wrapper.log" >> "%WRAPPER_BAT%"
echo exit /b 1 >> "%WRAPPER_BAT%"
echo ^) >> "%WRAPPER_BAT%"
echo. >> "%WRAPPER_BAT%"
echo REM Start service with error handling >> "%WRAPPER_BAT%"
echo echo Starting service process... ^>^> "%LOG_DIR%\service_wrapper.log" >> "%WRAPPER_BAT%"
echo "%PYTHON_EXE%" "%PYTHON_SCRIPT%" --service >> "%WRAPPER_BAT%"
echo. >> "%WRAPPER_BAT%"
echo REM Log exit code >> "%WRAPPER_BAT%"
echo echo Service exited with code %%errorlevel%% ^>^> "%LOG_DIR%\service_wrapper.log" >> "%WRAPPER_BAT%"
REM Method 1: Windows SC Service
echo Installing as Windows SC Service...
sc create "%SERVICE_NAME%" binPath= "\"%WRAPPER_BAT%\"" DisplayName= "%SERVICE_DISPLAY_NAME%" start= auto >nul 2>&1
if %errorLevel% equ 0 (
echo Windows SC Service created ✅
REM Configure recovery
sc failure "%SERVICE_NAME%" reset= 86400 actions= restart/5000/restart/5000/restart/5000 >nul 2>&1
REM Try to start service
echo Starting SC service...
net start "%SERVICE_NAME%" >nul 2>&1
if !errorLevel! equ 0 (
echo SC Service started successfully ✅
set SERVICE_METHOD=SC_SERVICE
goto :service_installed
) else (
echo ⚠️ SC Service created but failed to start - trying Task Scheduler...
)
) else (
echo ❌ SC Service creation failed - trying Task Scheduler...
)
REM Method 2: Task Scheduler Service
echo Installing as Task Scheduler Service...
REM Create task XML
set TASK_XML=%INSTALL_DIR%\%SERVICE_NAME%_task.xml
echo ^<?xml version="1.0" encoding="UTF-16"?^> > "%TASK_XML%"
echo ^<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"^> >> "%TASK_XML%"
echo ^<RegistrationInfo^> >> "%TASK_XML%"
echo ^<Description^>Quality Print Service - Automatic Label Printing^</Description^> >> "%TASK_XML%"
echo ^</RegistrationInfo^> >> "%TASK_XML%"
echo ^<Triggers^> >> "%TASK_XML%"
echo ^<BootTrigger^> >> "%TASK_XML%"
echo ^<Enabled^>true^</Enabled^> >> "%TASK_XML%"
echo ^</BootTrigger^> >> "%TASK_XML%"
echo ^</Triggers^> >> "%TASK_XML%"
echo ^<Principals^> >> "%TASK_XML%"
echo ^<Principal id="Author"^> >> "%TASK_XML%"
echo ^<UserId^>S-1-5-18^</UserId^> >> "%TASK_XML%"
echo ^<RunLevel^>HighestAvailable^</RunLevel^> >> "%TASK_XML%"
echo ^</Principal^> >> "%TASK_XML%"
echo ^</Principals^> >> "%TASK_XML%"
echo ^<Settings^> >> "%TASK_XML%"
echo ^<MultipleInstancesPolicy^>IgnoreNew^</MultipleInstancesPolicy^> >> "%TASK_XML%"
echo ^<DisallowStartIfOnBatteries^>false^</DisallowStartIfOnBatteries^> >> "%TASK_XML%"
echo ^<StopIfGoingOnBatteries^>false^</StopIfGoingOnBatteries^> >> "%TASK_XML%"
echo ^<AllowHardTerminate^>true^</AllowHardTerminate^> >> "%TASK_XML%"
echo ^<StartWhenAvailable^>true^</StartWhenAvailable^> >> "%TASK_XML%"
echo ^<RunOnlyIfNetworkAvailable^>false^</RunOnlyIfNetworkAvailable^> >> "%TASK_XML%"
echo ^<AllowStartOnDemand^>true^</AllowStartOnDemand^> >> "%TASK_XML%"
echo ^<Enabled^>true^</Enabled^> >> "%TASK_XML%"
echo ^<Hidden^>false^</Hidden^> >> "%TASK_XML%"
echo ^<RestartOnFailure^> >> "%TASK_XML%"
echo ^<Interval^>PT5M^</Interval^> >> "%TASK_XML%"
echo ^<Count^>3^</Count^> >> "%TASK_XML%"
echo ^</RestartOnFailure^> >> "%TASK_XML%"
echo ^</Settings^> >> "%TASK_XML%"
echo ^<Actions Context="Author"^> >> "%TASK_XML%"
echo ^<Exec^> >> "%TASK_XML%"
echo ^<Command^>"%PYTHON_EXE%"^</Command^> >> "%TASK_XML%"
echo ^<Arguments^>"%PYTHON_SCRIPT%" --service^</Arguments^> >> "%TASK_XML%"
echo ^<WorkingDirectory^>%INSTALL_DIR%^</WorkingDirectory^> >> "%TASK_XML%"
echo ^</Exec^> >> "%TASK_XML%"
echo ^</Actions^> >> "%TASK_XML%"
echo ^</Task^> >> "%TASK_XML%"
schtasks /create /tn "%SERVICE_NAME%" /xml "%TASK_XML%" >nul 2>&1
if %errorLevel% equ 0 (
echo Task Scheduler service created ✅
REM Start task
schtasks /run /tn "%SERVICE_NAME%" >nul 2>&1
if !errorLevel! equ 0 (
echo Task Scheduler service started ✅
set SERVICE_METHOD=TASK_SCHEDULER
goto :service_installed
) else (
echo ⚠️ Task created but failed to start
)
) else (
echo ❌ Task Scheduler creation failed
)
REM Method 3: Startup Script Fallback
echo Installing as Startup Script...
set STARTUP_DIR=%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup
set STARTUP_SCRIPT=%STARTUP_DIR%\QualityPrintService.bat
echo @echo off > "%STARTUP_SCRIPT%"
echo REM Quality Print Service - Startup Script >> "%STARTUP_SCRIPT%"
echo cd /d "%INSTALL_DIR%" >> "%STARTUP_SCRIPT%"
echo start /min "Quality Print Service" "%PYTHON_EXE%" "%PYTHON_SCRIPT%" --service >> "%STARTUP_SCRIPT%"
if exist "%STARTUP_SCRIPT%" (
echo Startup script created ✅
REM Start immediately
start /min "" "%STARTUP_SCRIPT%"
echo Service started via startup script ✅
set SERVICE_METHOD=STARTUP_SCRIPT
goto :service_installed
)
REM If we get here, all methods failed
echo ❌ All installation methods failed
goto :installation_failed
:service_installed
echo.
REM Verification
echo [8/8] Verifying installation...
echo Waiting for service to start...
timeout /t 5 >nul
REM Test service connection
echo Testing service connection...
for /L %%i in (1,1,10) do (
curl -s http://localhost:8765/health >nul 2>&1
if !errorLevel! equ 0 (
echo Service is responding ✅
goto :success
)
powershell -Command "try { Invoke-WebRequest -Uri 'http://localhost:8765/health' -UseBasicParsing } catch { exit 1 }" >nul 2>&1
if !errorLevel! equ 0 (
echo Service is responding ✅
goto :success
)
echo Attempt %%i failed, retrying...
timeout /t 2 >nul
)
echo ⚠️ Service installed but not responding
goto :partial_success
:success
echo.
echo ========================================
echo INSTALLATION SUCCESSFUL! 🎉
echo ========================================
echo.
echo ✅ Service Method: !SERVICE_METHOD!
echo ✅ Service URL: http://localhost:8765
echo ✅ Health Check: http://localhost:8765/health
echo ✅ Service will start automatically on boot
echo.
echo 📋 NEXT STEPS:
echo 1. Install Chrome extension from 'chrome_extension' folder
echo 2. Test printing from the web application
echo 3. Configure printer settings if needed
echo.
echo 📊 Service Management:
if "!SERVICE_METHOD!"=="SC_SERVICE" (
echo - Start: net start %SERVICE_NAME%
echo - Stop: net stop %SERVICE_NAME%
echo - Status: sc query %SERVICE_NAME%
) else if "!SERVICE_METHOD!"=="TASK_SCHEDULER" (
echo - Start: schtasks /run /tn %SERVICE_NAME%
echo - Stop: schtasks /end /tn %SERVICE_NAME%
echo - Status: schtasks /query /tn %SERVICE_NAME%
) else (
echo - Start: Run startup script manually
echo - Stop: End process in Task Manager
)
echo.
echo 📁 Logs: %LOG_DIR%
echo.
goto :end
:partial_success
echo.
echo ========================================
echo INSTALLATION COMPLETED
echo ========================================
echo.
echo ⚠️ Service installed but verification failed
echo 📋 Manual verification steps:
echo.
echo 1. Check if service is running:
if "!SERVICE_METHOD!"=="SC_SERVICE" (
echo sc query %SERVICE_NAME%
) else if "!SERVICE_METHOD!"=="TASK_SCHEDULER" (
echo schtasks /query /tn %SERVICE_NAME%
) else (
echo Check Task Manager for python.exe process
)
echo.
echo 2. Test service manually:
echo http://localhost:8765/health
echo.
echo 3. Check logs:
echo %LOG_DIR%\service_wrapper.log
echo.
echo 4. Manual start if needed:
if "!SERVICE_METHOD!"=="SC_SERVICE" (
echo net start %SERVICE_NAME%
) else if "!SERVICE_METHOD!"=="TASK_SCHEDULER" (
echo schtasks /run /tn %SERVICE_NAME%
) else (
echo Run: %STARTUP_SCRIPT%
)
echo.
goto :end
:installation_failed
echo.
echo ========================================
echo INSTALLATION FAILED
echo ========================================
echo.
echo ❌ All service installation methods failed
echo.
echo 📋 Troubleshooting steps:
echo 1. Verify Administrator privileges
echo 2. Check Python installation
echo 3. Ensure port 8765 is available
echo 4. Review logs in: %LOG_DIR%
echo 5. Try manual service start
echo.
echo 🔧 Manual installation:
echo cd /d "%INSTALL_DIR%"
echo "%PYTHON_EXE%" "%PYTHON_SCRIPT%" --standalone
echo.
pause
exit /b 1
:end
echo Press any key to exit...
pause >nul