private void OnReplicaChanged(ReplicaEvent replicaEvent) { var replica = replicaEvent.Replica; if (!(replica is ProcessStatus process)) { // This temporarily only works for processes launched return; } switch (replicaEvent.State) { case ReplicaState.Started: { var cts = new CancellationTokenSource(); var state = new DiagnosticsState { StoppingTokenSource = cts, Thread = new Thread(() => { var replicaInfo = new ReplicaInfo( (processes) => processes.FirstOrDefault(p => p.Id == process.Pid !.Value), // TODO: Finding the application name requires msbuild knowledge Path.GetFileNameWithoutExtension(process.Service.Status.ProjectFilePath) !, process.Service.Description.Name, replica.Name, replica.Metrics); _diagnosticsCollector.Collect(replicaInfo, cts.Token); }), }; replica.Items[typeof(DiagnosticsState)] = state; state.Thread.Start(); } break; case ReplicaState.Stopped: { if (replica.Items.TryGetValue(typeof(DiagnosticsState), out var item) && item is DiagnosticsState state) { state.StoppingTokenSource.Cancel(); state.Thread.Join(); } } break; default: break; } }
void OnReplicaChange(ReplicaEvent ev) { if (replicas.Contains(ev.Replica.Name) && ev.State == ReplicaState.Stopped) { Interlocked.Decrement(ref remaining); } if (remaining == 0) { stoppedTask.TrySetResult(true); } }
private void OnReplicaEvent(ReplicaEvent replicaEvent) { foreach (var binding in replicaEvent.Replica.Bindings) { if (replicaEvent.State == ReplicaState.Ready) { _readyPorts.TryAdd(binding.Port, true); } else { _readyPorts.TryRemove(binding.Port, out _); } } }
/// <summary> /// Serializes the object to JSON. /// </summary> /// <param name="writer">The <see cref="T: Newtonsoft.Json.JsonWriter" /> to write to.</param> /// <param name="obj">The object to serialize to JSON.</param> internal static void Serialize(JsonWriter writer, ReplicaEvent obj) { // Required properties are always serialized, optional properties are serialized when not null. writer.WriteStartObject(); writer.WriteProperty(obj.Kind.ToString(), "Kind", JsonWriterExtensions.WriteStringValue); writer.WriteProperty(obj.EventInstanceId, "EventInstanceId", JsonWriterExtensions.WriteGuidValue); writer.WriteProperty(obj.TimeStamp, "TimeStamp", JsonWriterExtensions.WriteDateTimeValue); writer.WriteProperty(obj.PartitionId, "PartitionId", PartitionIdConverter.Serialize); writer.WriteProperty(obj.ReplicaId, "ReplicaId", ReplicaIdConverter.Serialize); if (obj.HasCorrelatedEvents != null) { writer.WriteProperty(obj.HasCorrelatedEvents, "HasCorrelatedEvents", JsonWriterExtensions.WriteBoolValue); } writer.WriteEndObject(); }
private void OnReplicaEvent(ReplicaEvent replicaEvent) { // when a replica becomes ready for the first time, it shouldn't have a cancellation token in the dictionary // for any event other than ready, we want to cancel the token and remove it from the dictionary foreach (var binding in replicaEvent.Replica.Bindings) { if (_cancellationsByReplicaPort.TryRemove(binding.Port, out var cts)) { cts.Cancel(); } } if (replicaEvent.State == ReplicaState.Ready) { foreach (var binding in replicaEvent.Replica.Bindings) { _cancellationsByReplicaPort.TryAdd(binding.Port, new CancellationTokenSource()); } } }
private void OnReplicaChanged(ReplicaEvent replicaEvent) { switch (replicaEvent.State) { case ReplicaState.Started: _states.TryAdd(replicaEvent.Replica.Name, new ReplicaMonitorState(replicaEvent.Replica, _logger)); break; case ReplicaState.Stopped: if (_states.TryRemove(replicaEvent.Replica.Name, out var stateToDispose)) { stateToDispose.Dispose(); } break; default: if (_states.TryGetValue(replicaEvent.Replica.Name, out var state)) { state.Update(replicaEvent); } break; } }
public void Update(ReplicaEvent replicaEvent) { }