/// <summary>
        /// Method registers application instance to eureka service with ability to provide additional healthcheck callback.
        /// </summary>
        /// <param name="healthCheck">External function performing additional validation for the service health.
        /// Function must return boolean value indicating the service health.
        /// If FALSE value returned, then no HEALTHY heartbeat is being sent.</param>
        /// <returns>Returns boolean indicator of success.</returns>
        public bool RegisterApplicationInstance(Func <bool> healthCheck)
        {
            bool result = false;

            try
            {
                Trace.TraceInformation("Registering instance with EUREKA service...");
                result = EurekaClientExecutor.Wrap(this.eureka).ExecuteWithRetry(() => this.eureka.Register(DataCenterMetadata), this.AppConfig.EurekaServiceUrls);

                if (result)
                {
                    Trace.TraceInformation("Initializing instance heartbeat...");
                    this.heartbeat = new HeartbeatObserver(this.eureka, this.DataCenterMetadata, this.AppConfig.EurekaServiceUrls);
                    if (healthCheck != null)
                    {
                        this.heartbeat.OnHealthCheck = healthCheck;
                    }
                    this.heartbeat.InitializeAndStartTimer();
                }
            }
            catch (Exception ex)
            {
                if (this.eureka != null)
                {
                    EurekaClientExecutor.Wrap(this.eureka).ExecuteWithRetry(() => this.eureka.Unregister(DataCenterMetadata), this.AppConfig.EurekaServiceUrls);
                }
                if (this.heartbeat != null)
                {
                    this.heartbeat.StopTimer();
                }
                throw ex;
            }
            return(result);
        }
        /// <summary>
        /// Method unregisters application instance from eureka service.
        /// </summary>
        /// <returns>Returns boolean indicator of success.</returns>
        public bool UnregisterApplicationInstance()
        {
            bool result = EurekaClientExecutor.Wrap(this.eureka).ExecuteWithRetry(() => this.eureka.Unregister(DataCenterMetadata), this.AppConfig.EurekaServiceUrls);

            if (this.heartbeat != null)
            {
                this.heartbeat.StopTimer();
            }
            return(result);
        }
        /// <summary>
        /// Method takes application instance back to service at the eureka service.
        /// </summary>
        /// <returns>Returns boolean indicator of success.</returns>
        public bool PutInstanceToService()
        {
            bool result = EurekaClientExecutor.Wrap(this.eureka).ExecuteWithRetry(() => this.eureka.PutInstanceToService(DataCenterMetadata), this.AppConfig.EurekaServiceUrls);

            if (this.heartbeat != null)
            {
                this.heartbeat.StartTimer();
            }
            return(result);
        }
Ejemplo n.º 4
0
 private void TimerWorker(object sender, System.Timers.ElapsedEventArgs e)
 {
     try
     {
         if (this.OnHealthCheck != null)
         {
             if (!this.OnHealthCheck())
             {
                 timer.Start();
                 return;
             }
         }
         EurekaClientExecutor.Wrap(this.eureka).ExecuteWithRetry(() => this.eureka.SendHeartbeat(DataCenterMetadata), this.EurekaServiceUrls);
         timer.Start();
     }
     catch (Exception ex)
     {
         Trace.TraceError("Exception: " + ex.ToString());
     }
 }