~ruther/NosSmooth.Local

b68acbc97dbf91b32804757b1c27fe644718dd7d — Rutherther 2 years ago efd90a9
feat(samples): add file manager initialization retries in SimplePiiBot
1 files changed, 20 insertions(+), 1 deletions(-)

M src/Samples/HighLevel/SimplePiiBot/HostedService.cs
M src/Samples/HighLevel/SimplePiiBot/HostedService.cs => src/Samples/HighLevel/SimplePiiBot/HostedService.cs +20 -1
@@ 13,6 13,8 @@ using NosSmooth.Data.NOSFiles;
using NosSmooth.LocalBinding;
using NosSmooth.Packets.Extensions;
using NosSmooth.Packets.Packets;
using OneOf.Types;
using Remora.Results;

namespace SimplePiiBot;



@@ 65,7 67,7 @@ public class HostedService : BackgroundService
            return;
        }

        var filesResult = _filesManager.Initialize();
        var filesResult = InitializeFileManager();
        if (!filesResult.IsSuccess)
        {
            _logger.LogResultError(filesResult);


@@ 86,4 88,21 @@ public class HostedService : BackgroundService
            await _lifetime.StopAsync(default);
        }
    }

    private int _maxRetries = 10;

    private Result InitializeFileManager()
    {
        var filesResult = _filesManager.Initialize();

        if (_maxRetries-- > 0 && !filesResult.IsSuccess && filesResult.Error is ExceptionError exceptionError
            && exceptionError.Exception is IOException ioException && ioException.HResult == -2147024864)
        { // could not load files, the NosTale may be just starting an using .NOS files, retry few times.
            _logger.LogWarning($"Could not obtain .NOS files. Going to retry. {ioException.Message}");
            Thread.Sleep(1000);
            return InitializeFileManager();
        }

        return filesResult;
    }
}
\ No newline at end of file

Do not follow this link