public async Task Process(ThumbnailsTaskRunRequest generateRequest, ThumbnailProcessNotification notifyEvent, IJobCancellationToken cancellationToken, PerformContext context) { try { Action <ThumbnailTaskProgress> progressCallback = x => { notifyEvent.Description = x.Message; notifyEvent.Errors = x.Errors; notifyEvent.ErrorCount = notifyEvent.Errors.Count; notifyEvent.TotalCount = x.TotalCount ?? 0; notifyEvent.ProcessedCount = x.ProcessedCount ?? 0; notifyEvent.JobId = context.BackgroundJob.Id; _pushNotifier.Upsert(notifyEvent); }; //wrap token var tasks = _taskService.GetByIds(generateRequest.TaskIds); var cancellationTokenWrapper = new JobCancellationTokenWrapper(cancellationToken); await _thumbnailProcessor.ProcessTasksAsync(tasks, generateRequest.Regenerate, progressCallback, cancellationTokenWrapper); //update tasks in case of successful generation foreach (var task in tasks) { task.LastRun = DateTime.UtcNow; } _taskService.SaveOrUpdate(tasks); } catch (JobAbortedException) { //do nothing } catch (Exception ex) { notifyEvent.Description = "Error"; notifyEvent.ErrorCount++; notifyEvent.Errors.Add(ex.ToString()); } finally { notifyEvent.Finished = DateTime.UtcNow; notifyEvent.Description = "Process finished" + (notifyEvent.Errors.Any() ? " with errors" : " successfully"); _pushNotifier.Upsert(notifyEvent); } }
public async Task ProcessAll(IJobCancellationToken cancellationToken) { var totalCount = _taskSearchService.Search(new ThumbnailTaskSearchCriteria() { Take = 0, Skip = 0 }).TotalCount; var tasks = _taskSearchService.Search(new ThumbnailTaskSearchCriteria() { Take = totalCount, Skip = 0 }).Results.ToArray(); Action <ThumbnailTaskProgress> progressCallback = x => { }; var cancellationTokenWrapper = new JobCancellationTokenWrapper(cancellationToken); await _thumbnailProcessor.ProcessTasksAsync(tasks, false, progressCallback, cancellationTokenWrapper); //update tasks in case of successful generation foreach (var task in tasks) { task.LastRun = DateTime.UtcNow; } _taskService.SaveOrUpdate(tasks); }
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); }