public async Task RunAsync(SessionPreview preview)
        {
            var entry = runners.Read(l => l.Single(r => r.Runner.Id == preview.Id));
            await entry.Runner.RunAsync(preview, Printer);

            runners.Write(l => l.Remove(entry));
        }
Beispiel #2
0
 public Task <SessionPreview> PreviewAsync(bool includeNoneActionItems, IPrinter printer)
 {
     return(printer.IndentAsync($"Previewing synchronization session '{Session.Name}' {(Session.MakePreviewInParallel ? "in parallel" : "sequentially")}",
                                async() =>
     {
         var res = new SessionPreview(Session.Id);
         works = Session.Works.Select(d => new WorkRunner(d, printer)).ToList();
         List <WorkPreview> resTask = new List <WorkPreview>();
         if (Session.MakePreviewInParallel)
         {
             var tasks = works.Select(w => w.PreviewAsync(includeNoneActionItems, printer));
             resTask = (await Task.WhenAll(tasks)).ToList();
         }
         else
         {
             foreach (var work in works)
             {
                 resTask.Add(await work.PreviewAsync(includeNoneActionItems, printer));
             }
         }
         res.Works = resTask.Select(w =>
         {
             w.Session = res;
             return w;
         }).ToList();
         if (!includeNoneActionItems)
         {
             foreach (var work in res.Works.ToList())
             {
                 if (!work.Items.Any())
                 {
                     res.Works.Remove(work);
                 }
             }
         }
         return res;
     }));
 }
Beispiel #3
0
 public async Task <SessionPreview> PreviewAsync(bool includeNoneActionItems, IPrinter printer)
 {
     using (printer.Indent($"Previewing synchronization session '{Session.Name}' {(Session.MakePreviewInParallel ? "in parallel" : "sequentially")}"))
     {
         var res = new SessionPreview(Session.Id);
         works = Session.Works.Select(w => new WorkRunner(w, printer)).ToList();
         List <WorkPreview> resTask = new List <WorkPreview>();
         if (Session.MakePreviewInParallel)
         {
             var tasks = works.Select(w => w.PreviewAsync(printer));
             resTask = (await Task.WhenAll(tasks)).ToList();
         }
         else
         {
             foreach (var work in works)
             {
                 resTask.Add(await work.PreviewAsync(printer));
             }
         }
         res.Works = resTask.Select(w =>
         {
             w.Session = res;
             return(w);
         }).ToList();
         // Run post preview actions
         foreach (var work in works.Where(w => w.Definition.PostPreviewAction != null))
         {
             work.Definition.PostPreviewAction(res);
         }
         // Clean results
         if (!includeNoneActionItems)
         {
             res.CleanNoneActions();
         }
         return(res);
     }
 }
Beispiel #4
0
        public Task RunAsync(SessionPreview preview, IPrinter printer)
        {
            return(printer.IndentAsync("Running session:", async() =>
            {
                // 1 - Insert 1º level
                // 2 - Update 1º level

                // 3 - Insert 2º level
                // 4 - Update 2º level
                // . - ...............
                // 5 - Insert nº level
                // 6 - Update nº level

                // 7 - Delete nº level
                // . - ...............
                // 8 - Delete 2º level
                // 9 - Delete 1º level

                List <Task> tasks = new List <Task>();
                List <Tuple <ICollection <ItemPreview>, WorkRunner> > main = new List <Tuple <ICollection <ItemPreview>, WorkRunner> >();
                List <Tuple <ICollection <ItemRelationPreview>, IItemSideRunner> > levels = new List <Tuple <ICollection <ItemRelationPreview>, IItemSideRunner> >();

                foreach (var work in preview.Works)
                {
                    var runWork = works.Single(w => w.Id == work.Id);
                    main.Add(new Tuple <ICollection <ItemPreview>, WorkRunner>(work.Items, runWork));
                }
                await printer.ForeachAsync("Inserting level 0", main, async m =>
                {
                    levels.AddRange(await ProcessWork(m.Item1, m.Item2, SynchronizationAction.Insert));
                }, false);
                await printer.ForeachAsync("Updating level 0", main, async m =>
                {
                    levels.AddRange(await ProcessWork(m.Item1, m.Item2, SynchronizationAction.Update));
                }, false);
                levels = levels.Distinct().ToList();
                int level = 1;
                while (levels.Any())
                {
                    var aux = levels.ToList();
                    levels.Clear();
                    await printer.ForeachAsync($"Inserting level {level}", aux, async lev =>
                    {
                        levels.AddRange(await ProcessRelations(lev.Item1, lev.Item2, SynchronizationAction.Insert, level));
                    }, false);
                    await printer.ForeachAsync($"Updating level {level}", aux, async lev =>
                    {
                        levels.AddRange(await ProcessRelations(lev.Item1, lev.Item2, SynchronizationAction.Update, level));
                    }, false);
                    levels = levels.Distinct().ToList();
                    level++;
                }
                await printer.ForeachAsync("Deleting level 0", main, async m =>
                {
                    levels.AddRange(await ProcessWork(m.Item1, m.Item2, SynchronizationAction.Delete));
                }, false);

                level = 1;
                while (levels.Any())
                {
                    var aux = levels.ToList();
                    levels.Clear();
                    await printer.ForeachAsync($"Deleting level {level}", aux, async lev =>
                    {
                        levels.AddRange(await ProcessRelations(lev.Item1, lev.Item2, SynchronizationAction.Delete, level));
                    }, false);
                    level++;
                }
            }));
        }
