From a0698fbc9545bc35de9b7faeb604554352422e94 Mon Sep 17 00:00:00 2001 From: hunterlong Date: Wed, 10 Jun 2020 10:46:32 -0700 Subject: [PATCH] tests --- types/checkins/checkins_test.go | 2 +- types/checkins/database.go | 14 ++++++++++++- types/checkins/hooks.go | 24 --------------------- types/checkins/methods.go | 3 ++- types/checkins/routine.go | 6 +----- types/services/database.go | 19 ++++++++++++++++- types/services/hooks.go | 37 --------------------------------- utils/env.go | 2 +- 8 files changed, 36 insertions(+), 71 deletions(-) delete mode 100644 types/checkins/hooks.go delete mode 100644 types/services/hooks.go diff --git a/types/checkins/checkins_test.go b/types/checkins/checkins_test.go index 82c79fce..a850c9bd 100644 --- a/types/checkins/checkins_test.go +++ b/types/checkins/checkins_test.go @@ -88,7 +88,6 @@ func TestInit(t *testing.T) { err = i.Update() require.Nil(t, err) assert.Equal(t, "Updated", i.Name) - i.Close() }) t.Run("Test Expected Time", func(t *testing.T) { @@ -115,6 +114,7 @@ func TestInit(t *testing.T) { t.Run("Test Checkin", func(t *testing.T) { assert.Nil(t, db.Close()) + assert.Nil(t, dbHits.Close()) }) } diff --git a/types/checkins/database.go b/types/checkins/database.go index 9c21abaf..be063613 100644 --- a/types/checkins/database.go +++ b/types/checkins/database.go @@ -2,6 +2,7 @@ package checkins import ( "github.com/statping/statping/database" + "github.com/statping/statping/utils" ) var db database.Database @@ -31,7 +32,13 @@ func All() []*Checkin { } func (c *Checkin) Create() error { + if c.ApiKey == "" { + c.ApiKey = utils.RandomString(32) + } q := db.Create(c) + + c.Start() + go c.checkinRoutine() return q.Error() } @@ -41,6 +48,11 @@ func (c *Checkin) Update() error { } func (c *Checkin) Delete() error { - q := db.Delete(c) + c.Close() + q := dbHits.Where("checkin = ?", c.Id).Delete(&CheckinHit{}) + if err := q.Error(); err != nil { + return err + } + q = db.Model(&Checkin{}).Delete(c) return q.Error() } diff --git a/types/checkins/hooks.go b/types/checkins/hooks.go deleted file mode 100644 index 38b90d92..00000000 --- a/types/checkins/hooks.go +++ /dev/null @@ -1,24 +0,0 @@ -package checkins - -import "github.com/statping/statping/utils" - -func (c *Checkin) BeforeCreate() error { - if c.ApiKey == "" { - c.ApiKey = utils.RandomString(32) - } - return nil -} - -func (c *Checkin) AfterCreate() error { - c.Start() - return nil -} - -func (c *Checkin) BeforeDelete() error { - c.Close() - q := dbHits.Where("checkin = ?", c.Id).Delete(&CheckinHit{}) - if err := q.Error(); err != nil { - return err - } - return nil -} diff --git a/types/checkins/methods.go b/types/checkins/methods.go index 31729f2b..fde9867f 100644 --- a/types/checkins/methods.go +++ b/types/checkins/methods.go @@ -30,13 +30,14 @@ func (c *Checkin) Grace() time.Duration { // Start will create a channel for the checkin checking go routine func (c *Checkin) Start() { + log.Infoln(fmt.Sprintf("Starting checkin routine: %s", c.Name)) c.Running = make(chan bool) go c.checkinRoutine() } // Close will stop the checkin routine func (c *Checkin) Close() { - if c.IsRunning() { + if c.Running != nil { close(c.Running) } } diff --git a/types/checkins/routine.go b/types/checkins/routine.go index 8e376571..e9f960df 100644 --- a/types/checkins/routine.go +++ b/types/checkins/routine.go @@ -25,9 +25,6 @@ func (c *Checkin) RecheckCheckinFailure(guard chan struct{}) { // checkinRoutine for checking if the last Checkin was within its interval func (c *Checkin) checkinRoutine() { - if c == nil { - return - } lastHit := c.LastHit() if lastHit == nil { return @@ -37,7 +34,7 @@ CheckinLoop: for { select { case <-c.Running: - log.Infoln(fmt.Sprintf("Stopping checkin routine: %v", c.Name)) + log.Infoln(fmt.Sprintf("Stopping checkin routine: %s", c.Name)) c.Failing = false break CheckinLoop case <-time.After(reCheck): @@ -58,6 +55,5 @@ CheckinLoop: } reCheck = c.Period() } - continue } } diff --git a/types/services/database.go b/types/services/database.go index 8f1aa79f..26dfcab9 100644 --- a/types/services/database.go +++ b/types/services/database.go @@ -63,13 +63,30 @@ func (s *Service) Create() error { return nil } +func (s *Service) AfterCreate() error { + allServices[s.Id] = s + return nil +} + func (s *Service) Update() error { q := db.Update(s) + allServices[s.Id] = s + s.Close() + s.SleepDuration = s.Duration() + go ServiceCheckQueue(allServices[s.Id], true) return q.Error() } func (s *Service) Delete() error { - q := db.Delete(s) + s.Close() + if err := s.DeleteFailures(); err != nil { + return err + } + if err := s.DeleteHits(); err != nil { + return err + } + delete(allServices, s.Id) + q := db.Model(&Service{}).Delete(s) return q.Error() } diff --git a/types/services/hooks.go b/types/services/hooks.go deleted file mode 100644 index 4c774c4b..00000000 --- a/types/services/hooks.go +++ /dev/null @@ -1,37 +0,0 @@ -package services - -import "github.com/statping/statping/utils" - -// BeforeCreate for Service will set CreatedAt to UTC -func (s *Service) BeforeCreate() (err error) { - if s.CreatedAt.IsZero() { - s.CreatedAt = utils.Now() - s.UpdatedAt = utils.Now() - } - return -} - -func (s *Service) AfterCreate() error { - allServices[s.Id] = s - return nil -} - -func (s *Service) AfterUpdate() error { - allServices[s.Id] = s - s.Close() - s.SleepDuration = s.Duration() - go ServiceCheckQueue(allServices[s.Id], true) - return nil -} - -func (s *Service) BeforeDelete() error { - s.Close() - if err := s.DeleteFailures(); err != nil { - return err - } - if err := s.DeleteHits(); err != nil { - return err - } - delete(allServices, s.Id) - return nil -} diff --git a/utils/env.go b/utils/env.go index 92a275db..4560da1f 100644 --- a/utils/env.go +++ b/utils/env.go @@ -26,7 +26,7 @@ func initEnvs() { Params.SetDefault("HOST", "0.0.0.0") Params.SetDefault("DISABLE_HTTP", false) Params.SetDefault("STATPING_DIR", defaultDir) - Params.SetDefault("GO_ENV", "") + Params.SetDefault("GO_ENV", "production") Params.SetDefault("DB_CONN", "") Params.SetDefault("DISABLE_LOGS", false) Params.SetDefault("USE_ASSETS", false)