Skip to content

Commit d52af39

Browse files
authored
Merge pull request #988 from KelvinTegelaar/dev
[pull] dev from KelvinTegelaar:dev
2 parents 3f97bcc + 1167ff5 commit d52af39

3 files changed

Lines changed: 77 additions & 23 deletions

File tree

Modules/CIPPHTTP/Public/Entrypoints/HTTP Functions/Invoke-ExecTestRun.ps1

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,68 @@ function Invoke-ExecTestRun {
1212

1313
try {
1414
$TenantFilter = $Request.Query.tenantFilter ?? $Request.Body.tenantFilter
15-
Write-LogMessage -API $APIName -tenant $TenantFilter -message "Starting data collection and test run for tenant: $TenantFilter" -sev Info
16-
$Batch = @(
17-
@{
18-
FunctionName = 'CIPPDBCacheData'
19-
TenantFilter = $TenantFilter
20-
QueueName = "Cache - $TenantFilter"
15+
$Mode = ($Request.Query.mode ?? $Request.Body.mode ?? 'both').ToString().ToLower()
16+
if ($Mode -notin @('both', 'cache', 'tests')) { $Mode = 'both' }
17+
18+
switch ($Mode) {
19+
'tests' {
20+
Write-LogMessage -API $APIName -tenant $TenantFilter -message "Starting tests-only run for tenant: $TenantFilter" -sev Info
21+
$InstanceId = Start-CIPPDBTestsRun -TenantFilter $TenantFilter -Force
22+
$ResultMessage = "Successfully started test run for $TenantFilter"
23+
}
24+
'cache' {
25+
Write-LogMessage -API $APIName -tenant $TenantFilter -message "Starting cache-only collection for tenant: $TenantFilter" -sev Info
26+
$Batch = @(
27+
@{
28+
FunctionName = 'CIPPDBCacheData'
29+
TenantFilter = $TenantFilter
30+
QueueName = "Cache - $TenantFilter"
31+
}
32+
)
33+
$InputObject = [PSCustomObject]@{
34+
OrchestratorName = "TestDataCollection-$TenantFilter"
35+
Batch = $Batch
36+
SkipLog = $false
37+
PostExecution = @{
38+
FunctionName = 'CIPPDBCacheApplyBatch'
39+
Parameters = @{
40+
TenantFilter = $TenantFilter
41+
}
42+
}
43+
}
44+
$InstanceId = Start-CIPPOrchestrator -InputObject $InputObject
45+
$ResultMessage = "Successfully started cache collection for $TenantFilter"
2146
}
22-
)
23-
$InputObject = [PSCustomObject]@{
24-
OrchestratorName = "TestDataCollectionAndRun-$TenantFilter"
25-
Batch = $Batch
26-
SkipLog = $false
27-
PostExecution = @{
28-
FunctionName = 'CIPPDBCacheApplyBatch'
29-
Parameters = @{
30-
TestRun = $true
31-
TenantFilter = $TenantFilter
47+
default {
48+
Write-LogMessage -API $APIName -tenant $TenantFilter -message "Starting data collection and test run for tenant: $TenantFilter" -sev Info
49+
$Batch = @(
50+
@{
51+
FunctionName = 'CIPPDBCacheData'
52+
TenantFilter = $TenantFilter
53+
QueueName = "Cache - $TenantFilter"
54+
}
55+
)
56+
$InputObject = [PSCustomObject]@{
57+
OrchestratorName = "TestDataCollectionAndRun-$TenantFilter"
58+
Batch = $Batch
59+
SkipLog = $false
60+
PostExecution = @{
61+
FunctionName = 'CIPPDBCacheApplyBatch'
62+
Parameters = @{
63+
TestRun = $true
64+
TenantFilter = $TenantFilter
65+
}
66+
}
3267
}
68+
$InstanceId = Start-CIPPOrchestrator -InputObject $InputObject
69+
$ResultMessage = "Successfully started data collection and test run for $TenantFilter"
3370
}
3471
}
3572

36-
$InstanceId = Start-CIPPOrchestrator -InputObject $InputObject
37-
3873
$StatusCode = [HttpStatusCode]::OK
39-
$Body = [PSCustomObject]@{ Results = "Successfully started data collection and test run for $TenantFilter" }
74+
$Body = [PSCustomObject]@{ Results = $ResultMessage }
4075

41-
Write-LogMessage -API $APIName -tenant $TenantFilter -message "Data collection and test run orchestration started. Instance ID: $InstanceId" -sev Info
76+
Write-LogMessage -API $APIName -tenant $TenantFilter -message "Mode '$Mode' orchestration started. Instance ID: $InstanceId" -sev Info
4277

4378
} catch {
4479
$ErrorMessage = Get-CippException -Exception $_

Shared/CIPPSharp/CIPPTestDataCache.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ namespace CIPP
1919
public static class TestDataCache
2020
{
2121
// ── Configuration ──
22-
private static long _maxBytes = 50L * 1024 * 1024; // 50 MB default
23-
private static TimeSpan _ttl = TimeSpan.FromMinutes(1);
22+
private static long _maxBytes = 100L * 1024 * 1024; // 100 MB default
23+
private static TimeSpan _ttl = TimeSpan.FromMinutes(5);
2424

2525
// ── State ──
2626
private static readonly ConcurrentDictionary<string, CacheEntry> _cache = new();
@@ -52,7 +52,7 @@ public CacheEntry(object? value, long sizeBytes, DateTime expiresUtc)
5252
}
5353

5454
/// <summary>Configure the cache limits. Call before first use or between test runs.</summary>
55-
public static void Configure(long maxBytes = 50 * 1024 * 1024, int ttlSeconds = 60)
55+
public static void Configure(long maxBytes = 100L * 1024 * 1024, int ttlSeconds = 300)
5656
{
5757
_maxBytes = maxBytes;
5858
_ttl = TimeSpan.FromSeconds(ttlSeconds);
@@ -172,6 +172,25 @@ public static void Clear()
172172
Interlocked.Exchange(ref _evictions, 0);
173173
}
174174

175+
/// <summary>
176+
/// Remove all entries belonging to a single tenant. Cache keys are formatted
177+
/// as "tenantFilter|type", so we match by the "tenantFilter|" prefix.
178+
/// </summary>
179+
public static int ClearTenant(string tenantFilter)
180+
{
181+
if (string.IsNullOrWhiteSpace(tenantFilter)) return 0;
182+
var prefix = tenantFilter + "|";
183+
int removed = 0;
184+
// Snapshot keys to avoid mutating while iterating the concurrent dictionary
185+
var matchingKeys = _cache.Keys.Where(k => k.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)).ToList();
186+
foreach (var key in matchingKeys)
187+
{
188+
RemoveEntry(key);
189+
removed++;
190+
}
191+
return removed;
192+
}
193+
175194
public static int Count => _cache.Count;
176195
public static long CurrentBytes => Interlocked.Read(ref _currentBytes);
177196
public static double CurrentMB => Math.Round(Interlocked.Read(ref _currentBytes) / (1024.0 * 1024.0), 2);

Shared/CIPPSharp/bin/CIPPSharp.dll

512 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)