modularize

This commit is contained in:
Matej Kramny
2015-03-15 20:58:14 +01:00
parent 7b3d6eba89
commit 1cadc9b3b3
4 changed files with 110 additions and 58 deletions

49
cachet/monitor.go Normal file
View File

@@ -0,0 +1,49 @@
package cachet
import (
"fmt"
"time"
"net/http"
)
const timeout = time.Duration(time.Second)
type Monitor struct {
Url string `json:"url"`
MetricId int `json:"metric_id"`
}
func (monitor *Monitor) Run() {
reqStart := getMs()
err := monitor.doRequest()
lag := getMs() - reqStart
failed := false
if err != nil {
failed = true
}
if failed == true {
fmt.Println("Req failed")
}
SendMetric(1, lag)
}
func (monitor *Monitor) doRequest() error {
client := &http.Client{
Timeout: timeout,
}
resp, err := client.Get(monitor.Url) // http://127.0.0.1:1337
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
func getMs() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}