public ITaskProgressionInfo Post(IEnumerable <Tentity> entities, IScheduler scheduler = null, int?patchSize = null) { var progInfo = new TaskProgression(); var chunks = entities.ChunkBy(patchSize ?? settingsService.MaxPatchSize) .ToList(); progInfo.RequiredProgress = chunks.Count; Observable.Start(() => { progInfo.State = TaskState.InProgress; try { progInfo.Details = "posting ..."; chunks.ForEach(x => { this.cache.AddOrUpdate(x); progInfo.CurrentProgress++; }); progInfo.State = TaskState.Done; progInfo.Details = "done"; } catch (Exception ex) { progInfo.Error = ex; progInfo.Details = ex.ToString(); progInfo.State = TaskState.Failed; } }, scheduler ?? Scheduler.CurrentThread); return(progInfo); }
public static IEnumerable <IObservable <IEnumerable <Tout> > > SelectAsync <Tin, Tout>(this IEnumerable <Tin> list, Func <Tin, Tout> selector, int chunkSize, out ITaskProgressionInfo progress) { var prog = new TaskProgression() { State = TaskState.Todo }; var chunkCount = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(list.Count()) / chunkSize)); var res = getSubjectList <IEnumerable <Tout> >(chunkCount); progress = prog; Observable.Start(() => { try { prog.State = TaskState.InProgress; var chunks = list.ChunkBy(chunkSize).ToArray(); prog.RequiredProgress = chunks.Length; var i = 0; foreach (var chunk in chunks) { res.ElementAt(i).OnNext(chunk.Select(selector).ToArray()); prog.CurrentProgress++; i++; } prog.State = TaskState.Done; } catch (Exception ex) { prog.Details = "failed: " + ex.ToString(); prog.Error = ex; prog.State = TaskState.Failed; } }, RxApp.TaskpoolScheduler); return(res); }
public IEnumerable <ITaskProgressionInfo> CreateAndLinkThumbnail() { return(this.itemService .GetExtended() .Items .Where(item => !item.LatestThumbnail.HasValue && item.LatestVersionedFile != null ) .SplitEvenly(settingsService.ThreadCount) .Select(chunk => { TaskProgression prog = new TaskProgression(); prog.Title = "Creating Thumbnails"; prog.RequiredProgress = chunk.Count(); Observable.Start(() => { prog.State = TaskState.InProgress; var chunkResult = chunk.Select(item => { FileInfo file = default; FileItemLinkChangeset linkCs = null; FailedThumbnailResult?err = null; var imgPath = item.GenerateAbsoluteThumbnailPath(); var itemPath = item.LatestFilePath; try { this.CreateAndLinkThumbnail(item, fs => { createThumbnail(itemPath, fs); }, out file, out linkCs); } catch (Exception ex) { err = new FailedThumbnailResult(ex, itemPath, imgPath); prog.State = TaskState.RunningWithErrors; } prog.CurrentProgress++; return new { file, linkCs, err }; }).ToArray(); var groupedResult = chunkResult.GroupBy(data => data.err.HasValue); var successes = groupedResult.FirstOrDefault(x => !x.Key)?.Where(x => x != null); var errors = groupedResult.FirstOrDefault(x => x.Key)?.Select(x => x.err.Value); if (prog.State == TaskState.RunningWithErrors) { prog.State = successes?.Any() == true ? TaskState.PartialSuccess : TaskState.Failed; } else { prog.State = TaskState.Done; } if (errors?.Any() == true) { var eGroups = errors.GroupBy(e => e.Exception.GetType().FullName); string msg = "the following Thumbnails could not be created"; foreach (var eGroup in eGroups) { msg += string.Join(Environment.NewLine, new string[] { "", "--------------------------------", eGroup.Key, "3dFile / thumbnail", string.Join(Environment.NewLine, eGroup.Select(e => $"{e.FilePath} / {e.ThumbnailPath}")) }); } prog.Error = new Exception(msg); } }, RxApp.TaskpoolScheduler); return prog as ITaskProgressionInfo; }) .ToArray()); }