/// <summary> /// Runs this the VCAP component. This method is non-blocking. /// </summary> public virtual void Run() { this.Discover = new Dictionary <string, object>() { { "type", this.ComponentType }, { "index", this.Index }, { "uuid", this.UUID }, { "host", string.Format(CultureInfo.InvariantCulture, "{0}:{1}", this.Host, this.Port) }, { "credentials", this.Authentication }, { "start", RubyCompatibility.DateTimeToRubyString(this.StartedAt = DateTime.Now) } }; // Listen for discovery requests VCAPReactor.OnComponentDiscover += delegate(string msg, string reply, string subject) { this.UpdateDiscoverUptime(); VCAPReactor.SendReply(reply, JsonConvertibleObject.SerializeToJson(this.Discover)); }; VCAPReactor.Start(); // Varz is customizable this.Varz = new Dictionary <string, object>(); foreach (string key in this.Discover.Keys) { this.Varz[key] = this.Discover[key]; } this.Varz["num_cores"] = Environment.ProcessorCount; // todo: change this to a more accurate method // consider: // PerformanceCounter upTime = new PerformanceCounter("System", "System Up Time"); // upTime.NextValue(); // TimeSpan ts2 = TimeSpan.FromSeconds(upTime.NextValue()); // Console.WriteLine("{0}d {1}h {2}m {3}s", ts2.Days, ts2.Hours, ts2.Minutes, ts2.Seconds); this.Varz["system_start"] = RubyCompatibility.DateTimeToRubyString(DateTime.Now.AddMilliseconds(-Environment.TickCount)); this.CpuPerformance = new PerformanceCounter(); this.CpuPerformance.CategoryName = "Processor Information"; this.CpuPerformance.CounterName = "% Processor Time"; this.CpuPerformance.InstanceName = "_Total"; this.CpuPerformance.NextValue(); this.Healthz = "ok\n"; this.StartHttpServer(); // Also announce ourselves on startup.. VCAPReactor.SendVCAPComponentAnnounce(JsonConvertibleObject.SerializeToJson(this.Discover)); }
/// <summary> /// Registers a component using the specified options. /// </summary> /// <param name="options">The options for the component.</param> public void Register(Dictionary <string, object> options) { if (options == null) { throw new ArgumentNullException("options"); } this.Varz = new Dictionary <string, object>(); string uuid = Guid.NewGuid().ToString("N"); object type = options["type"]; object index = options.ContainsKey("index") ? options["index"] : null; if (index != null) { uuid = string.Format(CultureInfo.InvariantCulture, "{0}-{1}", index, uuid); } string host = options["host"].ToString(); int port = options.ContainsKey("statusPort") ? (int)options["statusPort"] : 0; if (port < 1) { port = NetworkInterface.GrabEphemeralPort(); } Reactor nats = (Reactor)options["nats"]; //// string[] auth = new string[] //// { //// options.ContainsKey("user") ? options["user"].ToString() : string.Empty, //// options.ContainsKey("password") ? options["password"].ToString() : string.Empty //// }; string[] auth = new string[] { Credentials.GenerateCredential(32), Credentials.GenerateCredential(32) }; // Discover message limited this.discover = new Dictionary <string, object>() { { "type", type }, { "index", index }, { "uuid", uuid }, { "host", string.Format(CultureInfo.InvariantCulture, "{0}:{1}", host, port) }, { "credentials", auth }, { "start", RubyCompatibility.DateTimeToRubyString(DateTime.Now) } }; // Varz is customizable this.Varz = new Dictionary <string, object>(); foreach (string key in this.discover.Keys) { this.Varz[key] = this.discover[key]; } this.Varz["num_cores"] = Environment.ProcessorCount; // todo: change this to a more accurate method // consider: // PerformanceCounter upTime = new PerformanceCounter("System", "System Up Time"); // upTime.NextValue(); // TimeSpan ts2 = TimeSpan.FromSeconds(upTime.NextValue()); // Console.WriteLine("{0}d {1}h {2}m {3}s", ts2.Days, ts2.Hours, ts2.Minutes, ts2.Seconds); this.Varz["system_start"] = RubyCompatibility.DateTimeToRubyString(DateTime.Now.AddMilliseconds(-Environment.TickCount)); this.CpuPerformance = new PerformanceCounter(); this.CpuPerformance.CategoryName = "Processor Information"; this.CpuPerformance.CounterName = "% Processor Time"; this.CpuPerformance.InstanceName = "_Total"; this.CpuPerformance.NextValue(); this.Healthz = "ok\n"; this.StartHttpServer(host, port, auth); // Listen for discovery requests nats.Subscribe( "vcap.component.discover", delegate(string msg, string reply, string subject) { this.UpdateDiscoverUptime(); nats.Publish(reply, null, JsonConvertibleObject.SerializeToJson(this.discover)); }); // Also announce ourselves on startup.. nats.Publish("vcap.component.announce", null, msg: JsonConvertibleObject.SerializeToJson(this.discover)); }