mirror of
https://github.com/wahyd4/next-dashboard.git
synced 2026-08-09 04:26:06 +10:00
43 lines
1023 B
Go
43 lines
1023 B
Go
package workers
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/wahyd4/next-dashboard/models"
|
|
resty "gopkg.in/resty.v1"
|
|
)
|
|
|
|
type HTTPWorker struct {
|
|
DB *gorm.DB
|
|
}
|
|
|
|
func (worker *HTTPWorker) SetupHttpEndpointsWorker() {
|
|
frequency := models.GlobalConfig.Frequency
|
|
DoRequest:
|
|
for _, check := range models.GlobalConfig.HTTP.Checks {
|
|
go doHTTPChecks(worker.DB, check)
|
|
}
|
|
time.Sleep(time.Second * time.Duration(frequency))
|
|
goto DoRequest
|
|
}
|
|
|
|
func doHTTPChecks(db *gorm.DB, check *models.HTTPCheck) {
|
|
// logrus.Info("Request URL:" + check.URL)
|
|
resp, err := resty.R().Get(check.URL)
|
|
logrus.Infof("Request of URL: %s Status code is: %d, response time is: %f s", check.URL, resp.StatusCode(), resp.Time().Seconds())
|
|
db.Create(&models.HTTPHealth{
|
|
URL: check.URL,
|
|
Name: check.Name,
|
|
StatusCode: resp.StatusCode(),
|
|
ResponseTime: resp.Time().Seconds(),
|
|
})
|
|
|
|
if err != nil {
|
|
logrus.Errorf("Failed when do request HTTP endpoints health check: %v", err)
|
|
}
|
|
}
|