public void Publish(CheckEvent ev) { using (new TraceLogicalScope(source, "Publish")) { try { string path = String.Format("{0}/Events", dc); this.source.TraceData(TraceEventType.Verbose, 0, new object[] { "Path", path }); string connectionString = configuration.ServiceBus.ConnectionString; this.source.TraceData(TraceEventType.Verbose, 0, new object[] { "ConnectionString", connectionString }); NamespaceManager ns = NamespaceManager.CreateFromConnectionString(connectionString); lock (this.locker) { if (!ns.TopicExists(path)) { CreateTopic(ns, dc, path); } } TopicClient client = TopicClient.CreateFromConnectionString(connectionString, path); DataContractJsonSerializer serializer = new DataContractJsonSerializer(ev.GetType()); using (Stream stream = new MemoryStream()) { serializer.WriteObject(stream, ev); stream.Seek(0, SeekOrigin.Begin); BrokeredMessage message = new BrokeredMessage(stream); message.To = dc; message.Label = "Check"; message.Properties.Add("Type", "Check"); message.Properties.Add("Method", "PUT"); client.Send(message); } } catch (Exception ex) { source.TraceData(TraceEventType.Error, 0, ex); throw; } } }
internal void Start() { Microsoft.ServiceBus.ServiceBusEnvironment.SystemConnectivity.Mode = Microsoft.ServiceBus.ConnectivityMode.Http; string node = configuration.Node; string dc = configuration.Dc; this.publisher = new Publisher(dc); TraceSource source = new TraceSource("Health.Checks"); string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), dc, "Checks"); source.TraceData(TraceEventType.Verbose, 0, "Path", path); foreach (PowerShellCheckItem item in PowerShellCheckFactory.Initialize(path).Values) { using (new TraceLogicalScope(source, String.Format("Processing {0}.", item.Configuration.Id))) { if (!item.Configuration.Disabled) { List<string> validationErrors = new List<string>(); item.Check.Validate(validationErrors); if (validationErrors.Count > 0) { foreach (string error in validationErrors) source.TraceData(TraceEventType.Warning, 0, error); throw new Exception("The Check contains validation errors."); } var ob = Observable.Create<CheckResult>((IObserver<CheckResult> observer) => { return Scheduler.Default.Schedule(item.Configuration.Interval, recursive => { CheckResult result = item.Check.Execute(); item.Check.PreviousStatus = item.Check.Status; item.Check.Status = result.Status; //We only push data if the status has changed if (item.Check.Status != item.Check.PreviousStatus) observer.OnNext(result); recursive(item.Configuration.Interval); }); }); ob.Subscribe((CheckResult result) => { CheckEvent ev = new CheckEvent() { Created = DateTime.Now, Id = item.Configuration.Id, Message = result.Message, Node = node, Notes = result.Notes, Source = Environment.MachineName, Status = result.Status }; publisher.Publish(ev); }); } } } Console.ReadLine(); }
static void Main(string[] args) { Configuration.Configuration configuration = Configuration.Configuration.GetConfiguration(); string node = configuration.Node; string dc = configuration.Dc; Publisher publisher = new Publisher(dc); TraceSource source = new TraceSource("Health.Checks"); foreach (PowerShellCheckItem item in PowerShellCheckFactory.Initialize(@"C:\Projects\Toyota.Tsusho\Health\Health.Tests\Checks").Values) { using (new TraceLogicalScope(source, String.Format("Processing {0}.", item.Configuration.Id))) { if (!item.Configuration.Disabled) { List<string> validationErrors = new List<string>(); item.Check.Validate(validationErrors); if (validationErrors.Count > 0) { foreach (string error in validationErrors) source.TraceData(TraceEventType.Warning, 0, error); throw new Exception("The Check contains validation errors."); } var ob = Observable.Create<CheckResult>((IObserver<CheckResult> observer) => { return Scheduler.Default.Schedule(item.Configuration.Interval, recursive => { CheckResult result = item.Check.Execute(); item.Check.PreviousStatus = item.Check.Status; item.Check.Status = result.Status; //We only push data if the status has changed if (item.Check.Status != item.Check.PreviousStatus) observer.OnNext(item.Check.Execute()); recursive(item.Configuration.Interval); }); }); ob.Subscribe((CheckResult result) => { CheckEvent ev = new CheckEvent() { Created = DateTime.Now, Id = item.Configuration.Id, Message = result.Message, Node = node, Notes = result.Notes, Source = Environment.MachineName, Status = result.Status }; publisher.Publish(ev); }); } } } Console.ReadLine(); }