private async Task <ProcessedMessageStatus> SingleMessageConsumeAsync(MotorCloudEvent <byte[]> dataCloudEvent, CancellationToken token) { try { byte[] decoded; using (new AutoObserveStopwatch(() => _messageDecoding)) { decoded = await DecodeMessageAsync( dataCloudEvent.GetEncoding(), dataCloudEvent.TypedData, token); } TInput deserialized; using (new AutoObserveStopwatch(() => _messageDeserialization)) { deserialized = _deserializer.Deserialize(decoded); } return(await _queue .QueueBackgroundWorkItem(dataCloudEvent.CreateNew(deserialized, true)) .ConfigureAwait(true)); } catch (ArgumentException e) { _logger.LogError(LogEvents.InvalidInput, e, "Invalid Input"); return(ProcessedMessageStatus.InvalidInput); } catch (Exception e) { _logger.LogError(LogEvents.UnexpectedErrorOnMessageProcessing, e, "Invalid Input"); return(ProcessedMessageStatus.CriticalFailure); } }
public async Task <MotorCloudEvent <string>?> ConvertMessageAsync(MotorCloudEvent <string> dataCloudEvent, CancellationToken token = default) { await Task.Delay(Timeout.InfiniteTimeSpan, token).ConfigureAwait(false); return(dataCloudEvent.CreateNew(string.Empty)); }
public Task <MotorCloudEvent <string> > ConvertMessageAsync(MotorCloudEvent <string> dataCloudEvent, CancellationToken token = default) { _logger.LogInformation("log your request"); var tmpChar = dataCloudEvent.TypedData.ToCharArray(); var reversed = tmpChar.Reverse().ToArray(); _summary.WithLabels("collect_your_metrics").Observe(1.0); return(Task.FromResult(dataCloudEvent.CreateNew(new string(reversed)))); }
public void CreateNew_UseFromOld_DataIsUpdated() { var oldEvent = new MotorCloudEvent <string>(GetApplicationNameService("test://non2"), " ", "String", new Uri("test://non")); var expectedData = new List <string>(); var newEvent = oldEvent.CreateNew(expectedData); Assert.Equal(expectedData, newEvent.Data); }
public void CreateNew_DoNotUseOldIdentifierFromOld_TypeMatchesNewDataType() { var oldEvent = new MotorCloudEvent <string>(GetApplicationNameService(), " ", new Uri("test://non")); var expectedData = new List <string>(); var newEvent = oldEvent.CreateNew(expectedData, false); Assert.Equal(nameof(String), oldEvent.Type); Assert.Equal(typeof(List <string>).Name, newEvent.Type); }
public void CreateNew_OldEventHasContentEncoding_NewEventHasSameContentEncoding() { var oldEvent = new MotorCloudEvent <string>(GetApplicationNameService(), " ", new Uri("test://non")); oldEvent.SetEncoding("some-encoding"); var expectedData = new List <string>(); var newEvent = oldEvent.CreateNew(expectedData); Assert.Equal(oldEvent.GetEncoding(), newEvent.GetEncoding()); }
public void CreateNew_UseOldIdentifierFromOld_IdAndDateAreNewCreated() { var oldEvent = new MotorCloudEvent <string>(GetApplicationNameService(), " ", new Uri("test://non")); var expectedData = new List <string>(); var newEvent = oldEvent.CreateNew(expectedData, true); Assert.Equal(oldEvent.Id, newEvent.Id); Assert.Equal(oldEvent.Time, newEvent.Time); Assert.Equal(oldEvent.Source, newEvent.Source); }
public Task <MotorCloudEvent <string>?> ConvertMessageAsync(MotorCloudEvent <string> dataCloudEvent, CancellationToken token = default) { var incrementedCounter = Interlocked.Increment(ref _consumedCounter); if (incrementedCounter % 2 == 0) { throw new TemporaryFailureException(); } return(Task.FromResult <MotorCloudEvent <string>?>(dataCloudEvent.CreateNew(string.Empty))); }
// Handle incoming messages public Task <IEnumerable <MotorCloudEvent <OutputMessage> > > ConvertMessageAsync( MotorCloudEvent <InputMessage> inputEvent, CancellationToken token = default) { // Get the input message from the cloud event var input = inputEvent.TypedData; // Do your magic here ..... var output = MagicFunc(input); // Create a new cloud event from your output message which is automatically published and return a new task. var outputEvent = output.Select(singleEvent => inputEvent.CreateNew(singleEvent)); return(Task.FromResult(outputEvent)); }
// Handle incoming messages public async IAsyncEnumerable <MotorCloudEvent <OutputMessage> > ConvertMessageAsync( MotorCloudEvent <InputMessage> inputEvent, [EnumeratorCancellation] CancellationToken token = default) { // Get the input message from the cloud event var input = inputEvent.TypedData; // Do your magic here ..... var output = MagicFuncAsync(input); // Create a new cloud event from your output message which is automatically published and return a new task. await foreach (var outputMessage in output.WithCancellation(token)) { yield return(inputEvent.CreateNew(outputMessage)); } }
public async Task PublishMessageAsync(MotorCloudEvent <TOutput> cloudEvent, CancellationToken token = default) { var watch = Stopwatch.StartNew(); byte[] bytes; try { bytes = _messageSerializer.Serialize(cloudEvent.TypedData); } finally { watch.Stop(); _messageSerialization?.Observe(watch.ElapsedMilliseconds); } var bytesEvent = cloudEvent.CreateNew(bytes, true); await _bytesMessagePublisher.PublishMessageAsync(bytesEvent, token); }
private async Task <ProcessedMessageStatus> SingleMessageConsumeAsync(MotorCloudEvent <byte[]> dataCloudEvent, CancellationToken token) { try { var deserialize = _deserializer.Deserialize(dataCloudEvent.TypedData); return(await _queue .QueueBackgroundWorkItem(dataCloudEvent.CreateNew(deserialize, true)) .ConfigureAwait(true)); } catch (ArgumentException e) { _logger.LogError(LogEvents.InvalidInput, e, "Invalid Input"); return(ProcessedMessageStatus.InvalidInput); } catch (Exception e) { _logger.LogError(LogEvents.UnexpectedErrorOnMessageProcessing, e, "Invalid Input"); return(ProcessedMessageStatus.CriticalFailure); } }
public async Task PublishMessageAsync(MotorCloudEvent <TOutput> motorCloudEvent, CancellationToken token = default) { byte[] bytes, encodedBytes; using (new AutoObserveStopwatch(() => _messageSerialization)) { bytes = _messageSerializer.Serialize(motorCloudEvent.TypedData); } using (new AutoObserveStopwatch(() => _messageEncoding)) { encodedBytes = await _messageEncoder.EncodeAsync(bytes, token); } var bytesEvent = motorCloudEvent.CreateNew(encodedBytes, true); bytesEvent.SetMotorVersion(); if (!_encodingOptions.IgnoreEncoding) { bytesEvent.SetEncoding(_messageEncoder.Encoding); } await _bytesMessagePublisher.PublishMessageAsync(bytesEvent, token); }
public Task <MotorCloudEvent <OutgoingMessage> > ConvertMessageAsync( MotorCloudEvent <IncomingMessage> dataCloudEvent, CancellationToken token = default) { Console.WriteLine(dataCloudEvent.TypedData.SomeProperty); return(Task.FromResult(dataCloudEvent.CreateNew(DoSomething(dataCloudEvent.TypedData)))); }
public Task <MotorCloudEvent <ByteData>?> ConvertMessageAsync(MotorCloudEvent <ByteData> dataCloudEvent, CancellationToken token = default) { summary.Observe(dataCloudEvent.TypedData?.data.Length ?? 0); return(Task.FromResult <MotorCloudEvent <ByteData>?>(dataCloudEvent.CreateNew(dataCloudEvent.TypedData, true))); }