コード例 #1
0
 public async Task <TDataType> Read <TDataType>(int id) where TDataType : DataEntity
 {
     using (_logger.BeginScope("{Operation} is {Action} {DataType} with Id ({Id})", nameof(FileDataWorker),
                               "reading", typeof(TDataType).Name, id))
     {
         var filename    = GetFileName <TDataType>(id);
         var maxAttempts = MaxAttempts <TDataType>();
         _logger.LogTrace("Will try to read from {filename} {MaxAttempts} times", filename, maxAttempts);
         var attempts = 0;
         while (attempts < maxAttempts)
         {
             attempts++;
             _logger.LogTrace("Attempt number {Attempt}", attempts);
             if (await FileStreamer.GetLockForFile(filename))
             {
                 try
                 {
                     _logger.LogTrace("Got lock on file");
                     return(await FileStreamer.ReadDataFromStream <TDataType>(filename));
                 }
                 catch (Exception exception)
                 {
                     _logger.LogWarning(exception, "Failed to read data as an error happened");
                 }
             }
コード例 #2
0
        public async Task <AuditLog> ReadAllEvents <TDataType>(int id) where TDataType : DataEntity
        {
            using (_logger.BeginScope("{Operation} is {Action} for {DataType} with id ({id})", nameof(FileAuditWorker),
                                      "reading all events", typeof(TDataType).Name, id))
            {
                var filename = GetFileName <TDataType>(id);
                if (!await FileStreamer.Exists(filename))
                {
                    return(null);
                }
                var maxAttempts = MaxAttempts <TDataType>();
                _logger.LogTrace("Will try to write to {filename} {MaxAttempts} times", filename,
                                 maxAttempts >= 0 ? maxAttempts.ToString() : "until success");
                var attempts = 0;
                while (maxAttempts == -1 || attempts < maxAttempts)
                {
                    attempts++;
                    _logger.LogTrace("Attempt number {Attempt}", attempts);
                    if (await FileStreamer.GetLockForFile(filename))
                    {
                        try
                        {
                            var audit = await FileStreamer.ReadDataFromStream <AuditLog>(filename);

                            _logger.LogInformation("Read audit from disk");
                            return(audit);
                        }
                        catch (Exception exception)
                        {
                            _logger.LogWarning(exception, "Failed to read audit");
                        }
                    }
コード例 #3
0
        public async Task <bool> GetLockForFileWaitSuccess()
        {
            var result = FileStreamer.GetLockForFile(Filename);

            MockSemaphoreFactory.Verify(x => x.Create(1, 1), Times.Once);
            return(await result);
        }
コード例 #4
0
        public async Task UnlockFileWhenThereIsALock()
        {
            await FileStreamer.GetLockForFile(Filename);

            await FileStreamer.UnlockFile(Filename);

            MockSemaphoreFactory.Verify(x => x.Create(1, 1), Times.Once);
            MockSemaphore.Verify(x => x.Release(), Times.Once);
        }
コード例 #5
0
        public async Task <bool> GetLockForFileWaitFails()
        {
            MockSemaphore
            .Setup(x => x.Wait(It.Is <TimeSpan>(y => Math.Abs(y.TotalMinutes - 1) < 1)))
            .Returns(false);
            var result = FileStreamer.GetLockForFile(Filename);

            MockSemaphoreFactory.Verify(x => x.Create(1, 1), Times.Once);
            return(await result);
        }
コード例 #6
0
        public async Task <bool> Write <TDataType>(int id, TDataType data) where TDataType : DataEntity
        {
            using (_logger.BeginScope("{Operation} is {Action} {DataType} with Id ({Id})", nameof(FileDataWorker),
                                      "writing", typeof(TDataType).Name, id))
            {
                var filename    = GetFileName <TDataType>(id);
                var maxAttempts = MaxAttempts <TDataType>();
                _logger.LogTrace("Will try to write to {filename} {MaxAttempts} times", filename, maxAttempts);
                var attempts = 0;
                while (attempts < maxAttempts)
                {
                    attempts++;
                    _logger.LogTrace("Attempt number {Attempt}", attempts);
                    if (await FileStreamer.GetLockForFile(filename))
                    {
                        try
                        {
                            _logger.LogTrace("Got a lock on the file");
                            if (await FileStreamer.WriteDataToStream(filename, data))
                            {
                                _logger.LogInformation("Wrote the data to the file system");
                                return(true);
                            }

                            _logger.LogWarning("Failed to write data to the file system");
                            await FileStreamer.UnlockFile(filename);
                        }
                        catch (Exception exception)
                        {
                            _logger.LogWarning(exception, "While writing data to the file system an error happened");
                            await FileStreamer.UnlockFile(filename);
                        }
                    }
                }

                _logger.LogError("Failed to write data to the file system");
                return(false);
            }
        }