public override Task PublishAsync(HealthReport report, CancellationToken cancellationToken)
        {
            Task.Run(() =>
            {
                var alert = this.HasToPublishAlert(report);

                if (alert)
                {
                    using var collector = new CollectorConfiguration()
                                          .Tag.With("name", _healthCheck.Name)
                                          .Batch.AtInterval(TimeSpan.FromSeconds(2))
                                          .WriteTo.InfluxDB(_influxDBTransportSettings.Host, _influxDBTransportSettings.Database)
                                          .CreateCollector();

                    var entry = report
                                .Entries
                                .FirstOrDefault(x => x.Key == this._healthCheck.Name);

                    var tags = new Dictionary <string, string>()
                    {
                        { "endpoint", _healthCheck.EndpointOrHost ?? _healthCheck.ConnectionString }
                    };

                    var fields = new Dictionary <string, object>()
                    {
                        { "status", (int)entry.Value.Status },
                        { "error", entry.Value.Exception },
                        { "responsetime", entry.Value.Duration.Milliseconds }
                    };

                    foreach (var valueTag in entry.Value.Data)
                    {
                        fields.Add(valueTag.Key, valueTag.Value);
                    }

                    collector.Write("health_check", fields, tags);
                }
            });

            return(Task.CompletedTask);
        }
Exemplo n.º 2
0
        private void WorkThread()
        {
            bool alreadyForceStopped = false;

            runningLock.EnterWriteLock();
            if (this.state != ServiceState.ForceStop)
            {
                this.state = ServiceState.Running;
            }
            else
            {
                alreadyForceStopped = true;
            }
            runningLock.ExitWriteLock();

            //updates the state every service but only running the command every 5 times
            int skip        = 5;
            int skipcounter = 0;


            if (!alreadyForceStopped)
            {
                WriteEntryInLog(String.Format("Starting with {0} on db {1}", influxconfig.GetServiceString(), influxconfig.GetDB()));

                try
                {
                    MetricsCollector collector = new CollectorConfiguration().Tag.With("host", Environment.GetEnvironmentVariable("COMPUTERNAME")).Batch.AtInterval(TimeSpan.FromSeconds(2)).WriteTo.InfluxDB(influxconfig.GetServiceString(), influxconfig.GetDB()).CreateCollector();


                    var getjobcode = GetEmbededScript("getjobs");
                    psinstance.AddScript(getjobcode);
                    psinstance.Invoke();

                    if (psinstance.Streams.Error.Count > 0)
                    {
                        throw new Exception("Unable to fetch initial jobs " + psinstance.Streams.Error[0]);
                    }
                    psinstance.Streams.ClearStreams();

                    var getjobstatuscode = GetEmbededScript("getjobstatus");

                    ServiceState cstate = getState();

                    for (; cstate != ServiceState.Stopped && cstate != ServiceState.ForceStop; cstate = getState())
                    {
                        if (cstate != ServiceState.Paused && (skipcounter % skip == 0))
                        {
                            psinstance.AddScript(getjobstatuscode);
                            var jobstats = psinstance.Invoke();
                            if (psinstance.Streams.Error.Count > 0)
                            {
                                WriteEntryInLog("Failed Collection " + psinstance.Streams.Error[0]);
                            }
                            else
                            {
                                foreach (PSObject jobstat in jobstats)
                                {
                                    //@{jobname=$job.name;speed=$speed;progress=$progress;state=$state;running=$running}
                                    collector.Write("jobprogress",
                                                    new Dictionary <string, object>
                                    {
                                        { "speed", Int64.Parse(jobstat.Properties["speed"].Value.ToString()) },
                                        { "progress", Int32.Parse(jobstat.Properties["progress"].Value.ToString()) },
                                        { "running", Int32.Parse(jobstat.Properties["running"].Value.ToString()) },
                                        { "state", jobstat.Properties["state"].Value.ToString() },
                                    },
                                                    new Dictionary <string, string>
                                    {
                                        { "jobname", jobstat.Properties["jobname"].Value.ToString() }
                                    }
                                                    );
                                }
                            }
                            psinstance.Streams.ClearStreams();

                            //make sure int never overflows
                            if (skipcounter >= 120)
                            {
                                WriteEntryInLog(String.Format("Heartbeating, service still working"));
                                skipcounter = 0;
                            }
                        }
                        skipcounter++;
                        Thread.Sleep(1000);
                    }

                    if (cstate == ServiceState.ForceStop)
                    {
                        this.Stop();
                    }
                }
                catch (Exception e)
                {
                    EventLog.WriteEntry(sSource, String.Format("Failure of some kind {0}", e.Message));
                }
            }
            else
            {
                this.Stop();
            }
        }