public async Task <IEnumerable <ISagaLogData> > ReadAsync(SagaId id, Type sagaType) { if (string.IsNullOrWhiteSpace(id)) { throw new ChronicleException($"{nameof(id)} was null."); } if (sagaType is null) { throw new ChronicleException($"{nameof(sagaType)} was null."); } var sagaLogDatas = new List <RedisSagaLogData>(); var deserializedSagaLogDatas = new List <RedisSagaLogData>(); var cachedSagaLogDatas = await cache.GetStringAsync(LogId(id, sagaType)); if (!string.IsNullOrWhiteSpace(cachedSagaLogDatas)) { sagaLogDatas = JsonConvert.DeserializeObject <List <RedisSagaLogData> >(cachedSagaLogDatas); sagaLogDatas.ForEach(sld => { { var message = (sld.Message as JObject)?.ToObject(sld.MessageType); deserializedSagaLogDatas.Add(new RedisSagaLogData { SagaId = sld.Id, Type = sld.Type, CreatedAt = sld.CreatedAt, Message = message, MessageType = sld.MessageType }); } }); } return(deserializedSagaLogDatas); }
/// <summary> /// Transforms the timeout to a <see cref="TransportMessage"/>. /// </summary> /// <returns>Returns a <see cref="TransportMessage"/>.</returns> public TransportMessage ToTransportMessage() { var replyToAddress = Address.Local; if (Headers != null && Headers.ContainsKey(OriginalReplyToAddress)) { replyToAddress = Address.Parse(Headers[OriginalReplyToAddress]); Headers.Remove(OriginalReplyToAddress); } var transportMessage = new TransportMessage(Id, Headers) { ReplyToAddress = replyToAddress, Recoverable = true, CorrelationId = CorrelationId, Body = State }; if (SagaId != Guid.Empty) { transportMessage.Headers[NServiceBus.Headers.SagaId] = SagaId.ToString(); } transportMessage.Headers["NServiceBus.RelatedToTimeoutId"] = Id; return(transportMessage); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { var coordinator = app.ApplicationServices.GetService <ISagaCoordinator>(); var context = SagaContext .Create() .WithSagaId(SagaId.NewSagaId()) .WithOriginator("Test") .WithMetadata("key", "lulz") .Build(); var context2 = SagaContext .Create() .WithSagaId(SagaId.NewSagaId()) .WithOriginator("Test") .WithMetadata("key", "lulz") .Build(); coordinator.ProcessAsync(new Message1 { Text = "This message will be used one day..." }, context); coordinator.ProcessAsync(new Message2 { Text = "But this one will be printed first! (We compensate from the end to beggining of the log)" }, onCompleted: (m, ctx) => { Console.WriteLine("My work is done"); return(Task.CompletedTask); }, context: context); Console.ReadLine(); }
public RedisSagaLogData(SagaId id, Type type, long createdAt, object message, Type messageType) { Id = id; Type = type; CreatedAt = createdAt; Message = message; MessageType = messageType; }
public async Task <ISagaState> ReadAsync(SagaId id, Type type) { var _currSagaState = await SagaUnitOfWork .SagaStateDBRepository .ReadAsync(id, type); return(_currSagaState); }
public override SagaId ResolveId(object message, ISagaContext context) { switch (message) { case SignedIn m: return(SagaId.NewSagaId()); default: return(base.ResolveId(message, context)); } }
public void WithCorrelationId_Sets_CorrelationId_Field_With_Given_Data() { var correlationId = SagaId.NewSagaId(); var context = _builder .WithSagaId(correlationId) .Build(); context.SagaId.ShouldBe(correlationId); }
protected ICorrelationContext GetContext <T>(string resourceId = "", string resource = "") where T : ICommand { if (!string.IsNullOrWhiteSpace(resource)) { resource = $"{resource}/{resourceId}"; } return(CorrelationContext.Create <T>(SagaId.NewSagaId(), UserId, resourceId, HttpContext.TraceIdentifier, HttpContext.Connection.Id, Tracer?.ActiveSpan?.Context?.ToString() ?? "", Request.Path.ToString(), Culture, resource)); }
private void InitializeSaga(ISaga saga, SagaId id, ISagaState state) { if (state.Data is null) { saga.Initialize(id, state.State); } else { saga.InvokeGeneric(nameof(ISaga <object> .Initialize), id, state.State, state.Data); } }
public async Task DeleteAsync(SagaId sagaId, Type sagaType) { if (string.IsNullOrWhiteSpace(sagaId)) { throw new ChronicleException($"{nameof(sagaId)} was null or whitespace."); } if (sagaType is null) { throw new ChronicleException($"{nameof(sagaType)} was null."); } await cache.RemoveAsync(LogId(sagaId, sagaType)); }
/// <summary> /// Returns the hash code for this instance. /// </summary> public override Int32 GetHashCode() { unchecked { var hash = 43; hash = (hash * 397) + SagaType.GetHashCode(); hash = (hash * 397) + SagaId.GetHashCode(); hash = (hash * 397) + Timeout.GetHashCode(); return(hash); } }
public long CalculateSize() { unsafe { return((2 * PartitionKey.Length) + (2 * RowKey.Length) + sizeof(DateTimeOffset) + // Timestamp 8 + (2 * nameof(Destination).Length) + Destination.Length + 8 + (2 * nameof(SagaId).Length) + SagaId.ToString().Length + 8 + (2 * nameof(StateAddress).Length) + StateAddress.Length + 8 + (2 * nameof(Time).Length) + sizeof(DateTime) + // Time property 8 + (2 * nameof(OwningTimeoutManager).Length) + OwningTimeoutManager.Length + 8 + (2 * nameof(Headers).Length) + Headers.Length); } }
/// <summary> /// Transforms the timeout to a <see cref="TransportMessage"/>. /// </summary> /// <returns>Returns a <see cref="TransportMessage"/>.</returns> public TransportMessage ToTransportMessage() { var transportMessage = new TransportMessage(Id, Headers) { Recoverable = true, Body = State }; if (SagaId != Guid.Empty) { transportMessage.Headers[NServiceBus.Headers.SagaId] = SagaId.ToString(); } transportMessage.Headers[NServiceBus.Headers.TimeSent] = DateTimeExtensions.ToWireFormattedString(DateTime.UtcNow); transportMessage.Headers["NServiceBus.RelatedToTimeoutId"] = Id; return(transportMessage); }
/// <summary> /// Transforms the timeout to a <see cref="TransportMessage"/>. /// </summary> /// <returns>Returns a <see cref="TransportMessage"/>.</returns> public TransportMessage ToTransportMessage() { var transportMessage = new TransportMessage(Id, Headers) { Recoverable = true, Body = State }; if (SagaId != Guid.Empty) { transportMessage.Headers[NServiceBus.Headers.SagaId] = SagaId.ToString(); } transportMessage.Headers["NServiceBus.RelatedToTimeoutId"] = Id; return(transportMessage); }
public async Task SetAsync(/*Guid*/ SagaId id, Guid userId, string name, OperationState state, string resource, string code = null, string reason = null) { var newState = state.ToString().ToLowerInvariant(); var operation = await GetAsync(id); operation = operation ?? new OperationDto(); operation.Id = string.IsNullOrEmpty(id)?SagaId.NewSagaId():id; operation.UserId = userId; operation.Name = name; operation.State = newState; operation.Resource = resource; operation.Code = code ?? string.Empty; operation.Reason = reason ?? string.Empty; await _cache.SetStringAsync(operation.Id, JsonConvert.SerializeObject(operation), new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10), SlidingExpiration = TimeSpan.FromMinutes(1) }); }
public async Task <IReadOnlyCollection <EFCoreSagaLogData> > ReadAsync(SagaId id, Type type) { return(await _dbContext.SagaLog .Where(sld => sld.SagaId == id.Id && sld.SagaType == type.FullName) .ToArrayAsync()); }
public async Task <IEnumerable <ISagaLogData> > ReadAsync(SagaId id, Type type) => await SagaUnitOfWork.SagaLogRepository .ReadAsync(id, type);
private SagaLogData(SagaId sagaId, Type sagaType, long createdAt, object message) => (Id, Type, CreatedAt, Message) = (sagaId, sagaType, createdAt, message);
public async Task <ISagaState> ReadAsync(SagaId id, Type type) => await _dbContext.Set <EFCoreSagaStateData>() .FirstOrDefaultAsync(sld => sld.SagaId == id.Id && sld.SagaType == type.FullName);
public async Task <EFCoreSagaStateData> GetByIdAsync(SagaId sagaId) { return(await _dbContext.SagaState .FirstOrDefaultAsync(sld => sld.SagaId == sagaId.Id)); }
public async Task <IEnumerable <ISagaLogData> > ReadAsync(SagaId id, Type type) => await _dbContext.Set <EFCoreSagaLogData>() .Where(sld => sld.SagaId == id.Id && sld.SagaType == type.FullName) .ToArrayAsync();
public Task <ISagaState> ReadAsync(SagaId id, Type type) { throw new NotImplementedException(); }
public static ISagaLogData Create(SagaId sagaId, Type sagaType, object message) => new RedisSagaLogData(sagaId, sagaType, DateTimeOffset.Now.ToUnixTimeMilliseconds(), message, message.GetType());
private SagaState(SagaId id, Type type, SagaStates state, object data) => (Id, Type, State, Data) = (id, type, state, data);
public async Task <IReadOnlyCollection <EFCoreSagaLogData> > ReadByIdAsync(SagaId id) { return(await _dbContext.SagaLog .Where(sld => sld.SagaId == id.Id) .ToArrayAsync()); }
public async Task <EFCoreSagaStateData> ReadAsync(SagaId id, Type type) { return(await _dbContext.SagaState .FirstOrDefaultAsync(sld => sld.SagaId == id.Id && sld.SagaType == type.FullName)); }
public OperationPending(SagaId id, Guid userId, string name, string resource) : base(id, userId, name, resource) { }
private static ISagaState CreateSagaState(SagaId id, Type sagaType, Type dataType) { var sagaData = dataType != null?Activator.CreateInstance(dataType) : null; return(SagaState.Create(id, sagaType, SagaStates.Pending, sagaData)); }
public OperationCompleted(SagaId id, Guid userId, string name, string resource) : base(id, userId, name, resource) { }
public OperationBase(SagaId id, Guid userId, string name, string resource) => (Id, UserId, Name, Resource) = (id, userId, name, resource);