Ejemplo n.º 1
0
 public static DataImportStatus GetFinishingStateOfAbortProcess(this DataImportStatus status)
 {
     return(status switch
     {
         CANCELLING => CANCELLED,
         _ => throw new ArgumentOutOfRangeException($"No final abort state exists for state {status}")
     });
        public async Task UpdateStatus(Guid id, DataImportStatus newStatus, double percentageComplete)
        {
            await using var contentDbContext = new ContentDbContext(_contentDbContextOptions);
            await ExecuteWithExclusiveLock(
                contentDbContext,
                $"LockForImport-{id}",
                async context =>
            {
                var import = await context.DataImports
                             .Include(i => i.File)
                             .SingleAsync(i => i.Id == id);

                var filename = import.File.Filename;

                var percentageCompleteBefore = import.StagePercentageComplete;
                var percentageCompleteAfter  = (int)Math.Clamp(percentageComplete, 0, 100);

                // Ignore updating if already finished
                if (import.Status.IsFinished())
                {
                    _logger.LogWarning(
                        $"Update: {filename} {import.Status} ({percentageCompleteBefore}%) -> " +
                        $"{newStatus} ({percentageCompleteAfter}%) ignored as this import is already finished");
                    return;
                }

                // Ignore updating if already aborting and the new state is not aborting or finishing
                if (import.Status.IsAborting() && !newStatus.IsFinishedOrAborting())
                {
                    _logger.LogWarning(
                        $"Update: {filename} {import.Status} ({percentageCompleteBefore}%) -> " +
                        $"{newStatus} ({percentageCompleteAfter}%) ignored as this import is already aborting or is finished");
                    return;
                }

                // Ignore updates if attempting to downgrade from a normal importing state to a lower normal importing state,
                // or if the percentage is being set lower or the same as is currently and is the same state
                if (!newStatus.IsFinishedOrAborting() &&
                    (import.Status.CompareTo(newStatus) > 0 ||
                     import.Status == newStatus && percentageCompleteBefore > percentageCompleteAfter))
                {
                    _logger.LogWarning(
                        $"Update: {filename} {import.Status} ({percentageCompleteBefore}%) -> " +
                        $"{newStatus} ({percentageCompleteAfter}%) ignored");
                    return;
                }

                // Ignore updating to an equal percentage complete (after rounding) at the same status without logging it
                if (import.Status == newStatus && percentageCompleteBefore == percentageCompleteAfter)
                {
                    return;
                }

                _logger.LogInformation(
                    $"Update: {filename} {import.Status} ({percentageCompleteBefore}%) -> {newStatus} ({percentageCompleteAfter}%)");

                import.StagePercentageComplete = percentageCompleteAfter;
                import.Status = newStatus;
                context.DataImports.Update(import);
                await context.SaveChangesAsync();
            }
                );
        }