feat: schedule local workspace verification
This commit is contained in:
100
scripts/register-scheduled-verification.ps1
Normal file
100
scripts/register-scheduled-verification.ps1
Normal file
@@ -0,0 +1,100 @@
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[string] $TaskName = 'KRI Agent Harness Workspace Verify',
|
||||
[string] $At = '08:30',
|
||||
[string] $WorkspaceRoot,
|
||||
[switch] $Unregister
|
||||
)
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
|
||||
$scriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
$harnessRoot = Split-Path -Parent $scriptRoot
|
||||
$runnerScript = Join-Path $scriptRoot 'run-scheduled-verification.ps1'
|
||||
|
||||
if ($Unregister) {
|
||||
$existingTask = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
|
||||
if ($null -eq $existingTask) {
|
||||
Write-Host "Scheduled task is not registered: $TaskName"
|
||||
exit 0
|
||||
}
|
||||
|
||||
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false
|
||||
Write-Host "Scheduled task removed: $TaskName"
|
||||
exit 0
|
||||
}
|
||||
|
||||
if ([string]::IsNullOrWhiteSpace($WorkspaceRoot)) {
|
||||
$WorkspaceRoot = Split-Path -Parent $harnessRoot
|
||||
}
|
||||
|
||||
$scheduledTime = [TimeSpan]::Zero
|
||||
if (-not [TimeSpan]::TryParseExact(
|
||||
$At,
|
||||
'hh\:mm',
|
||||
[System.Globalization.CultureInfo]::InvariantCulture,
|
||||
[ref] $scheduledTime
|
||||
)) {
|
||||
throw "Scheduled time must use HH:mm format. Found: $At"
|
||||
}
|
||||
|
||||
$resolvedWorkspaceRoot = [System.IO.Path]::GetFullPath($WorkspaceRoot)
|
||||
$expectedHarnessRoot = [System.IO.Path]::GetFullPath(
|
||||
(Join-Path $resolvedWorkspaceRoot '_________AgentHarnessBase')
|
||||
)
|
||||
$resolvedHarnessRoot = [System.IO.Path]::GetFullPath($harnessRoot)
|
||||
|
||||
if (-not $resolvedHarnessRoot.Equals(
|
||||
$expectedHarnessRoot,
|
||||
[System.StringComparison]::OrdinalIgnoreCase
|
||||
)) {
|
||||
throw "Harness repository must be a direct child of the configured workspace root. Expected: $expectedHarnessRoot"
|
||||
}
|
||||
|
||||
if (-not (Test-Path -LiteralPath $runnerScript -PathType Leaf)) {
|
||||
throw "Scheduled verification runner not found: $runnerScript"
|
||||
}
|
||||
|
||||
$account = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
|
||||
$powershellCommand = Join-Path $PSHOME 'powershell.exe'
|
||||
$actionArguments = @(
|
||||
'-NoProfile'
|
||||
'-NonInteractive'
|
||||
'-ExecutionPolicy Bypass'
|
||||
"-File `"$runnerScript`""
|
||||
"-WorkspaceRoot `"$resolvedWorkspaceRoot`""
|
||||
) -join ' '
|
||||
|
||||
$action = New-ScheduledTaskAction `
|
||||
-Execute $powershellCommand `
|
||||
-Argument $actionArguments `
|
||||
-WorkingDirectory $resolvedHarnessRoot
|
||||
$trigger = New-ScheduledTaskTrigger `
|
||||
-Weekly `
|
||||
-WeeksInterval 1 `
|
||||
-DaysOfWeek Monday, Tuesday, Wednesday, Thursday, Friday `
|
||||
-At ([DateTime]::Today.Add($scheduledTime))
|
||||
$principal = New-ScheduledTaskPrincipal `
|
||||
-UserId $account `
|
||||
-LogonType Interactive `
|
||||
-RunLevel Limited
|
||||
$settings = New-ScheduledTaskSettingsSet `
|
||||
-StartWhenAvailable `
|
||||
-MultipleInstances IgnoreNew `
|
||||
-RestartCount 3 `
|
||||
-RestartInterval (New-TimeSpan -Minutes 10) `
|
||||
-ExecutionTimeLimit (New-TimeSpan -Hours 1)
|
||||
|
||||
Register-ScheduledTask `
|
||||
-TaskName $TaskName `
|
||||
-Action $action `
|
||||
-Trigger $trigger `
|
||||
-Principal $principal `
|
||||
-Settings $settings `
|
||||
-Description 'Runs the read-only KRI Agent Harness workspace verification and stores a local log.' `
|
||||
-Force | Out-Null
|
||||
|
||||
Write-Host "Scheduled task registered: $TaskName"
|
||||
Write-Host "User: $account"
|
||||
Write-Host "Schedule: Monday-Friday $At"
|
||||
Write-Host "Workspace: $resolvedWorkspaceRoot"
|
||||
89
scripts/run-scheduled-verification.ps1
Normal file
89
scripts/run-scheduled-verification.ps1
Normal file
@@ -0,0 +1,89 @@
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[string] $WorkspaceRoot,
|
||||
[string] $LogDirectory,
|
||||
[ValidateRange(1, 3650)]
|
||||
[int] $RetentionDays = 30
|
||||
)
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
|
||||
$scriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
$harnessRoot = Split-Path -Parent $scriptRoot
|
||||
$verificationScript = Join-Path $scriptRoot 'verify-workspace.ps1'
|
||||
|
||||
if ([string]::IsNullOrWhiteSpace($WorkspaceRoot)) {
|
||||
$WorkspaceRoot = Split-Path -Parent $harnessRoot
|
||||
}
|
||||
|
||||
$resolvedWorkspaceRoot = [System.IO.Path]::GetFullPath($WorkspaceRoot)
|
||||
|
||||
if ([string]::IsNullOrWhiteSpace($LogDirectory)) {
|
||||
$LogDirectory = Join-Path $harnessRoot '.local\verification-logs'
|
||||
}
|
||||
|
||||
$resolvedLogDirectory = [System.IO.Path]::GetFullPath($LogDirectory)
|
||||
[System.IO.Directory]::CreateDirectory($resolvedLogDirectory) | Out-Null
|
||||
|
||||
$timestamp = Get-Date -Format 'yyyyMMdd-HHmmss'
|
||||
$logFile = Join-Path $resolvedLogDirectory "verify-$timestamp.log"
|
||||
$utf8WithoutBom = [System.Text.UTF8Encoding]::new($false)
|
||||
$logWriter = [System.IO.StreamWriter]::new($logFile, $false, $utf8WithoutBom)
|
||||
$exitCode = 1
|
||||
|
||||
function Write-VerificationLogLine {
|
||||
param([AllowEmptyString()][string] $Line)
|
||||
|
||||
Write-Host $Line
|
||||
$logWriter.WriteLine($Line)
|
||||
$logWriter.Flush()
|
||||
}
|
||||
|
||||
try {
|
||||
Write-VerificationLogLine "[Agent Harness] scheduled verification started=$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss zzz')"
|
||||
Write-VerificationLogLine "[Agent Harness] computer=$env:COMPUTERNAME"
|
||||
Write-VerificationLogLine "[Agent Harness] log=$logFile"
|
||||
|
||||
$powershellCommand = Join-Path $PSHOME 'powershell.exe'
|
||||
|
||||
& $powershellCommand `
|
||||
-NoProfile `
|
||||
-NonInteractive `
|
||||
-ExecutionPolicy Bypass `
|
||||
-File $verificationScript `
|
||||
-WorkspaceRoot $resolvedWorkspaceRoot 2>&1 |
|
||||
ForEach-Object {
|
||||
Write-VerificationLogLine $_.ToString()
|
||||
}
|
||||
|
||||
$exitCode = $LASTEXITCODE
|
||||
if ($null -eq $exitCode) {
|
||||
$exitCode = 1
|
||||
}
|
||||
|
||||
Write-VerificationLogLine "[Agent Harness] scheduled verification exitCode=$exitCode"
|
||||
}
|
||||
catch {
|
||||
Write-VerificationLogLine "[Agent Harness] scheduled verification error=$($_.Exception.Message)"
|
||||
$exitCode = 1
|
||||
}
|
||||
finally {
|
||||
$logWriter.Dispose()
|
||||
}
|
||||
|
||||
$retentionCutoff = (Get-Date).AddDays(-$RetentionDays)
|
||||
|
||||
try {
|
||||
Get-ChildItem -LiteralPath $resolvedLogDirectory -File -Filter 'verify-*.log' |
|
||||
Where-Object {
|
||||
$_.FullName -ne $logFile -and $_.LastWriteTime -lt $retentionCutoff
|
||||
} |
|
||||
ForEach-Object {
|
||||
Remove-Item -LiteralPath $_.FullName -Force
|
||||
}
|
||||
}
|
||||
catch {
|
||||
Write-Warning "Failed to remove an expired verification log: $($_.Exception.Message)"
|
||||
}
|
||||
|
||||
exit $exitCode
|
||||
Reference in New Issue
Block a user