예제 #1
0
        public void Compare_Repos_July_vs_Today(string owner)
        {
            NotificationBuilder         nbuilder        = new NotificationBuilder();
            NotificationChannelsBuilder channelsBuilder = new NotificationChannelsBuilder().UseSmtpPickupDirectory(@"c:\work").UseSmtpPickupDirectory(@"c:\work2\send");

            List <RepositoryEvent> Events = new List <RepositoryEvent>();
            ITableSnapshotBuilder  builder;

            builder = RepositoryListSnapshotBuilder.CreateInMemorySnapshotFromRequest(Resources.GetRepositoryResponse(owner, RepositoryResponseGeneration.July));
            ITableSnapshot microsoftSnapshotJuly = builder.Build();

            builder = RepositoryListSnapshotBuilder.CreateInMemorySnapshotFromRequest(this.Extractor.GetMetadataAsynch(this.Token, owner).Result);
            ITableSnapshot microsoftSnapshotToday = builder.Build();

            TableDiffByLookup differ = new TableDiffByLookup(microsoftSnapshotJuly, microsoftSnapshotToday);


            differ.DifferencesDelegate = (deletedRecord, inserted) =>
            {
                EventType   et       = EventType.Created;
                IDataRecord template = inserted;
                if ((deletedRecord != null) && (inserted != null))
                {
                    et       = EventType.Modified;
                    template = inserted;
                }

                if ((deletedRecord != null) && (inserted == null))
                {
                    et       = EventType.Deleted;
                    template = deletedRecord;
                    //RepositoryTableSnapshot.CreatedAtFieldName
                }

                RepositoryEvent ev = EventFactory.CreateRepositoryEvent(template, et);
                nbuilder.AddEvent(ev);
            };
            differ.Execute();
            //Assert.Equal(1312, differ.OldSnapshotRecordCount);

            //create Notification
            //Deliver Notification

            nbuilder.AddChannels(channelsBuilder);

            List <Notification> toSend = new List <Notification>();

            for (int i = 0; i < 5; i++)
            {
                Notification noti = nbuilder.Build();
                noti.From = new NotificationAddress()
                {
                    Identifier = "*****@*****.**"
                };
                noti.To = new NotificationAddress[] { noti.From };
                toSend.Add(noti);
            }

            Postman.DeliverNotification(toSend);
        }
예제 #2
0
        public static int HandleWork <T>(IConsole console, WorkDescription currentWork, ILogger logger, Func <WorkDescription, string, IEvent[]> callback) where T : IEvent
        {
            Guard.ArgumentNotNull(currentWork, nameof(currentWork));
            Guard.ArgumentNotNull(logger, nameof(logger));
            Guard.ArgumentNotNull(currentWork.Channels, "currentWork.Channels");

            List <IEvent> FullList = new List <IEvent>();
            List <Task <Tuple <IEvent[], TimeSpan> > > TaskList = new List <Task <Tuple <IEvent[], TimeSpan> > >();
            List <Task> continuations = new List <Task>();

            foreach (string item in currentWork.Items)
            {
                lock (console)
                {
                    console.WriteLine(string.Format("  {0} Task started...", item), null, null);
                }

                Task <Tuple <IEvent[], TimeSpan> > task = Task.Factory.StartNew <Tuple <IEvent[], TimeSpan> >(() =>
                {
                    Stopwatch sw = Stopwatch.StartNew();
                    IEvent[] ev  = callback(currentWork, item);
                    sw.Stop();
                    return(new Tuple <IEvent[], TimeSpan>(ev, sw.Elapsed));
                });

                Task ct = task.ContinueWith((prec) =>
                {
                    lock (console)
                    {
                        console.WriteLine(string.Format("  {0} Task completed", item), null, null);
                        console.WriteLine(string.Format("    {0} Events identified in {1}ms", prec.Result.Item1.Length, Convert.ToInt64(prec.Result.Item2.TotalMilliseconds)), null, null);
                    }
                }, TaskContinuationOptions.NotOnFaulted);
                TaskList.Add(task);
                continuations.Add(ct);
            }

            Task.WaitAll(TaskList.ToArray());
            Task.WaitAll(continuations.ToArray());

            NotificationBuilder nbuilder = new NotificationBuilder().UseHtmlRenderer();

            //Date Hierarchy (Year/Month/Day) created by Blogger, we must only provide a meaningfull subject

            string itemsAsString = string.Join(",", currentWork.Items);

            nbuilder.UseSubject(string.Format("{0} {1}", itemsAsString, Pluralize(currentWork.ItemType.ToString())));

            foreach (Task <Tuple <IEvent[], TimeSpan> > t in TaskList)
            {
                nbuilder.AddEvents(t.Result.Item1);
            }

            if (nbuilder.HasEvents == false)
            {
                console.WriteLine("Now changes found", null, null);
                return(0); //NO NOTIFICATION required!
            }
            nbuilder.AddChannels(currentWork.Channels);
            Notification noti = nbuilder.Build();

            noti.From = currentWork.From;
            noti.To   = currentWork.To;

            Postman.DeliverNotification(new Notification[] { noti });
            console.WriteLine("", null, null);
            console.WriteLine(string.Format("{0} changes delivered", nbuilder.EventCount), null, null);
            return(0);
        }