public async Task ProcessTasksAsync(ICollection <ThumbnailTask> tasks, bool regenerate, Action <ThumbnailTaskProgress> progressCallback, ICancellationToken token)
        {
            var progressInfo = new ThumbnailTaskProgress {
                Message = "Reading the tasks..."
            };

            if (_imageChangesProvider.IsTotalCountSupported)
            {
                foreach (var task in tasks)
                {
                    var changedSince = regenerate ? null : task.LastRun;
                    progressInfo.TotalCount = await _imageChangesProvider.GetTotalChangesCount(task, changedSince, token);
                }
            }

            progressCallback(progressInfo);

            var pageSize = _settingsManager.GetValue(ModuleConstants.Settings.General.ProcessBatchSize.Name, 50);

            foreach (var task in tasks)
            {
                progressInfo.Message = $"Procesing task {task.Name}...";
                progressCallback(progressInfo);

                var skip = 0;
                while (true)
                {
                    var changes = await _imageChangesProvider.GetNextChangesBatch(task, regenerate?null : task.LastRun, skip, pageSize, token);

                    if (!changes.Any())
                    {
                        break;
                    }

                    foreach (var fileChange in changes)
                    {
                        var result = await _generator.GenerateThumbnailsAsync(fileChange.Url, task.WorkPath, task.ThumbnailOptions, token);

                        progressInfo.ProcessedCount++;

                        if (!result.Errors.IsNullOrEmpty())
                        {
                            progressInfo.Errors.AddRange(result.Errors);
                        }
                    }

                    skip += changes.Length;

                    progressCallback(progressInfo);
                    token?.ThrowIfCancellationRequested();
                }
            }

            progressInfo.Message = "Finished generating thumbnails!";
            progressCallback(progressInfo);
        }
Beispiel #2
0
        private async Task PerformGeneration(IEnumerable <ThumbnailTask> tasks, bool regenerate, Action <ThumbnailTaskProgress> progressCallback, IJobCancellationToken cancellationToken)
        {
            var cancellationTokenWrapper = new JobCancellationTokenWrapper(cancellationToken);

            foreach (var task in tasks)
            {
                // Better to run and save tasks one by one to save LastRun date once every task is completed, opposing to waiting all tasks completion, as it could be a long process.
                var oneTaskArray = new[] { task };
                //Need to save runTime at start in order to not loose changes that may be done between the moment of getting changes and the task completion.
                var runTime = DateTime.UtcNow;

                await _thumbnailProcessor.ProcessTasksAsync(oneTaskArray, regenerate, progressCallback, cancellationTokenWrapper);

                task.LastRun = runTime;

                await _taskService.SaveChangesAsync(oneTaskArray);
            }

            var progressInfo = new ThumbnailTaskProgress {
                Message = "Finished generating thumbnails!"
            };

            progressCallback(progressInfo);
        }