2020-03-04 10:29:00 +00:00
|
|
|
package checkins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-04-04 00:28:09 +00:00
|
|
|
"github.com/statping/statping/utils"
|
2020-03-04 10:29:00 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Checkin) Expected() time.Duration {
|
|
|
|
last := c.LastHit()
|
2020-04-04 00:28:09 +00:00
|
|
|
now := utils.Now()
|
2020-03-04 10:29:00 +00:00
|
|
|
lastDir := now.Sub(last.CreatedAt)
|
|
|
|
sub := time.Duration(c.Period() - lastDir)
|
|
|
|
return sub
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Checkin) Period() time.Duration {
|
2020-04-16 17:32:54 +00:00
|
|
|
duration, _ := time.ParseDuration(fmt.Sprintf("%ds", c.Interval))
|
2020-04-17 03:21:17 +00:00
|
|
|
if duration.Seconds() <= 15 {
|
|
|
|
return 15 * time.Second
|
|
|
|
}
|
2020-03-04 10:29:00 +00:00
|
|
|
return duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grace will return the duration of the Checkin Grace Period (after service hasn't responded, wait a bit for a response)
|
|
|
|
func (c *Checkin) Grace() time.Duration {
|
|
|
|
duration, _ := time.ParseDuration(fmt.Sprintf("%vs", c.GracePeriod))
|
|
|
|
return duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start will create a channel for the checkin checking go routine
|
|
|
|
func (c *Checkin) Start() {
|
|
|
|
c.Running = make(chan bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close will stop the checkin routine
|
|
|
|
func (c *Checkin) Close() {
|
|
|
|
if c.IsRunning() {
|
|
|
|
close(c.Running)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsRunning returns true if the checkin go routine is running
|
|
|
|
func (c *Checkin) IsRunning() bool {
|
|
|
|
if c.Running == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-c.Running:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// String will return a Checkin API string
|
|
|
|
func (c *Checkin) String() string {
|
|
|
|
return c.ApiKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Checkin) Link() string {
|
|
|
|
return fmt.Sprintf("%v/checkin/%v", "DOMAINHERE", c.ApiKey)
|
|
|
|
}
|