private void InvokeCleanupIfNeeded()

in src/Covi/Features/BluetoothTracing/DeviceManager/Internal/DefaultDeviceManager.cs [39:85]


        private void InvokeCleanupIfNeeded()
        {
            if (!((DateTime.UtcNow - _lastCleanupTime) <= Configuration.Constants.BluetoothConstants.DeviceThrottleTime))
            {
                return;
            }

            Task.Run(() =>
            {
                if (!Monitor.TryEnter(_cleanupLock))
                {
                    return;
                }

                try
                {
                    if (!((DateTime.UtcNow - _lastCleanupTime) <= Configuration.Constants.BluetoothConstants.DeviceThrottleTime))
                    {
                        return;
                    }

                    _logger.LogDebug($"DeviceManager - Device cleanup started.");

                    var devices = _discoveredDevices.Values.ToList();
                    foreach (var device in devices)
                    {
                        if (_discoveredDevices.TryGetValue(device.Identifier, out var descriptor))
                        {
                            if (DateTime.UtcNow - descriptor.DiscoveryTime >= Configuration.Constants.BluetoothConstants.DeviceThrottleTime)
                            {
                                _discoveredDevices.TryRemove(device.Identifier, out var _);
                            }
                        }
                    }

                    _lastCleanupTime = DateTime.UtcNow;
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, $"DeviceManager - Device cleanup failed.");
                }
                finally
                {
                    Monitor.Exit(_cleanupLock);
                }
            }).FireAndForget();
        }