Repository: microsoft/onefuzz
File: src/ApiService/ApiService/onefuzzlib/ScalesetOperations.cs (C#)

Code before:
        var strategy = await _context.FeatureManagerSnapshot.IsEnabledAsync(FeatureFlagConstants.EnableNodeDecommissionStrategy) ? NodeDisposalStrategy.Decommission : NodeDisposalStrategy.ScaleIn;

        var reimageNodes = await ReimageNodes(scaleSet, toReimage.Values, strategy);
        if (!reimageNodes.IsOk) {
            _log.Warning(reimageNodes.ErrorV);
            return (false, scaleSet);
        }
        var deleteNodes = await DeleteNodes(scaleSet, toDelete.Values, "Node was ReadyForReset");
        if (!deleteNodes.IsOk) {
            _log.Warning(deleteNodes.ErrorV);
            return (toReimage.Count > 0, scaleSet);
        }

        return (toReimage.Count > 0 || toDelete.Count > 0, scaleSet);
    }


    public async Async.Task<OneFuzzResultVoid> ReimageNodes(Scaleset scaleset, IEnumerable<Node> nodes, NodeDisposalStrategy disposalStrategy) {
        if (nodes is null || !nodes.Any()) {
            _log.Info($"no nodes to reimage: {scaleset.ScalesetId:Tag:ScalesetId}");
            return OneFuzzResultVoid.Ok;
        }

        if (scaleset.State == ScalesetState.Shutdown) {
            _log.Info($"scaleset shutting down, deleting rather than reimaging nodes {scaleset.ScalesetId:Tag:ScalesetId}");
            return await DeleteNodes(scaleset, nodes, "node is shutting down");
        }

        if (scaleset.State == ScalesetState.Halt) {
            _log.Info($"scaleset halting, ignoring node reimage {scaleset.ScalesetId:Tag:ScalesetId}");
            return OneFuzzResultVoid.Ok;
        }

        var nodesToReimage = new List<Node>();
        foreach (var node in nodes) {
            if (node.State != NodeState.Done) {
                continue;
            }

            if (node.DebugKeepNode) {
                _log.Warning($"not reimaging manually overridden node {node.MachineId:Tag:MachineId} in scaleset {scaleset.ScalesetId:Tag:ScalesetId}");
            } else {
                nodesToReimage.Add(node);
            }
        }

        if (!nodesToReimage.Any()) {
            _log.Info($"no nodes to reimage {scaleset.ScalesetId:Tag:ScalesetId}");
            return OneFuzzResultVoid.Ok;
        }


Review comment (suggestion):
```suggestion
            return await DeleteNodes(scaleset, nodes, "scaleset is shutting down");
```

Code after:
        var strategy = await _context.FeatureManagerSnapshot.IsEnabledAsync(FeatureFlagConstants.EnableNodeDecommissionStrategy) ? NodeDisposalStrategy.Decommission : NodeDisposalStrategy.ScaleIn;

        var reimageNodes = await ReimageNodes(scaleSet, toReimage.Values, strategy);
        if (!reimageNodes.IsOk) {
            _log.Warning(reimageNodes.ErrorV);
            return (false, scaleSet);
        }
        var deleteNodes = await DeleteNodes(scaleSet, toDelete.Values, "Node was ReadyForReset");
        if (!deleteNodes.IsOk) {
            _log.Warning(deleteNodes.ErrorV);
            return (toReimage.Count > 0, scaleSet);
        }

        return (toReimage.Count > 0 || toDelete.Count > 0, scaleSet);
    }


    public async Async.Task<OneFuzzResultVoid> ReimageNodes(Scaleset scaleset, IEnumerable<Node> nodes, NodeDisposalStrategy disposalStrategy) {
        if (nodes is null || !nodes.Any()) {
            _log.Info($"no nodes to reimage: {scaleset.ScalesetId:Tag:ScalesetId}");
            return OneFuzzResultVoid.Ok;
        }

        if (scaleset.State == ScalesetState.Shutdown) {
            _log.Info($"scaleset shutting down, deleting rather than reimaging nodes {scaleset.ScalesetId:Tag:ScalesetId}");
            return await DeleteNodes(scaleset, nodes, "scaleset is shutting down");
        }

        if (scaleset.State == ScalesetState.Halt) {
            _log.Info($"scaleset halting, ignoring node reimage {scaleset.ScalesetId:Tag:ScalesetId}");
            return OneFuzzResultVoid.Ok;
        }

        var nodesToReimage = new List<Node>();
        foreach (var node in nodes) {
            if (node.State != NodeState.Done) {
                continue;
            }

            if (node.DebugKeepNode) {
                _log.Warning($"not reimaging manually overridden node {node.MachineId:Tag:MachineId} in scaleset {scaleset.ScalesetId:Tag:ScalesetId}");
            } else {
                nodesToReimage.Add(node);
            }
        }

        if (!nodesToReimage.Any()) {
            _log.Info($"no nodes to reimage {scaleset.ScalesetId:Tag:ScalesetId}");
            return OneFuzzResultVoid.Ok;
        }