TenantAtlas/app/Services/Operations/TargetScopeConcurrencyLimiter.php
ahmido a97beefda3 056-remove-legacy-bulkops (#65)
Kurzbeschreibung

Versteckt die Rerun-Row-Action für archivierte (soft-deleted) RestoreRuns und verhindert damit fehlerhafte Neu-Starts aus dem Archiv; ergänzt einen Regressionstest.
Änderungen

Code: RestoreRunResource.php — Sichtbarkeit der rerun-Action geprüft auf ! $record->trashed() und defensive Abbruchprüfung im Action-Handler.
Tests: RestoreRunRerunTest.php — neuer Test rerun action is hidden for archived restore runs.
Warum

Archivierte RestoreRuns durften nicht neu gestartet werden; UI zeigte trotzdem die Option. Das führte zu verwirrendem Verhalten und möglichen Fehlern beim Enqueueing.
Verifikation / QA

Unit/Feature:
./vendor/bin/sail artisan test tests/Feature/RestoreRunRerunTest.php
Stil/format:
./vendor/bin/pint --dirty
Manuell (UI):
Als Tenant-Admin Filament → Restore Runs öffnen.
Filter Archived aktivieren (oder Trashed filter auswählen).
Sicherstellen, dass für archivierte Einträge die Rerun-Action nicht sichtbar ist.
Auf einem aktiven (nicht-archivierten) Run prüfen, dass Rerun sichtbar bleibt und wie erwartet eine neue RestoreRun erzeugt.
Wichtige Hinweise

Kein DB-Migration required.
Diese PR enthält nur den UI-/Filament-Fix; die zuvor gemachten operative Fixes für Queue/adapter-Reconciliation bleiben ebenfalls auf dem Branch (z. B. frühere commits während der Debugging-Session).
T055 (Schema squash) wurde bewusst zurückgestellt und ist nicht Teil dieses PRs.
Merge-Checklist

 Tests lokal laufen (RestoreRunRerunTest grünt)
 Pint läuft ohne ungepatchte Fehler
 Branch gepusht: 056-remove-legacy-bulkops (PR-URL: https://git.cloudarix.de/ahmido/TenantAtlas/compare/dev...056-remove-legacy-bulkops)

Co-authored-by: Ahmed Darrazi <ahmeddarrazi@adsmac.local>
Reviewed-on: #65
2026-01-19 23:27:52 +00:00

73 lines
2.2 KiB
PHP

<?php
namespace App\Services\Operations;
use Illuminate\Contracts\Cache\Lock;
use Illuminate\Support\Facades\Cache;
use InvalidArgumentException;
final class TargetScopeConcurrencyLimiter
{
public function __construct(private readonly int $lockTtlSeconds = 900) {}
/**
* Acquire a concurrency slot for a given tenant + target scope.
*
* Returns a held lock when a slot is available, otherwise null.
*
* @param array{entra_tenant_id?: mixed, directory_context_id?: mixed} $targetScope
*/
public function acquireSlot(int $tenantId, array $targetScope): ?Lock
{
$max = (int) config('tenantpilot.bulk_operations.concurrency.per_target_scope_max', 1);
$max = max(0, $max);
if ($max === 0) {
return null;
}
$scopeKey = $this->scopeKey($targetScope);
return $this->acquireSlotInternal("bulk_ops:tenant:{$tenantId}:scope:{$scopeKey}:slot:", $max);
}
private function acquireSlotInternal(string $prefix, int $max): ?Lock
{
$ttlSeconds = (int) config('tenantpilot.bulk_operations.concurrency.lock_ttl_seconds', $this->lockTtlSeconds);
$ttlSeconds = max(1, $ttlSeconds);
for ($slot = 0; $slot < $max; $slot++) {
$lock = Cache::lock($prefix.$slot, $ttlSeconds);
if ($lock->get()) {
return $lock;
}
}
return null;
}
/**
* @param array{entra_tenant_id?: mixed, directory_context_id?: mixed} $targetScope
*/
private function scopeKey(array $targetScope): string
{
$entraTenantId = $targetScope['entra_tenant_id'] ?? null;
$directoryContextId = $targetScope['directory_context_id'] ?? null;
if (is_string($entraTenantId) && trim($entraTenantId) !== '') {
return 'entra:'.trim($entraTenantId);
}
if (is_string($directoryContextId) && trim($directoryContextId) !== '') {
return 'directory_context:'.trim($directoryContextId);
}
if (is_int($directoryContextId)) {
return 'directory_context:'.$directoryContextId;
}
throw new InvalidArgumentException('Target scope must include entra_tenant_id or directory_context_id.');
}
}