Beispiel #5
0
        public async Task RunAsync(SessionPreview preview, IPrinter printer)
        {
            using (printer.Indent("Running session:"))
            {
                // Run order:

                // 1 - Insert 1º level
                // 2 - Update 1º level

                // 3 - Insert 2º level
                // 4 - Update 2º level
                // . - ...............
                // 5 - Insert nº level
                // 6 - Update nº level

                // 7 - Delete nº level
                // . - ...............
                // 8 - Delete 2º level
                // 9 - Delete 1º level

                var main = new List <(IList <ItemPreview> Items, WorkRunner Runner)>(
                    preview.Works.Select(work => (work.Items, works.Single(w => w.Definition.Id == work.Id))));
                var nextLevels = new List <(ICollection <ItemRelationPreview> Items, IItemSideRunner Runner)>();

                await printer.ForeachAsync("Inserting level 0", main,
                                           async m => nextLevels.AddRange(await ProcessWork(m.Items, m.Runner, SynchronizationAction.Insert)
                                                                          .Transform(async item =>
                {
                    await item.ProcessAction();
                    return(item.NextLevels);
                })),
                                           false);

                await printer.ForeachAsync("Updating level 0", main,
                                           async m => nextLevels.AddRange(await ProcessWork(m.Items, m.Runner, SynchronizationAction.Update)
                                                                          .Transform(async item =>
                {
                    await item.ProcessAction();
                    return(item.NextLevels);
                })),
                                           false);

                nextLevels = nextLevels.Distinct().ToList();
                int level = 1;
                while (nextLevels.Any())
                {
                    var aux = nextLevels.ToList();
                    nextLevels.Clear();
                    await printer.ForeachAsync($"Inserting level {level}", aux,
                                               async lev => nextLevels.AddRange(await ProcessRelations(lev.Items, lev.Runner, SynchronizationAction.Insert)
                                                                                .Transform(async item =>
                    {
                        await item.ProcessAction();
                        return(item.NextLevels);
                    })),
                                               false);

                    await printer.ForeachAsync($"Updating level {level}", aux,
                                               async lev => nextLevels.AddRange(await ProcessRelations(lev.Items, lev.Runner, SynchronizationAction.Update)
                                                                                .Transform(async item =>
                    {
                        await item.ProcessAction();
                        return(item.NextLevels);
                    })),
                                               false);

                    nextLevels = nextLevels.Distinct().ToList();
                    level++;
                }
                level = 0;
                var processActions = new List <(int Level, Func <Task> Action)>();
                foreach (var m in main)
                {
                    nextLevels.AddRange(ProcessWork(m.Items, m.Runner, SynchronizationAction.Delete).Transform(item =>
                    {
                        processActions.Add((level, item.ProcessAction));
                        return(item.NextLevels);
                    }));
                }
                level++;
                while (nextLevels.Any())
                {
                    var aux = nextLevels.ToList();
                    nextLevels.Clear();
                    foreach (var lev in aux)
                    {
                        nextLevels.AddRange(ProcessRelations(lev.Items, lev.Runner, SynchronizationAction.Delete).Transform(item =>
                        {
                            processActions.Add((level, item.ProcessAction));
                            return(item.NextLevels);
                        }));
                    }
                    level++;
                }
                var lastLevel = -1;
                foreach (var act in processActions.OrderByDescending(a => a.Level))
                {
                    if (act.Level != lastLevel)
                    {
                        using (printer.Indent($"Deleting level {act.Level}"))
                            await act.Action();
                    }
                    else
                    {
                        await act.Action();
                    }
                    lastLevel = act.Level;
                }
                // Run post run actions
                foreach (var work in works.Where(w => w.Definition.PostRunAction != null))
                {
                    work.Definition.PostRunAction(preview);
                }
            }
        }