Skip to content

Commit 1a595e8

Browse files
committed
Validate action passed to script
1 parent 129005e commit 1a595e8

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

scripts/powershell/maintenance.ps1

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
param(
2-
[ValidateSet("BackupPACSDatabase","BackupMWLDatabase","RotateLogs")]
32
[string]$BaseInstallPath = "C:\Program Files\NHS\ManageBreastScreeningGateway",
3+
[Parameter(Mandatory)]
44
[string]$Action
55
)
66

@@ -72,7 +72,12 @@ function Start-AllServices {
7272
Start-Sleep -Milliseconds 500
7373
}
7474

75-
Write-Log "$($svc.Name) started." "SUCCESS"
75+
$finalStatus = Get-Service -Name $svc.Name -ErrorAction SilentlyContinue
76+
if (-not $finalStatus -or $finalStatus.Status -ne 'Running') {
77+
throw "Service $($svc.Name) did not start within ${TimeoutSeconds}s (state: $($finalStatus.Status))."
78+
}
79+
80+
Write-Log "$($svc.Name) started in $([math]::Round($stopwatch.Elapsed.TotalSeconds, 1))s." "SUCCESS"
7681
}
7782
}
7883

@@ -178,27 +183,33 @@ function Archive-PACS {
178183

179184
# -- Main ----------------------------------------------------------------------
180185

186+
$startStopTimeoutSeconds = 30
187+
181188
switch ($Action) {
182189

183190
"RotateLogs" {
184-
Stop-AllServices -Services $Services
191+
Stop-AllServices -Services $Services -TimeoutSeconds $startStopTimeoutSeconds
185192
Rotate-ServiceLogs -Services $services -LogsDir $logsDir
186-
Start-AllServices -Services $Services
193+
Start-AllServices -Services $Services -TimeoutSeconds $startStopTimeoutSeconds
187194
}
188195

189196
"BackupPACSDatabase" {
190-
Stop-AllServices -Services $Services
197+
Stop-AllServices -Services $Services -TimeoutSeconds $startStopTimeoutSeconds
191198
Invoke-DatabaseBackup -BaseInstallPath $BaseInstallPath -DbServiceName "PACS"
192199
Archive-PACS -BaseInstallPath $BaseInstallPath
193-
Start-AllServices -Services $Services
200+
Start-AllServices -Services $Services -TimeoutSeconds $startStopTimeoutSeconds
194201
}
195202

196203
"BackupMWLDatabase" {
197-
Stop-AllServices -Services $Services
204+
Stop-AllServices -Services $Services -TimeoutSeconds $startStopTimeoutSeconds
198205
Invoke-DatabaseBackup -BaseInstallPath $BaseInstallPath -DbServiceName "MWL"
199-
Start-AllServices -Services $Services
206+
Start-AllServices -Services $Services -TimeoutSeconds $startStopTimeoutSeconds
200207
}
201208

209+
default {
210+
Write-Log "Unknown action: $Action" "ERROR"
211+
throw "Unknown action: $Action"
212+
}
202213
}
203214

204215
exit 0

0 commit comments

Comments
 (0)