diff --git a/.gitignore b/.gitignore index ae61a010..fd1d97de 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ source/rice-box.go sass .DS_Store source/css/base.css.map +/dev/test/node_modules/ diff --git a/.travis.yml b/.travis.yml index 0451fd59..d477e825 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,8 @@ go_import_path: github.com/hunterlong/statup cache: directories: - $GOPATH/pkg/dep + - ~/.npm + - ~/.cache sudo: required diff --git a/cmd/cli.go b/cmd/cli.go index 96d4fe77..132ec510 100644 --- a/cmd/cli.go +++ b/cmd/cli.go @@ -144,8 +144,8 @@ func RunOnce() { if err != nil { utils.Log(4, err) } - for _, s := range core.CoreApp.DbServices { - out := core.ServiceCheck(core.ReturnService(s), true) + for _, s := range core.CoreApp.Services() { + out := s.Check(true) fmt.Printf(" Service %v | URL: %v | Latency: %0.0fms | Online: %v\n", out.Name, out.Domain, (out.Latency * 1000), out.Online) } } diff --git a/cmd/main_test.go b/cmd/main_test.go index caa3ca2a..abca777b 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -104,7 +104,7 @@ func TestRunAll(t *testing.T) { t.Run(dbt+" Select Users", func(t *testing.T) { RunUser_SelectAll(t) }) - t.Run(dbt+" Select DbServices", func(t *testing.T) { + t.Run(dbt+" Select Services", func(t *testing.T) { RunSelectAllServices(t) }) t.Run(dbt+" Select One Service", func(t *testing.T) { @@ -353,7 +353,7 @@ func RunOneService_Check(t *testing.T) { } func RunService_Create(t *testing.T) { - service := &core.Service{Service: &types.Service{ + service := core.ReturnService(&types.Service{ Name: "test service", Domain: "https://google.com", ExpectedStatus: 200, @@ -362,7 +362,7 @@ func RunService_Create(t *testing.T) { Type: "http", Method: "GET", Timeout: 30, - }} + }) id, err := service.Create() assert.Nil(t, err) assert.Equal(t, int64(6), id) @@ -401,7 +401,7 @@ func RunService_GraphData(t *testing.T) { } func RunBadService_Create(t *testing.T) { - service := &core.Service{Service: &types.Service{ + service := core.ReturnService(&types.Service{ Name: "Bad Service", Domain: "https://9839f83h72gey2g29278hd2od2d.com", ExpectedStatus: 200, @@ -410,7 +410,7 @@ func RunBadService_Create(t *testing.T) { Type: "http", Method: "GET", Timeout: 30, - }} + }) id, err := service.Create() assert.Nil(t, err) assert.Equal(t, int64(7), id) @@ -421,7 +421,7 @@ func RunBadService_Check(t *testing.T) { assert.NotNil(t, service) assert.Equal(t, "Bad Service", service.Name) for i := 0; i <= 10; i++ { - core.ServiceHTTPCheck(service, true) + service.Check(true) } assert.True(t, service.IsRunning()) } @@ -439,18 +439,13 @@ func RunDeleteService(t *testing.T) { } func RunCreateService_Hits(t *testing.T) { - services, err := core.CoreApp.SelectAllServices() - assert.Nil(t, err) + services := core.CoreApp.Services() assert.NotNil(t, services) assert.Equal(t, 6, len(services)) for i := 0; i <= 15; i++ { for _, s := range services { var service *core.Service - if s.Type == "http" { - service = core.ServiceHTTPCheck(core.ReturnService(s), true) - } else { - service = core.ServiceTCPCheck(core.ReturnService(s), true) - } + service = s.Check(true) assert.NotNil(t, service) } } diff --git a/core/checker.go b/core/checker.go index f421fadf..778ea61e 100644 --- a/core/checker.go +++ b/core/checker.go @@ -28,15 +28,12 @@ import ( "time" ) -type FailureData types.FailureData - func CheckServices() { CoreApp.SelectAllServices() utils.Log(1, fmt.Sprintf("Starting monitoring process for %v DbServices", len(CoreApp.DbServices))) - for _, ser := range CoreApp.DbServices { + for _, ser := range CoreApp.Services() { //go obj.StartCheckins() - s := ReturnService(ser) - go s.CheckQueue(true) + go ser.CheckQueue(true) } } @@ -51,7 +48,7 @@ CheckLoop: break CheckLoop default: utils.Log(1, fmt.Sprintf("Checking service: %v", s.Name)) - ServiceCheck(s, record) + s.Check(record) // Set next time checkpoint and maybe sleep. s.Checkpoint = s.Checkpoint.Add(time.Duration(s.Interval) * time.Second) if sleepDuration := s.Checkpoint.Sub(time.Now()); sleepDuration > 0 { @@ -62,7 +59,7 @@ CheckLoop: } } -func DNSCheck(s *Service) (float64, error) { +func (s *Service) dnsCheck() (float64, error) { t1 := time.Now() url, err := url.Parse(s.Domain) if err != nil { @@ -77,7 +74,7 @@ func DNSCheck(s *Service) (float64, error) { return subTime, err } -func ServiceTCPCheck(s *Service, record bool) *Service { +func (s *Service) checkTcp(record bool) *Service { t1 := time.Now() domain := fmt.Sprintf("%v", s.Domain) if s.Port != 0 { @@ -105,18 +102,18 @@ func ServiceTCPCheck(s *Service, record bool) *Service { return s } -func ServiceCheck(s *Service, record bool) *Service { +func (s *Service) Check(record bool) *Service { switch s.Type { case "http": - ServiceHTTPCheck(s, record) + s.checkHttp(record) case "tcp": - ServiceTCPCheck(s, record) + s.checkTcp(record) } return s } -func ServiceHTTPCheck(s *Service, record bool) *Service { - dnsLookup, err := DNSCheck(s) +func (s *Service) checkHttp(record bool) *Service { + dnsLookup, err := s.dnsCheck() if err != nil { if record { RecordFailure(s, fmt.Sprintf("Could not get IP address for domain %v, %v", s.Domain, err)) diff --git a/core/services_test.go b/core/services_test.go index 640d7810..15158dfa 100644 --- a/core/services_test.go +++ b/core/services_test.go @@ -67,7 +67,7 @@ func TestUpdateAllServices(t *testing.T) { func TestServiceHTTPCheck(t *testing.T) { service := SelectService(1) - checked := ServiceCheck(service, true) + checked := service.Check(true) assert.Equal(t, "Changed Updated Google", checked.Name) assert.True(t, checked.Online) } @@ -82,7 +82,7 @@ func TestCheckHTTPService(t *testing.T) { func TestServiceTCPCheck(t *testing.T) { service := SelectService(5) - checked := ServiceCheck(service, true) + checked := service.Check(true) assert.Equal(t, "Changed Google DNS", checked.Name) assert.True(t, checked.Online) } @@ -189,7 +189,7 @@ func TestCreateFailingHTTPService(t *testing.T) { func TestServiceFailedCheck(t *testing.T) { service := SelectService(7) - checked := ServiceCheck(service, true) + checked := service.Check(true) assert.Equal(t, "Bad URL", checked.Name) assert.False(t, checked.Online) } @@ -213,7 +213,7 @@ func TestCreateFailingTCPService(t *testing.T) { func TestServiceFailedTCPCheck(t *testing.T) { service := SelectService(8) - checked := ServiceCheck(service, true) + checked := service.Check(true) assert.Equal(t, "Bad TCP", checked.Name) assert.False(t, checked.Online) } @@ -254,7 +254,36 @@ func TestServiceCloseRoutine(t *testing.T) { s.Interval = 1 s.Start() assert.True(t, s.IsRunning()) + t.Log(s.Checkpoint) go s.CheckQueue(false) + t.Log(s.Checkpoint) + time.Sleep(5 * time.Second) + t.Log(s.Checkpoint) + assert.True(t, s.IsRunning()) + s.Close() + assert.False(t, s.IsRunning()) + s.Close() + assert.False(t, s.IsRunning()) +} + +func TestServiceCheckQueue(t *testing.T) { + s := ReturnService(new(types.Service)) + s.Name = "example" + s.Domain = "https://google.com" + s.Type = "http" + s.Method = "GET" + s.ExpectedStatus = 200 + s.Interval = 1 + s.Start() + assert.True(t, s.IsRunning()) + go s.CheckQueue(false) + + go func() { + time.Sleep(5 * time.Second) + t.Log(s.Checkpoint) + time.Sleep(6 * time.Second) + }() + time.Sleep(5 * time.Second) assert.True(t, s.IsRunning()) s.Close() diff --git a/dev/Dockerfile-dev b/dev/Dockerfile-dev index d17d756a..1cbd35be 100644 --- a/dev/Dockerfile-dev +++ b/dev/Dockerfile-dev @@ -19,5 +19,7 @@ ENV IS_DOCKER=true RUN make dev-deps RUN make install +WORKDIR /app +VOLUME /app EXPOSE 8080 -ENTRYPOINT make run \ No newline at end of file +ENTRYPOINT statup \ No newline at end of file diff --git a/types/service.go b/types/service.go index d53d81b2..eb428de2 100644 --- a/types/service.go +++ b/types/service.go @@ -76,6 +76,9 @@ type ServiceInterface interface { SelectHitsGroupBy(string) ([]*Hit, error) // Go Routines CheckQueue(bool) + Check(bool) *Service + checkHttp(bool) *Service + checkTcp(bool) *Service // Checkin functions AllCheckins() []*Checkin } diff --git a/types/types.go b/types/types.go index 70f6977b..2ea0ee67 100644 --- a/types/types.go +++ b/types/types.go @@ -106,7 +106,3 @@ type PluginJSON struct { Author string `json:"author"` Namespace string `json:"namespace"` } - -type FailureData struct { - Issue string -} diff --git a/utils/log.go b/utils/log.go index 781e2c6a..d4e3d3b8 100644 --- a/utils/log.go +++ b/utils/log.go @@ -22,8 +22,8 @@ import ( "net/http" "os" "os/signal" - "syscall" "sync" + "syscall" ) var ( @@ -140,7 +140,7 @@ func GetLastLine() *LogRow { LockLines.Lock() defer LockLines.Unlock() if len(LastLines) > 0 { - return LastLines[len(LastLines) - 1] + return LastLines[len(LastLines)-1] } return nil } diff --git a/utils/logRow.go b/utils/logRow.go index 207c8a4b..08d641fa 100644 --- a/utils/logRow.go +++ b/utils/logRow.go @@ -16,13 +16,13 @@ package utils import ( - "time" "fmt" + "time" ) type LogRow struct { - Date time.Time - Line interface{} + Date time.Time + Line interface{} } func NewLogRow(line interface{}) (logRow *LogRow) {