Beispiel #1
0
        public override int ExecuteAndReturn()
        {
            Guard.AssertNotNull(this.CurrentWork);
            return(HandleWork <RepositoryEvent>(this.Console, this.CurrentWork, this.Logger, (w, i) =>
            {
                string key = w.GetKey(i);

                return CreateEvents(key, (sf) =>
                {
                    return RepositoryListSnapshotBuilder.CreateFileSnapshotFromRequest(sf, ExtractRepositoriesByOwner(w.AccessToken, i));
                }
                                    , (sf2) =>
                {
                    bool exists = w.Repository.TryPull(key, sf2);
                    this.Logger.LogTrace("GetLastSnapshot.SnapshotFilename={0} (Exists={1})", key, exists);
                    if (exists)
                    {
                        ITableSnapshotBuilder builder = RepositoryListSnapshotBuilder.CreateFromFile(sf2);
                        return builder;
                    }
                    else
                    {
                        return RepositoryListSnapshotBuilder.EmptySnapshot();
                    }
                }
                                    , w.Repository, this.PersistLastSnapshot, this.Logger, (dr, et)
                                    => EventFactory.CreateRepositoryEvent(dr, et)
                                    );
            }));
        }
        public static IEvent[] CreateEvents(string key, Func <string, ITableSnapshotBuilder> currentQuerySnapshotBuilderDelegate, Func <string, ITableSnapshotBuilder> lastSnapshotBuilderDelegate, SnapshotRepository snapshotRepository, bool persistAsLastSnapshot, ILogger logger, Func <IDataRecord, EventType, IEvent> factoryCallback)
        {
            Guard.ArgumentNotNullOrEmptyString(key, nameof(key));
            Guard.ArgumentNotNull(currentQuerySnapshotBuilderDelegate, nameof(currentQuerySnapshotBuilderDelegate));
            Guard.ArgumentNotNull(lastSnapshotBuilderDelegate, nameof(lastSnapshotBuilderDelegate));
            Guard.AssertNotNull(logger);
            Guard.AssertNotNull(snapshotRepository);
            List <IEvent> events = new List <IEvent>();

            using (DisposableFile temporarySnapshot = DisposableFile.GetTempFile())
            {
                Task <ITableSnapshot> queryNow = Task.Factory.StartNew <ITableSnapshot>(() =>
                {
                    ITableSnapshotBuilder builder      = currentQuerySnapshotBuilderDelegate(temporarySnapshot.Path);
                    ITableSnapshot snapShotFromRequest = builder.Build();
                    return(snapShotFromRequest);
                });

                Task <ITableSnapshot> lastSnapshot1 = Task.Factory.StartNew <ITableSnapshot>(() =>
                {
                    string localTempFile               = Path.GetTempFileName(); //DELETE The File at the end ??
                    ITableSnapshotBuilder builder      = lastSnapshotBuilderDelegate(localTempFile);
                    ITableSnapshot snapShotFromRequest = builder.Build();
                    return(snapShotFromRequest);
                });


                int msTimeoutLoggingSteps = 1000;
                while (Task.WaitAll(new Task[] { queryNow, lastSnapshot1 }, msTimeoutLoggingSteps) == false)
                {
                    logger.LogTrace("CreateEvents.Wait-Loop.{0}ms for '{1}'", msTimeoutLoggingSteps, key);
                }


                try
                {
                    TableDiffByLookup differ = new TableDiffByLookup(lastSnapshot1.Result, queryNow.Result);
                    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
                        }
                        if (et != EventType.Modified)
                        {
                            //IEvent ev = //EventFactory.CreateRepositoryEvent(template, et);
                            IEvent ev = factoryCallback(template, et);
                            Guard.AssertNotNull(ev);
                            events.Add(ev);
                        }
                        else
                        {
                            // we ignore modifications!
                        }
                    };
                    differ.Execute();
                }
                finally
                {
                    if (queryNow.IsCompletedSuccessfully())
                    {
                        queryNow.Result.Dispose();
                    }
                    if (lastSnapshot1.IsCompletedSuccessfully())
                    {
                        lastSnapshot1.Result.Dispose();
                    }
                }
                if (persistAsLastSnapshot)
                {
                    snapshotRepository.Push(key, temporarySnapshot.Path);
                }
                else
                {
                    logger.LogInformation("Snapshot {0} NOT persited!", key);
                }
            } //using temporary snapshotfile disposable

            return(events.ToArray());
            //throw new NotImplementedException();
        }