# Deployment #2 (2026-10-09): rotating log writer + service failure recovery. # Requires elevation (service stop/start + sc failure). $ErrorActionPreference = 'Continue' $outFile = "G:\WEB-SERVER\htdocs\AttendanceHub\ZKTecoSyncService-V2\_deployment\deploy_20261009_v2_results.txt" $r = New-Object System.Collections.Generic.List[string] $r.Add("deployStart=$(Get-Date -Format o)") # 1) Stop service (graceful, then force) Stop-Service ZKTecoSyncService -Force -ErrorAction SilentlyContinue $sw = [System.Diagnostics.Stopwatch]::StartNew() while ((Get-Service ZKTecoSyncService).Status -ne 'Stopped' -and $sw.Elapsed.TotalSeconds -lt 60) { Start-Sleep -Seconds 1 } $r.Add("after_stop: state=" + (Get-Service ZKTecoSyncService).Status) Start-Sleep -Seconds 3 foreach ($p in @(Get-Process AttendanceHub.Sync.Host -ErrorAction SilentlyContinue)) { Stop-Process -Id $p.Id -Force -ErrorAction SilentlyContinue Start-Sleep -Seconds 2 $r.Add("forced_exit_pid=$($p.Id)") } # 2) Backup current binaries $pub = "G:\WEB-SERVER\htdocs\AttendanceHub\ZKTecoSyncService-V2\publish" $bak = Join-Path $pub "bin_backup_pre_rotation_20261009" New-Item -ItemType Directory -Force -Path $bak | Out-Null foreach ($f in @('AttendanceHub.Sync.dll','AttendanceHub.Sync.pdb','AttendanceHub.Sync.Host.dll','AttendanceHub.Sync.Host.exe','AttendanceHub.Sync.Host.pdb','AttendanceHub.Sync.Host.deps.json')) { Copy-Item (Join-Path $pub $f) (Join-Path $bak $f) -Force -ErrorAction SilentlyContinue } $r.Add("backup_done=$bak") # 3) Copy freshly built binaries (includes RotatingFileWriter) $src = "G:\WEB-SERVER\htdocs\AttendanceHub\ZKTecoSyncService-V2\src\AttendanceHub.Sync.Host\bin\Release\net10.0-windows" foreach ($f in @('AttendanceHub.Sync.dll','AttendanceHub.Sync.pdb','AttendanceHub.Sync.Host.dll','AttendanceHub.Sync.Host.exe','AttendanceHub.Sync.Host.pdb','AttendanceHub.Sync.Host.deps.json')) { Copy-Item (Join-Path $src $f) (Join-Path $pub $f) -Force $r.Add("copied $f len=" + (Get-Item (Join-Path $pub $f)).Length) } # 4) Archive the old unrotated log (53 MB) so the new run starts with a clean counter $log = Join-Path $pub "logs\attendancehub_sync.log" if (Test-Path $log) { $oldLen = (Get-Item $log).Length $arch = "$log.pre_rotation_20261009" Move-Item $log $arch -Force -ErrorAction SilentlyContinue $r.Add("log_archived oldBytes=$oldLen path=$arch") } # 5) Auto-restart on failure: restart after 5s, 15s, 60s; give up after 24h of attempts & sc.exe failure ZKTecoSyncService reset= 86400 actions= restart/5000/restart/15000/restart/60000 2>&1 | Out-String | ForEach-Object { $r.Add("sc_failure: " + $_.Trim()) } & sc.exe qfailure ZKTecoSyncService 2>&1 | Out-String | ForEach-Object { $r.Add("qfailure: " + ($_.Trim() -replace '\s+', ' ')) } # 6) Start service $r.Add("---- start $(Get-Date -Format o)") & sc.exe start ZKTecoSyncService 2>&1 | Out-String | ForEach-Object { $r.Add($_.Trim()) } Start-Sleep -Seconds 12 $r.Add("final_state=" + (Get-Service ZKTecoSyncService).Status) $r.Add("final_pid=" + (Get-CimInstance Win32_Service -Filter "Name='ZKTecoSyncService'").ProcessId) # 7) Verify rotation banner appeared in the fresh log if (Test-Path $log) { $head = Get-Content $log -TotalCount 3 foreach ($h in $head) { $r.Add("log_head: $h") } } else { $r.Add("log_head: (not created yet)") } $r.Add("deployEnd=$(Get-Date -Format o)") Set-Content -Path $outFile -Value ($r -join "`r`n") -Encoding UTF8