# Deployment #3 (2026-10-10): SQLite ledger retention (prune + VACUUM). # Requires elevation (service stop/start). Binaries come from the fresh publish folder. $ErrorActionPreference = 'Continue' $outFile = "G:\WEB-SERVER\htdocs\AttendanceHub\ZKTecoSyncService-V2\_deployment\deploy_20261010_ledger_results.txt" $r = New-Object System.Collections.Generic.List[string] $r.Add("deployStart=$(Get-Date -Format o)") $r.Add("host=" + $env:COMPUTERNAME) # 0) Record DB size BEFORE prune so the effect is measurable $db = "G:\WEB-SERVER\htdocs\AttendanceHub\ZKTecoSyncService-V2\publish\attendancehub_v2.db" if (Test-Path $db) { $before = (Get-Item $db).Length $wal = if (Test-Path "$db-wal") { (Get-Item "$db-wal").Length } else { 0 } $r.Add("db_before_bytes=$before wal_before_bytes=$wal total=$($before + $wal)") } else { $r.Add("db_before: MISSING ($db)") } # 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_ledger_20261010" New-Item -ItemType Directory -Force -Path $bak | Out-Null foreach ($f in @('AttendanceHub.Sync.dll','AttendanceHub.Sync.Host.dll','AttendanceHub.Sync.Host.exe','AttendanceHub.Sync.Host.deps.json')) { Copy-Item (Join-Path $pub $f) (Join-Path $bak $f) -Force -ErrorAction SilentlyContinue $r.Add("backed_up $f") } # 3) Copy freshly published binaries (includes LedgerMaintenance) $src = "G:\WEB-SERVER\htdocs\AttendanceHub\ZKTecoSyncService-V2\_build\publish-staging" foreach ($f in @('AttendanceHub.Sync.dll','AttendanceHub.Sync.Host.dll','AttendanceHub.Sync.Host.exe','AttendanceHub.Sync.Host.deps.json')) { $s = Join-Path $src $f if (Test-Path $s) { Copy-Item $s (Join-Path $pub $f) -Force $r.Add("copied $f len=" + (Get-Item (Join-Path $pub $f)).Length) } else { $r.Add("MISSING_PUBLISH $f") } } # 4) 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) $r.Add("deployEnd=$(Get-Date -Format o)") [System.IO.File]::WriteAllLines($outFile, $r) Write-Output "WROTE $outFile"