public async Task SendMessage(StoreEvent storeEvent) { if (!_active) { // pass through _queueContainer.Send(storeEvent); return; } try { int ehPartitionId; if (!string.IsNullOrEmpty(storeEvent.PartitionId)) { ehPartitionId = storeEvent.PartitionId.GetHashCode() % _storeConfig.EventHubPartitionCount; } else { ehPartitionId = storeEvent.StoreId.GetHashCode() % _storeConfig.EventHubPartitionCount; } var content = JsonConvert.SerializeObject(storeEvent, Formatting.None); var bytes = Encoding.UTF8.GetBytes(content); await _eventHubClient.SendAsync(new EventData(bytes), ehPartitionId.ToString()); } catch (Exception e) { Console.WriteLine(e); } }
public async Task ProcessEventsAsync(IEnumerable <EventData> events) { if (events == null) { return; } foreach (var e in events) { _eventCount++; var content = Encoding.UTF8.GetString(e.Body); var partitionId = e.SystemProperties["x-opt-partition-key"].ToString(); var offset = e.SystemProperties["x-opt-offset"].ToString(); StoreEvent storeEvent; try { storeEvent = StoreEvent.FromString(content); } catch (Exception exception) { _logger.LogError(exception, "Unable to process event {offset} {partition} {}", offset, partitionId); continue; } await ProcessEventsAsync(storeEvent); _checkpoint.Write($"{Constants.EventHubCheckpoint}.{_storeConfig.EventHubName}.{partitionId}", offset); } return; }
public Task ProcessEventsAsync(StoreEvent storeEvent) { var storeId = storeEvent.StoreId; string operation = storeEvent.Operation; var opId = storeEvent.OperationId; var strict = storeEvent.Strict; TaskCompletionSource <bool> tc = null; if (opId != null) { _completions.TryGetValue(opId, out tc); } try { var data = storeEvent.Data; switch (operation) { case EventType.POST: _storeProcessor.Assert(storeId, data, strict); break; case EventType.PATCH_JSON: _storeProcessor.PatchJson(storeId, data); break; case EventType.PATCH_TRIPLE: var patch = (JObject)storeEvent.Data; _storeProcessor.PatchTriple(storeId, patch); break; case EventType.DELETE: _storeProcessor.Delete(storeId, data); break; default: throw new InvalidOperationException($"Unknown operation {operation}"); } tc?.SetResult(true); } catch (Exception exception) { tc?.SetException(exception); } finally { if (tc != null) { _completions.Remove(opId, out _); } } return(Task.CompletedTask); }
public async Task SendMessage(StoreEvent storeEvent) { if (!_active) { // pass through await _storeReceiver.ProcessEventsAsync(storeEvent); return; } try { storeEvent.PartitionId = storeEvent.StoreId.GetHashCode() % _storeConfig.EventHubPartitionCount; var content = JsonConvert.SerializeObject(storeEvent, Formatting.None); var bytes = Encoding.UTF8.GetBytes(content); await _eventHubClient.SendAsync(new EventData(bytes), storeEvent.PartitionId.ToString()); } catch (Exception e) { Console.WriteLine(e); } }