Beispiel #1
0
        public virtual void MarkCommitAsDispatched(Commit commit)
        {
            if (commit == null)
            {
                throw new ArgumentNullException("commit");
            }

            var patch = new PatchRequest {
                Type = PatchCommandType.Set, Name = "Dispatched", Value = RavenJToken.Parse("true")
            };
            var data = new PatchCommandData {
                Key = commit.ToRavenCommitId(), Patches = new[] { patch }
            };

            Logger.Debug(Messages.MarkingCommitAsDispatched, commit.CommitId, commit.BucketId);

            TryRaven(() =>
            {
                using (TransactionScope scope = OpenCommandScope())
                    using (IDocumentSession session = _store.OpenSession())
                    {
                        session.Advanced.DocumentStore.DatabaseCommands.Batch(new[] { data });
                        session.SaveChanges();
                        scope.Complete();
                        return(true);
                    }
            });
        }
Beispiel #2
0
        public void Run(ISagaInfo sagaInfo)
        {
            PrintHelper.PrintFunctionCallDetails("SetSagaUniqueValueInMetadata", sagaInfo);
            var documentStore = new DocumentStore
            {
                Url             = sagaInfo.RavenUrl,
                DefaultDatabase = sagaInfo.DatabaseName
            };

            documentStore.Initialize();
            const int pageSize     = 100;
            var       commandsList = new List <ICommandData>();

            for (var start = 0; ; start += pageSize)
            {
                var documents = documentStore.DatabaseCommands.GetDocuments(start, pageSize);
                foreach (var document in documents)
                {
                    if (document.Key.StartsWith(sagaInfo.SagaKeyPrefix) && !DocumentHelper.HasServicebusUniqueValue(document))
                    {
                        var body          = document.DataAsJson;
                        var uniqueValue   = body.Value <string>(sagaInfo.UniqueField);
                        var patchRequests = new[]
                        {
                            new PatchRequest
                            {
                                Type   = PatchCommandType.Modify,
                                Name   = "@metadata",
                                Nested = new[]
                                {
                                    new PatchRequest
                                    {
                                        Type  = PatchCommandType.Set,
                                        Name  = "NServiceBus-UniqueValue",
                                        Value = uniqueValue,
                                    }
                                }
                            }
                        };
                        ICommandData command = new PatchCommandData
                        {
                            Key     = document.Key,
                            Patches = patchRequests
                        };
                        commandsList.Add(command);
                    }
                }
                if (documents.Length < pageSize)
                {
                    break;
                }
            }
            Console.WriteLine("Attempt to patch {0} saga documents", commandsList.Count);
            var results = documentStore.AsyncDatabaseCommands.BatchAsync(commandsList.ToArray());

            PrintHelper.PrintResults("SetSagaUniqueValueInMetadata", results);
        }
        public static void PatchIf <TDocument>(this IAsyncDocumentSession session, string id, PropertyUpdateBatch <TDocument> updates, Expression <Func <TDocument, bool> > condition)
        {
            var batch = updates.CreateBatch();

            var patchRequest     = _patchRequestBuilder.CreatePatchRequest(batch, condition);
            var patchCommandData = new PatchCommandData(id, null, patchRequest, null);

            session.Advanced.Defer(new ICommandData[] { patchCommandData });
        }
        protected override void ConcreteExecute(IJobExecutionContext context)
        {
            if (AllApps == null)
            {
                return;
            }

            var commands = new List <PatchCommandData>();

            for (int index = 0; index < AllApps.Count(); index++)
            {
                var app      = AllApps[index];
                var isActive = AllActiveAppIds.Contains(app.AppId.ToString());

                if (app.IsActive != isActive)
                {
                    var cmd = new PatchCommandData()
                    {
                        Key     = app.Id,
                        Patches = new[]
                        {
                            new PatchRequest
                            {
                                Name  = "IsActive",
                                Value = RavenJToken.FromObject(isActive)
                            }
                        }
                    };
                    commands.Add(cmd);
                }
            }

            if (commands.Count > 0)
            {
                const int batchSize = 512;
                int       handled   = 0;
                do
                {
                    var commandsToSave = commands
                                         .Skip(handled)
                                         .Take(batchSize)
                                         .ToList();

                    Store.DatabaseCommands.Batch(commandsToSave);
                    handled += commandsToSave.Count();

                    Console.WriteLine("Saving {0} {1}/{2} applications", commandsToSave.Count(), handled, commands.Count);
                    Thread.Sleep(5000);
                } while (handled < commands.Count);
            }
        }
        async Task BatchDispatchToTransport(TransportOperation[] transportOperations, IReadOnlyCollection <FailedMessage> messages, IReadOnlyDictionary <string, FailedMessageRetry> failedMessageRetriesById, string stagingId)
        {
            try
            {
                await sender.Dispatch(new TransportOperations(transportOperations), transaction, contextBag).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                var commands     = new ICommandData[messages.Count];
                var commandIndex = 0;
                foreach (var failedMessage in messages)
                {
                    var failedMessageRetry = failedMessageRetriesById[failedMessage.Id];

                    Log.Warn($"Attempt {1} of {MaxStagingAttempts} to stage a retry message {failedMessage.UniqueMessageId} failed", e);

                    commands[commandIndex] = new PatchCommandData
                    {
                        Patches = new[]
                        {
                            new PatchRequest
                            {
                                Type  = PatchCommandType.Set,
                                Name  = "StageAttempts",
                                Value = 1
                            }
                        },
                        Key = failedMessageRetry.Id
                    };

                    commandIndex++;
                }

                try
                {
                    await store.AsyncDatabaseCommands.BatchAsync(commands).ConfigureAwait(false);
                }
                catch (ConcurrencyException)
                {
                    Log.DebugFormat("Ignoring concurrency exception while incrementing staging attempt count for {0}", stagingId);
                }

                throw new RetryStagingException(e);
            }
        }
        public virtual void MarkCommitAsDispatched(Commit commit)
        {
            if (commit == null)
            {
                throw new ArgumentNullException("commit");
            }

            var patch = new PatchRequest
            {
                Type  = PatchCommandType.Set,
                Name  = "Dispatched",
                Value = RavenJToken.Parse("true")
            };
            var data = new PatchCommandData
            {
                Key     = commit.ToRavenCommitId(),
                Patches = new[] { patch }
            };

            try
            {
                using (var scope = this.OpenCommandScope())
                    using (var session = this.store.OpenSession())
                    {
                        session.Advanced.DatabaseCommands.Batch(new[] { data });
                        session.SaveChanges();
                        scope.Complete();
                    }
            }
            catch (WebException e)
            {
                throw new StorageUnavailableException(e.Message, e);
            }
            catch (Exception e)
            {
                throw new StorageException(e.Message, e);
            }
        }
        private void SaveEmployeeEvents(
            Employee employee)
        {
            var patches = new List <PatchRequest>();

            foreach (var evt in employee.PendingEvents)
            {
                patches.Add(new PatchRequest
                {
                    Type  = PatchCommandType.Add,
                    Name  = "Events",
                    Value = RavenJObject.FromObject(evt, _serializer)
                });
            }
            var localId = $"employees/{employee.Id}";

            var addEmployeeEvents = new PatchCommandData()
            {
                Key     = localId,
                Patches = patches.ToArray()
            };

            var updateMetadata = new ScriptedPatchCommandData()
            {
                Key   = localId,
                Patch = new ScriptedPatchRequest
                {
                    Script = $"this['@metadata']['{EmployeeEntityVersion}'] = {employee.Version}; "
                }
            };

            _store.DatabaseCommands.Batch(new ICommandData[]
            {
                addEmployeeEvents,
                updateMetadata
            });
        }