|
| 1 | +param( |
| 2 | + [ValidateSet("StopBackupRotate","StartServices")] |
| 3 | + [string]$BaseInstallPath = "C:\Program Files\NHS\ManageBreastScreeningGateway", |
| 4 | + [string]$Action |
| 5 | +) |
| 6 | + |
| 7 | + |
| 8 | +# -- Service control helpers ------------------------------------------------------- |
| 9 | + |
| 10 | +function Stop-AllServices { |
| 11 | + param([array]$Services, [int]$TimeoutSeconds) |
| 12 | + foreach ($svc in $Services) { |
| 13 | + $status = Get-Service -Name $svc.Name -ErrorAction SilentlyContinue |
| 14 | + if (-not $status -or $status.Status -eq 'Stopped') { continue } |
| 15 | + |
| 16 | + Write-Log "Stopping $($svc.Name) (timeout: ${TimeoutSeconds}s)..." "INFO" |
| 17 | + Stop-Service -Name $svc.Name -Force |
| 18 | + |
| 19 | + $stopwatch = [System.Diagnostics.Stopwatch]::StartNew() |
| 20 | + while ($stopwatch.Elapsed.TotalSeconds -lt $TimeoutSeconds) { |
| 21 | + $current = Get-Service -Name $svc.Name -ErrorAction SilentlyContinue |
| 22 | + if (-not $current -or $current.Status -eq 'Stopped') { break } |
| 23 | + Start-Sleep -Milliseconds 500 |
| 24 | + } |
| 25 | + $stopwatch.Stop() |
| 26 | + |
| 27 | + $finalStatus = Get-Service -Name $svc.Name -ErrorAction SilentlyContinue |
| 28 | + if ($finalStatus -and $finalStatus.Status -ne 'Stopped') { |
| 29 | + throw "Service $($svc.Name) did not stop within ${TimeoutSeconds}s (state: $($finalStatus.Status))." |
| 30 | + } |
| 31 | + Write-Log "$($svc.Name) stopped in $([math]::Round($stopwatch.Elapsed.TotalSeconds, 1))s." "INFO" |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +function Start-AllServices { |
| 36 | + param( |
| 37 | + [array]$Services, [int]$TimeoutSeconds |
| 38 | + ) |
| 39 | + |
| 40 | + foreach ($svc in $Services) { |
| 41 | + Write-Log "Starting $($svc.Name)..." "INFO" |
| 42 | + |
| 43 | + Start-Service -Name $svc.Name |
| 44 | + |
| 45 | + $stopwatch = [System.Diagnostics.Stopwatch]::StartNew() |
| 46 | + while ($stopwatch.Elapsed.TotalSeconds -lt $TimeoutSeconds) { |
| 47 | + $status = Get-Service -Name $svc.Name -ErrorAction SilentlyContinue |
| 48 | + if ($status -and $status.Status -eq 'Running') { break } |
| 49 | + Start-Sleep -Milliseconds 500 |
| 50 | + } |
| 51 | + |
| 52 | + if ($status.Status -ne 'Running') { |
| 53 | + throw "Failed to start $($svc.Name)" |
| 54 | + } |
| 55 | + |
| 56 | + Write-Log "$($svc.Name) started." "SUCCESS" |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +# -- Database backup ---------------------------------------------------------- |
| 61 | + |
| 62 | +function Invoke-DatabaseBackup { |
| 63 | + param( |
| 64 | + [string]$BaseInstallPath |
| 65 | + ) |
| 66 | + |
| 67 | + $batPath = Join-Path $versionDir "backup_database.bat" |
| 68 | + |
| 69 | + if (-not (Test-Path $batPath)) { |
| 70 | + $batContent = @( |
| 71 | + '@echo off', |
| 72 | + "cd /d `"$BaseInstallPath`"", |
| 73 | + 'set "PYTHONPATH=current\src"', |
| 74 | + ('"current\.venv\Scripts\python.exe" "-c import scripts.python.database; scripts.python.database.backup_databases()"') |
| 75 | + ) -join "`r`n" |
| 76 | + [System.IO.File]::WriteAllText($batPath, $batContent, [System.Text.Encoding]::ASCII) |
| 77 | + } |
| 78 | + |
| 79 | + Write-Log "Starting database backup..." "INFO" |
| 80 | + |
| 81 | + & cmd.exe /c "`"$batPath`"" |
| 82 | + |
| 83 | + if ($LASTEXITCODE -ne 0) { |
| 84 | + throw "Database backup failed with exit code $LASTEXITCODE" |
| 85 | + } |
| 86 | + |
| 87 | + Write-Log "Database backup completed successfully." "SUCCESS" |
| 88 | +} |
| 89 | + |
| 90 | +# -- Log Rotation ------------------------------------------------------------- |
| 91 | + |
| 92 | +function Rotate-LogFile { |
| 93 | + param( |
| 94 | + [Parameter(Mandatory)] |
| 95 | + [string]$LogFile, |
| 96 | + |
| 97 | + [int]$RetainCount = 5 |
| 98 | + ) |
| 99 | + |
| 100 | + if (-not (Test-Path $LogFile)) { |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + # Remove oldest |
| 105 | + $oldest = "$LogFile.$RetainCount" |
| 106 | + if (Test-Path $oldest) { |
| 107 | + Remove-Item $oldest -Force |
| 108 | + } |
| 109 | + |
| 110 | + # Shift existing rotations |
| 111 | + for ($i = $RetainCount - 1; $i -ge 1; $i--) { |
| 112 | + $src = "$LogFile.$i" |
| 113 | + $dst = "$LogFile." + ($i + 1) |
| 114 | + |
| 115 | + if (Test-Path $src) { |
| 116 | + Move-Item $src $dst -Force |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + Move-Item $LogFile "$LogFile.1" -Force |
| 121 | +} |
| 122 | + |
| 123 | +function Rotate-ServiceLogs { |
| 124 | + param( |
| 125 | + [array]$Services, |
| 126 | + [string]$LogsDir |
| 127 | + ) |
| 128 | + |
| 129 | + foreach ($svc in $Services) { |
| 130 | + $logFile = Join-Path $LogsDir "$($svc.Name).log" |
| 131 | + Rotate-LogFile -LogFile $logFile -RetainCount 5 |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +# -- Main ---------------------------------------------------------------------- |
| 136 | + |
| 137 | +$logsDir = Join-Path $BaseInstallPath "logs" |
| 138 | + |
| 139 | +$services = Get-Service | |
| 140 | + Where-Object { $_.Name -like "Gateway-*" } | |
| 141 | + ForEach-Object { |
| 142 | + @{ Name = $_.Name } |
| 143 | + } |
| 144 | + |
| 145 | +switch ($Action) { |
| 146 | + |
| 147 | + "StopBackupRotate" { |
| 148 | + |
| 149 | + Stop-AllServices ` |
| 150 | + -Services $services ` |
| 151 | + -TimeoutSeconds 30 |
| 152 | + |
| 153 | + Invoke-DatabaseBackup ` |
| 154 | + -BaseInstallPath $BaseInstallPath |
| 155 | + |
| 156 | + Rotate-ServiceLogs ` |
| 157 | + -Services $services ` |
| 158 | + -LogsDir $logsDir |
| 159 | + } |
| 160 | + |
| 161 | + "StartServices" { |
| 162 | + |
| 163 | + Start-AllServices ` |
| 164 | + -Services $services |
| 165 | + -TimeoutSeconds 30 |
| 166 | + } |
| 167 | +} |
0 commit comments