public void when_wire_to_object_where_argument_is_interface() { var dynamicEvent = new RedirectToDynamicEvent(); var testClass = new TestClassWithInterfaceMethod(); dynamicEvent.WireToWhen(testClass); }
public void when_wire_to_object() { var dynamicEvent = new RedirectToDynamicEvent(); var testClass = new TestClassWithMethod(); dynamicEvent.WireToWhen(testClass); var t = new TestRedirectMethod { Id = 333, Name = "Name" }; dynamicEvent.InvokeEvent(t); Assert.AreEqual(t, testClass.RedirectMethod); }
public AssembledComponents AssembleComponents() { var documents = CreateNuclear(new DocumentStrategy()); var streamer = Streamer; var tapes = Tapes; var streaming = Streaming; var routerQueue = CreateQueueWriter(Topology.RouterQueue); var aggregates = new AggregateFactory(tapes, streamer, routerQueue); var sender = new SimpleMessageSender(streamer, routerQueue); var flow = new MessageSender(sender); var builder = new CqrsEngineBuilder(streamer); var log = tapes.GetOrCreateStream(Topology.DomainLogName); var dispatcher = new SimpleDispatcher(aggregates, streamer, log); builder.Handle(CreateInbox(Topology.RouterQueue), Topology.Route(CreateQueueWriter, streamer, tapes), "router"); builder.Handle(CreateInbox(Topology.EntityQueue), aggregates.Dispatch); var functions = new RedirectToDynamicEvent(); // documents //functions.WireToWhen(new RegistrationUniquenessProjection(atomic.Factory.GetEntityWriter<unit, RegistrationUniquenessDocument>())); // UI projections var projectionStore = CreateNuclear(new ProjectionStrategy()); foreach (var projection in BootstrapProjections.BuildProjectionsWithWhenConvention(projectionStore.Factory)) { functions.WireToWhen(projection); } // processes //functions.WireToWhen(new Domain.RecipeProcess(flow)); builder.Handle(CreateInbox(Topology.EventsQueue), aem => CallHandlers(functions, aem)); var timer = new StreamingTimerService(CreateQueueWriter(Topology.RouterQueue), streaming.GetContainer(Topology.FutureMessagesContainer), streamer); builder.Handle(CreateInbox(Topology.TimerQueue), timer.PutMessage); builder.AddProcess(timer); // GNS: just for this simple sample. In reality one would have some command handlers involved Bus.SetBus(new SimpleBus(sender, dispatcher)); return new AssembledComponents { Builder = builder, Sender = sender, Dispatcher = dispatcher, ProjectionFactory = projectionStore.Factory, Setup = this }; }
DirectoryInfo[] ObserveViews(ViewMapInfo[] handlers, DomainQueryManager queryManager) { WriteLine("Configuring {0} view handlers", handlers.Length); var name = string.Format("view-{0:yyyy-MM-dd-HH-mm-ss}", DateTime.Now); var combined = Path.Combine(Path.GetTempPath(), name); WriteLine("Redirecting atomic views to {0}", combined); var directoryInfo = new DirectoryInfo(combined); var file = FileStorage.CreateConfig(combined, reset: true); var nuclear = file.CreateNuclear(new ProjectionStrategy()); var watch = Stopwatch.StartNew(); var projectionTypes = new HashSet <Type>(handlers.Select(v => v.Projection)); WriteLine("Building consumers..."); var projections = BootstrapProjections.BuildProjectionsWithWhenConvention(nuclear.Factory).Where( x => projectionTypes.Contains(x.GetType())); var wire = new RedirectToDynamicEvent(); foreach (var projection in projections) { wire.WireToWhen(projection); } var handlersWatch = Stopwatch.StartNew(); queryManager.QueryConsumer(envelope => CallHandlers(wire, envelope)); WriteLine("Saved to {0}.", directoryInfo.FullName); var timeTotal = watch.Elapsed.TotalSeconds; var handlerTicks = handlersWatch.ElapsedTicks; var timeInHandlers = Math.Round(TimeSpan.FromTicks(handlerTicks).TotalSeconds, 1); WriteLine("Total Elapsed: {0}sec ({1}sec in handlers)", Math.Round(timeTotal, 0), timeInHandlers); WriteLine("Saved to {0}.", directoryInfo.FullName); var directoryInfos = handlers .Select(h => _strategy.GetFolderForEntity(h.ViewType, h.KeyType)) .Distinct() .Select(f => new DirectoryInfo(Path.Combine(directoryInfo.FullName, f.ToString()))) .ToArray(); return(directoryInfos); }
public void when_wire_to_object() { var dynamicEvent = new RedirectToDynamicEvent(); var testClass = new TestClassWithMethod(); dynamicEvent.WireToWhen(testClass); var t = new TestRedirectMethod { Id = 333, Name = "Name" }; dynamicEvent.InvokeEvent(t); Assert.AreEqual(t, testClass.RedirectMethod); }
public static IEnumerable <ViewMapInfo> GetActiveViewMaps() { var marker = new AtomicDetector(); var projections = BootstrapProjections.BuildProjectionsWithWhenConvention(marker) .ToArray(); int idx = 0; foreach (var projection in projections) { var redirect = new RedirectToDynamicEvent(); redirect.WireToWhen(projection); var mark = marker.List[idx]; yield return(new ViewMapInfo(mark.Item2, mark.Item1, projection.GetType(), redirect.Dict.Keys.ToArray()) ); idx += 1; } }
private Task RebuildProjections(List <ProjectionInfo> needRebuild, IDocumentStore memoryContainer, CancellationToken token) { if (needRebuild.Count == 0) { return(Task.FromResult(true)); } var updateTimer = new Stopwatch(); updateTimer.Start(); // observe projections var watch = Stopwatch.StartNew(); var wire = new RedirectToDynamicEvent(); needRebuild.ForEach(x => wire.WireToWhen(x.GetTempProjection())); var handlersWatch = Stopwatch.StartNew(); var eventStoreVersion = this.ObserveWhileCan(this._eventsStore.EnumerateAllItems(0, int.MaxValue), wire, token); if (token.IsCancellationRequested) { SystemObserver.Notify("[warn]\t{0}\tShutdown. Aborting projections before anything was changed.", this._name); return(Task.FromResult(true)); } var timeTotal = watch.Elapsed.TotalSeconds; var handlerTicks = handlersWatch.ElapsedTicks; var timeInHandlers = Math.Round(TimeSpan.FromTicks(handlerTicks).TotalSeconds, 1); SystemObserver.Notify("[observe]\t{2}\t{0}sec ({1}sec in handlers) - Replayed events from", Math.Round(timeTotal, 0), timeInHandlers, this._name); var rebuildTasks = needRebuild.Select(projectionInfo => this.RebuildProjection(eventStoreVersion, projectionInfo, memoryContainer, token)); return(Task.WhenAll(rebuildTasks)); }
public static void Rebuild(CancellationToken token, IDocumentStore targetContainer, MessageStore stream, Func <IDocumentStore, IEnumerable <object> > projectors) { var strategy = targetContainer.Strategy; var memory = new MemoryStorageConfig(); var memoryContainer = memory.CreateNuclear(strategy).Container; var tracked = new ProjectionInspectingStore(memoryContainer); var projections = new List <object>(); projections.AddRange(projectors(tracked)); if (tracked.Projections.Count != projections.Count()) { throw new InvalidOperationException("Count mismatch"); } tracked.ValidateSanity(); var storage = new NuclearStorage(targetContainer); var persistedHashes = new Dictionary <string, string>(); var name = "domain"; storage.GetEntity <ProjectionHash>(name).IfValue(v => persistedHashes = v.BucketHashes); var activeMemoryProjections = projections.Select((projection, i) => { var proj = tracked.Projections[i]; var bucketName = proj.StoreBucket; var viewType = proj.EntityType; var projectionHash = "Global change on 2012-08-24\r\n" + GetClassHash(projection.GetType()) + "\r\n " + GetClassHash(viewType) + "\r\n" + GetClassHash(strategy.GetType()); bool needsRebuild = !persistedHashes.ContainsKey(bucketName) || persistedHashes[bucketName] != projectionHash; return(new { bucketName, projection, hash = projectionHash, needsRebuild }); }).ToArray(); foreach (var memoryProjection in activeMemoryProjections) { if (memoryProjection.needsRebuild) { SystemObserver.Notify("[warn] {0} needs rebuild", memoryProjection.bucketName); } else { SystemObserver.Notify("[good] {0} is up-to-date", memoryProjection.bucketName); } } var needRebuild = activeMemoryProjections.Where(x => x.needsRebuild).ToArray(); if (needRebuild.Length == 0) { return; } var watch = Stopwatch.StartNew(); var wire = new RedirectToDynamicEvent(); needRebuild.ForEach(x => wire.WireToWhen(x.projection)); var handlersWatch = Stopwatch.StartNew(); ObserveWhileCan(stream.EnumerateAllItems(0, int.MaxValue), wire, token); if (token.IsCancellationRequested) { SystemObserver.Notify("[warn] Aborting projections before anything was changed"); return; } var timeTotal = watch.Elapsed.TotalSeconds; var handlerTicks = handlersWatch.ElapsedTicks; var timeInHandlers = Math.Round(TimeSpan.FromTicks(handlerTicks).TotalSeconds, 1); SystemObserver.Notify("Total Elapsed: {0}sec ({1}sec in handlers)", Math.Round(timeTotal, 0), timeInHandlers); // update projections that need rebuild foreach (var b in needRebuild) { // server might shut down the process soon anyway, but we'll be // in partially consistent mode (not all projections updated) // so at least we blow up between projection buckets token.ThrowIfCancellationRequested(); var bucketName = b.bucketName; var bucketHash = b.hash; // wipe contents targetContainer.Reset(bucketName); // write new versions var contents = memoryContainer.EnumerateContents(bucketName); targetContainer.WriteContents(bucketName, contents); // update hash storage.UpdateEntityEnforcingNew <ProjectionHash>(name, x => { x.BucketHashes[bucketName] = bucketHash; }); SystemObserver.Notify("[good] Updated View bucket {0}.{1}", name, bucketName); } // Clean up obsolete views var allBuckets = new HashSet <string>(activeMemoryProjections.Select(p => p.bucketName)); var obsoleteBuckets = persistedHashes.Where(s => !allBuckets.Contains(s.Key)).ToArray(); foreach (var hash in obsoleteBuckets) { // quit at this stage without any bad side effects if (token.IsCancellationRequested) { return; } var bucketName = hash.Key; SystemObserver.Notify("[warn] {0} is obsolete", bucketName); targetContainer.Reset(bucketName); storage.UpdateEntityEnforcingNew <ProjectionHash>(name, x => x.BucketHashes.Remove(bucketName)); SystemObserver.Notify("[good] Cleaned up obsolete view bucket {0}.{1}", name, bucketName); } }
public void when_wire_to_object_where_argument_is_interface() { var dynamicEvent = new RedirectToDynamicEvent(); var testClass = new TestClassWithInterfaceMethod(); dynamicEvent.WireToWhen(testClass); }
static void Main(string[] args) { const string integrationPath = @"temp"; ConfigureObserver(); var config = FileStorage.CreateConfig(integrationPath, "files"); var demoMessages = new List <ISampleMessage>(); var currentProcess = Process.GetCurrentProcess(); demoMessages.Add(new InstanceStarted("Inject git rev", currentProcess.ProcessName, currentProcess.Id.ToString())); { // wipe all folders config.Reset(); Console.WriteLine("Starting in funny mode"); var security = new SecurityId(0); demoMessages.Add(new CreateSecurityAggregate(security)); demoMessages.Add(new AddSecurityPassword(security, "Rinat Abdullin", "*****@*****.**", "password")); demoMessages.Add(new AddSecurityIdentity(security, "Rinat's Open ID", "http://abdullin.myopenid.org")); demoMessages.Add(new AddSecurityKey(security, "some key")); } var atomic = config.CreateNuclear(new DocumentStrategy()); var identity = new IdentityGenerator(atomic); var streamer = Contracts.CreateStreamer(); var tapes = config.CreateTape(Topology.TapesContainer); var streaming = config.CreateStreaming(); var routerQueue = config.CreateQueueWriter(Topology.RouterQueue); var aggregates = new AggregateFactory(tapes, streamer, routerQueue, atomic, identity); var sender = new SimpleMessageSender(streamer, routerQueue); var flow = new MessageSender(sender); var builder = new CqrsEngineBuilder(streamer); builder.Handle(config.CreateInbox(Topology.RouterQueue), Topology.Route(config.CreateQueueWriter, streamer, tapes), "router"); builder.Handle(config.CreateInbox(Topology.EntityQueue), aggregates.Dispatch); var functions = new RedirectToDynamicEvent(); // documents //functions.WireToWhen(new RegistrationUniquenessProjection(atomic.Factory.GetEntityWriter<unit, RegistrationUniquenessDocument>())); // UI projections var projectionStore = config.CreateNuclear(new ProjectionStrategy()); foreach (var projection in BootstrapProjections.BuildProjectionsWithWhenConvention(projectionStore.Factory)) { functions.WireToWhen(projection); } // processes //functions.WireToWhen(new BillingProcess(flow)); //functions.WireToWhen(new RegistrationProcess(flow)); functions.WireToWhen(new ReplicationProcess(flow)); builder.Handle(config.CreateInbox(Topology.EventsQueue), aem => CallHandlers(functions, aem)); var timer = new StreamingTimerService(config.CreateQueueWriter(Topology.RouterQueue), streaming.GetContainer(Topology.FutureMessagesContainer), streamer); builder.Handle(config.CreateInbox(Topology.TimerQueue), timer.PutMessage); builder.AddProcess(timer); using (var cts = new CancellationTokenSource()) using (var engine = builder.Build()) { var task = engine.Start(cts.Token); foreach (var sampleMessage in demoMessages) { sender.SendOne(sampleMessage); } Console.WriteLine(@"Press enter to stop"); Console.ReadLine(); cts.Cancel(); if (task.Wait(5000)) { Console.WriteLine(@"Terminating"); } } }
public static void Rebuild(IDocumentStore targetContainer, ITapeStream stream) { var strategy = targetContainer.Strategy; var memory = new MemoryStorageConfig(); var memoryContainer = memory.CreateNuclear(strategy).Container; var tracked = new ProjectionInspectingContainer(memoryContainer); var projections = new List <object>(); projections.AddRange(DomainBoundedContext.Projections(tracked)); projections.AddRange(ClientBoundedContext.Projections(tracked)); //projections.AddRange(ApiOpsBoundedContext.Projections(tracked)); if (tracked.Buckets.Count != projections.Count()) { throw new InvalidOperationException("Count mismatch"); } var storage = new NuclearStorage(targetContainer); var hashes = storage.GetSingletonOrNew <ProjectionHash>().Entries; var memoryProjections = projections.Select((projection, i) => { var bucketName = tracked.Buckets[i]; var viewType = tracked.Views[i]; var projectionHash = GetClassHash(projection.GetType()) + "\r\n" + GetClassHash(viewType); bool needsRebuild = !hashes.ContainsKey(bucketName) || hashes[bucketName] != projectionHash; return(new { bucketName, projection, hash = projectionHash, needsRebuild }); }).ToArray(); foreach (var memoryProjection in memoryProjections) { if (memoryProjection.needsRebuild) { SystemObserver.Notify("[warn] {0} needs rebuild", memoryProjection.bucketName); } else { SystemObserver.Notify("[good] {0} is up-to-date", memoryProjection.bucketName); } } var needRebuild = memoryProjections.Where(x => x.needsRebuild).ToArray(); if (needRebuild.Length == 0) { return; } var watch = Stopwatch.StartNew(); var wire = new RedirectToDynamicEvent(); needRebuild.ForEach(x => wire.WireToWhen(x.projection)); var handlersWatch = Stopwatch.StartNew(); Observe(stream, wire); var timeTotal = watch.Elapsed.TotalSeconds; var handlerTicks = handlersWatch.ElapsedTicks; var timeInHandlers = Math.Round(TimeSpan.FromTicks(handlerTicks).TotalSeconds, 1); Console.WriteLine("Total Elapsed: {0}sec ({1}sec in handlers)", Math.Round(timeTotal, 0), timeInHandlers); // delete projections that were rebuilt var bucketNames = needRebuild.Select(x => x.bucketName).ToArray(); foreach (var name in bucketNames) { targetContainer.Reset(name); var contents = memoryContainer.EnumerateContents(name); targetContainer.WriteContents(name, contents); } var allBuckets = new HashSet <string>(memoryProjections.Select(p => p.bucketName)); var obsolete = hashes.Keys.Where(s => !allBuckets.Contains(s)).ToArray(); foreach (var name in obsolete) { SystemObserver.Notify("[warn] {0} is obsolete", name); targetContainer.Reset(name); } storage.UpdateSingletonEnforcingNew <ProjectionHash>(x => { x.Entries.Clear(); foreach (var prj in memoryProjections) { x.Entries[prj.bucketName] = prj.hash; } }); }