pull/638/head
hunterlong 2020-06-10 10:46:32 -07:00
parent 8f85654040
commit a0698fbc95
8 changed files with 36 additions and 71 deletions

View File

@ -88,7 +88,6 @@ func TestInit(t *testing.T) {
err = i.Update() err = i.Update()
require.Nil(t, err) require.Nil(t, err)
assert.Equal(t, "Updated", i.Name) assert.Equal(t, "Updated", i.Name)
i.Close()
}) })
t.Run("Test Expected Time", func(t *testing.T) { 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) { t.Run("Test Checkin", func(t *testing.T) {
assert.Nil(t, db.Close()) assert.Nil(t, db.Close())
assert.Nil(t, dbHits.Close())
}) })
} }

View File

@ -2,6 +2,7 @@ package checkins
import ( import (
"github.com/statping/statping/database" "github.com/statping/statping/database"
"github.com/statping/statping/utils"
) )
var db database.Database var db database.Database
@ -31,7 +32,13 @@ func All() []*Checkin {
} }
func (c *Checkin) Create() error { func (c *Checkin) Create() error {
if c.ApiKey == "" {
c.ApiKey = utils.RandomString(32)
}
q := db.Create(c) q := db.Create(c)
c.Start()
go c.checkinRoutine()
return q.Error() return q.Error()
} }
@ -41,6 +48,11 @@ func (c *Checkin) Update() error {
} }
func (c *Checkin) Delete() 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() return q.Error()
} }

View File

@ -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
}

View File

@ -30,13 +30,14 @@ func (c *Checkin) Grace() time.Duration {
// Start will create a channel for the checkin checking go routine // Start will create a channel for the checkin checking go routine
func (c *Checkin) Start() { func (c *Checkin) Start() {
log.Infoln(fmt.Sprintf("Starting checkin routine: %s", c.Name))
c.Running = make(chan bool) c.Running = make(chan bool)
go c.checkinRoutine() go c.checkinRoutine()
} }
// Close will stop the checkin routine // Close will stop the checkin routine
func (c *Checkin) Close() { func (c *Checkin) Close() {
if c.IsRunning() { if c.Running != nil {
close(c.Running) close(c.Running)
} }
} }

View File

@ -25,9 +25,6 @@ func (c *Checkin) RecheckCheckinFailure(guard chan struct{}) {
// checkinRoutine for checking if the last Checkin was within its interval // checkinRoutine for checking if the last Checkin was within its interval
func (c *Checkin) checkinRoutine() { func (c *Checkin) checkinRoutine() {
if c == nil {
return
}
lastHit := c.LastHit() lastHit := c.LastHit()
if lastHit == nil { if lastHit == nil {
return return
@ -37,7 +34,7 @@ CheckinLoop:
for { for {
select { select {
case <-c.Running: 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 c.Failing = false
break CheckinLoop break CheckinLoop
case <-time.After(reCheck): case <-time.After(reCheck):
@ -58,6 +55,5 @@ CheckinLoop:
} }
reCheck = c.Period() reCheck = c.Period()
} }
continue
} }
} }

View File

@ -63,13 +63,30 @@ func (s *Service) Create() error {
return nil return nil
} }
func (s *Service) AfterCreate() error {
allServices[s.Id] = s
return nil
}
func (s *Service) Update() error { func (s *Service) Update() error {
q := db.Update(s) q := db.Update(s)
allServices[s.Id] = s
s.Close()
s.SleepDuration = s.Duration()
go ServiceCheckQueue(allServices[s.Id], true)
return q.Error() return q.Error()
} }
func (s *Service) Delete() 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() return q.Error()
} }

View File

@ -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
}

View File

@ -26,7 +26,7 @@ func initEnvs() {
Params.SetDefault("HOST", "0.0.0.0") Params.SetDefault("HOST", "0.0.0.0")
Params.SetDefault("DISABLE_HTTP", false) Params.SetDefault("DISABLE_HTTP", false)
Params.SetDefault("STATPING_DIR", defaultDir) Params.SetDefault("STATPING_DIR", defaultDir)
Params.SetDefault("GO_ENV", "") Params.SetDefault("GO_ENV", "production")
Params.SetDefault("DB_CONN", "") Params.SetDefault("DB_CONN", "")
Params.SetDefault("DISABLE_LOGS", false) Params.SetDefault("DISABLE_LOGS", false)
Params.SetDefault("USE_ASSETS", false) Params.SetDefault("USE_ASSETS", false)