Repository: microsoft/onefuzz
File: src/ApiService/ApiService/Functions/TimerProxy.cs (C#)

Code before:
        foreach (var p in proxies) {
            var proxy = p;

            if (VmStateHelper.Available.Contains(proxy.State)) {
                // Note, outdated checked at the start, but set at the end of this loop.
                // As this function is called via a timer, this works around a user
                // requesting to use the proxy while this function is checking if it's
                // out of date
                if (proxy.Outdated && !(await _context.ProxyOperations.IsUsed(proxy))) {
                    _logger.Warning($"scaleset-proxy: outdated and not used: {proxy.Region}");
                    await proxyOperations.SetState(proxy, VmState.Stopping);
                    // If something is "wrong" with a proxy, delete & recreate it
                } else if (!proxyOperations.IsAlive(proxy)) {
                    _logger.Error($"scaleset-proxy: alive check failed, stopping: {proxy.Region}");
                    await proxyOperations.SetState(proxy, VmState.Stopping);
                } else {
                    await proxyOperations.SaveProxyConfig(proxy);
                }
            }

            if (VmStateHelper.NeedsWork.Contains(proxy.State)) {
                _logger.Info($"scaleset-proxy: update state. proxy:{proxy.Region} state:{proxy.State}");
                proxy = await proxyOperations.ProcessStateUpdate(proxy);
            }

            if (proxy is not null && (proxy.State != VmState.Stopped && proxyOperations.IsOutdated(proxy))) {
                var r = await proxyOperations.Replace(proxy with { Outdated = true });
                if (!r.IsOk) {
                    _logger.Error($"Failed to replace proxy recordy for proxy {proxy.ProxyId} due to {r.ErrorV}");
                }
            }
        }

        // make sure there is a proxy for every currently active region
        var regions = await scalesetOperations.QueryAsync().Select(x => x.Region).ToHashSetAsync();

        foreach (var region in regions) {
            var allOutdated = proxies.Where(x => x.Region == region).All(p => p.Outdated);
            if (allOutdated) {
                var proxy = await proxyOperations.GetOrCreate(region);
                _logger.Info($"Creating new proxy with id {proxy.ProxyId} in region {region}");
            }

            // this is required in order to support upgrade from non-nsg to
            // nsg enabled OneFuzz this will overwrite existing NSG
            // assignment though. This behavior is acceptable at this point
            // since we do not support bring your own NSG
            var nsgName = Nsg.NameFromRegion(region);

            if (await nsgOpertions.GetNsg(nsgName) != null) {
                var network = await Network.Init(region, _context);

Review comment (suggestion):
```suggestion
            if (proxy is not null && proxy.State != VmState.Stopped && proxyOperations.IsOutdated(proxy)) {
```

Code after:
        foreach (var p in proxies) {
            var proxy = p;

            if (VmStateHelper.Available.Contains(proxy.State)) {
                // Note, outdated checked at the start, but set at the end of this loop.
                // As this function is called via a timer, this works around a user
                // requesting to use the proxy while this function is checking if it's
                // out of date
                if (proxy.Outdated && !(await _context.ProxyOperations.IsUsed(proxy))) {
                    _logger.Warning($"scaleset-proxy: outdated and not used: {proxy.Region}");
                    await proxyOperations.SetState(proxy, VmState.Stopping);
                    // If something is "wrong" with a proxy, delete & recreate it
                } else if (!proxyOperations.IsAlive(proxy)) {
                    _logger.Error($"scaleset-proxy: alive check failed, stopping: {proxy.Region}");
                    await proxyOperations.SetState(proxy, VmState.Stopping);
                } else {
                    await proxyOperations.SaveProxyConfig(proxy);
                }
            }

            if (VmStateHelper.NeedsWork.Contains(proxy.State)) {
                _logger.Info($"scaleset-proxy: update state. proxy:{proxy.Region} state:{proxy.State}");
                proxy = await proxyOperations.ProcessStateUpdate(proxy);
            }

            if (proxy is not null && proxy.State != VmState.Stopped && proxyOperations.IsOutdated(proxy)) {
                var r = await proxyOperations.Replace(proxy with { Outdated = true });
                if (!r.IsOk) {
                    _logger.Error($"Failed to replace proxy recordy for proxy {proxy.ProxyId} due to {r.ErrorV}");
                }
            }
        }

        // make sure there is a proxy for every currently active region
        var regions = await scalesetOperations.QueryAsync().Select(x => x.Region).ToHashSetAsync();

        foreach (var region in regions) {
            var allOutdated = proxies.Where(x => x.Region == region).All(p => p.Outdated);
            if (allOutdated) {
                var proxy = await proxyOperations.GetOrCreate(region);
                _logger.Info($"Creating new proxy with id {proxy.ProxyId} in region {region}");
            }

            // this is required in order to support upgrade from non-nsg to
            // nsg enabled OneFuzz this will overwrite existing NSG
            // assignment though. This behavior is acceptable at this point
            // since we do not support bring your own NSG
            var nsgName = Nsg.NameFromRegion(region);

            if (await nsgOpertions.GetNsg(nsgName) != null) {
                var network = await Network.Init(region, _context);