public void ValidateFailedSubscriptionScriptExceptionHandling() { using (var store = GetDocumentStore()) { var subscriptionId = store.Subscriptions.Create(new SubscriptionCreationOptions() { Query = @" declare function project(d){ throw 'a party'; return d; } from Users as d select project(d) " }); var subscription = store.Subscriptions.GetSubscriptionWorker <User>(new SubscriptionWorkerOptions(subscriptionId) { TimeToWaitBeforeConnectionRetry = TimeSpan.FromSeconds(5) }); var exceptions = new List <Exception>(); var mre = new ManualResetEvent(false); var receivedItem = new SubscriptionBatch <User> .Item(); var userId = string.Empty; using (var session = store.OpenSession()) { var newUser = new User(); session.Store(newUser); session.SaveChanges(); userId = session.Advanced.GetDocumentId(newUser); } subscription.Run(x => { foreach (var item in x.Items) { receivedItem = item; try { var res = item; } catch (Exception e) { exceptions.Add(e); } } mre.Set(); }); Assert.True(mre.WaitOne(_reasonableWaitTime)); Assert.NotNull(receivedItem); Assert.Throws <InvalidOperationException>(() => receivedItem.Result); Assert.NotNull(receivedItem.Metadata); Assert.Equal(receivedItem.Id, userId); } }
private async Task _exec(SubscriptionBatch <OutboxEvent> batch) { using (var session = batch.OpenAsyncSession()) { foreach (var e in batch.Items) { await _publisher.PublishAsync(e.Result); session.Delete(e.Result); } await session.SaveChangesAsync(); } }
private static void HandleSubscriptionBatch(int nodesAmount, SubscriptionBatch <Revision <User> > b, HashSet <string> uniqueDocs, ref int docsCount, HashSet <string> uniqueRevisions, AsyncManualResetEvent reachedMaxDocCountMre, ref int revisionsCount) { foreach (var item in b.Items) { var x = item.Result; try { if (x == null) { } else if (x.Previous == null) { if (uniqueDocs.Add(x.Current.Id)) { docsCount++; } if (uniqueRevisions.Add(x.Current.Name)) { revisionsCount++; } } else if (x.Current == null) { } else { if (x.Current.Age > x.Previous.Age) { if (uniqueRevisions.Add(x.Current.Name)) { revisionsCount++; } } } if (docsCount == nodesAmount && revisionsCount == Math.Pow(nodesAmount, 2)) { reachedMaxDocCountMre.Set(); } } catch (Exception) { } } }
private async Task _exec(SubscriptionBatch <AggregateEventStore> batch) { _logger.Trace($"Got a batch of {batch.NumberOfItemsInBatch} items"); var tasks = batch.Items.GroupBy(x => x.Result.AggregateId) .Select(async g => { foreach (var e in g) { AggregateEventEnvelope <TAggregate> envelope = e.Result.FromStore <TAggregate>(); var evtType = envelope.Event.GetType(); var methodToInvoke = _dispatchMethods .GetOrAdd(evtType, type => _getDispatchMethod(evtType)); await(Task) methodToInvoke.Invoke(this, new object[] { envelope.Event, envelope.Metadata }); } }); await Task.WhenAll(tasks); }
public void ValidateFailedRevisionsSubscriptionScriptExceptionHandling() { using (var store = GetDocumentStore()) { using (var context = JsonOperationContext.ShortTermSingleUse()) { var configuration = new RevisionsConfiguration { Default = new RevisionsCollectionConfiguration { Disabled = false, MinimumRevisionsToKeep = 5, }, Collections = new Dictionary <string, RevisionsCollectionConfiguration> { ["Users"] = new RevisionsCollectionConfiguration { Disabled = false }, ["Dons"] = new RevisionsCollectionConfiguration { Disabled = false } } }; AsyncHelpers.RunSync(() => Server.ServerStore.ModifyDatabaseRevisions(context, store.Database, DocumentConventions.Default.Serialization.DefaultConverter.ToBlittable(configuration, context), Guid.NewGuid().ToString())); } var subscriptionId = store.Subscriptions.Create(new SubscriptionCreationOptions() { Query = @" declare function project(d){ throw 'nice'; return d; } from Users (Revisions = true) as d select project(d) " }); var subscription = store.Subscriptions.GetSubscriptionWorker <User>(new SubscriptionWorkerOptions(subscriptionId) { TimeToWaitBeforeConnectionRetry = TimeSpan.FromSeconds(5) }); var exceptions = new List <Exception>(); var mre = new ManualResetEvent(false); var receivedItem = new SubscriptionBatch <User> .Item(); var userId = string.Empty; using (var session = store.OpenSession()) { var newUser = new User(); session.Store(newUser); session.SaveChanges(); userId = session.Advanced.GetDocumentId(newUser); } subscription.Run(x => { foreach (var item in x.Items) { receivedItem = item; try { var res = item; } catch (Exception e) { exceptions.Add(e); } } mre.Set(); }); Assert.True(mre.WaitOne(_reasonableWaitTime)); Assert.NotNull(receivedItem); Assert.Throws <InvalidOperationException>(() => receivedItem.Result); Assert.NotNull(receivedItem.Metadata); Assert.Equal(receivedItem.Id, userId); } }
private async Task _processAuditChange(SubscriptionBatch <Revision <dynamic> > batch) { using (var session = _store.OpenAsyncSession()) { foreach (var e in batch.Items) { if (e.Result.Current?.AuditId != null) //Delete does not have an audit { string operation = default; if (e.Result.Previous != null && e.Result.Current == null) { operation = Operations.Delete.ToString(); } else if (e.Result.Previous != null && e.Result.Current != null) { operation = Operations.Update.ToString(); } else if (e.Result.Previous == null && e.Result.Current != null) { operation = Operations.Insert.ToString(); } session.Advanced.Defer(new PatchCommandData( id: (string)e.Result.Current.AuditId, changeVector: null, patch: new PatchRequest { Script = @"this.EntityInfo .forEach(eInfo => { if (eInfo.EntityId == args.Id) { eInfo.CurrChangeVector = args.Cv; eInfo.Operation = args.Operation; eInfo.LastModified = args.LastMod; } }); " , Values = { { "Cv", e.ChangeVector }, { "Id", e.Id }, { "LastMod", e.Metadata["@last-modified"] }, { "Operation", operation } } }, patchIfMissing: null)); } } await session.SaveChangesAsync(); } }