forked from statping/statping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkin.go
110 lines (95 loc) · 2.07 KB
/
checkin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package core
import (
"fmt"
"github.com/ararog/timeago"
"github.com/hunterlong/statup/types"
"github.com/hunterlong/statup/utils"
"time"
)
type Checkin types.Checkin
func (c *Checkin) String() string {
return c.Api
}
func FindCheckin(api string) *Checkin {
for _, s := range CoreApp.Services {
for _, c := range s.Checkins {
if c.Api == api {
return c
}
}
}
return nil
}
func (s *Service) SelectAllCheckins() []*Checkin {
var checkins []*Checkin
col := DbSession.Collection("checkins").Find("service", s.Id).OrderBy("-id")
col.All(&checkins)
s.Checkins = checkins
return checkins
}
func (u *Checkin) Create() (int64, error) {
u.CreatedAt = time.Now()
uuid, err := DbSession.Collection("checkins").Insert(u)
if uuid == nil {
utils.Log(2, err)
return 0, err
}
fmt.Println("new checkin: ", uuid)
return uuid.(int64), err
}
func SelectCheckinApi(api string) *Checkin {
var checkin *Checkin
DbSession.Collection("checkins").Find("api", api).One(&checkin)
return checkin
}
func (c *Checkin) Receivehit() {
c.Hits++
c.Last = time.Now()
}
func (c *Checkin) RecheckCheckinFailure(guard chan struct{}) {
between := time.Now().Sub(c.Last).Seconds()
if between > float64(c.Interval) {
fmt.Println("rechecking every 15 seconds!")
c.CreateFailure()
time.Sleep(15 * time.Second)
guard <- struct{}{}
c.RecheckCheckinFailure(guard)
} else {
fmt.Println("i recovered!!")
}
<-guard
}
func (f *Checkin) CreateFailure() {
}
func (f *Checkin) Ago() string {
got, _ := timeago.TimeAgoWithTime(time.Now(), f.Last)
return got
}
func (c *Checkin) Run() {
if c.Interval == 0 {
return
}
fmt.Println("checking: ", c.Api)
between := time.Now().Sub(c.Last).Seconds()
if between > float64(c.Interval) {
guard := make(chan struct{})
c.RecheckCheckinFailure(guard)
<-guard
}
time.Sleep(1 * time.Second)
c.Run()
}
func (s *Service) StartCheckins() {
for _, c := range s.Checkins {
checkin := c
go checkin.Run()
}
}
func CheckinProcess() {
for _, s := range CoreApp.Services {
for _, c := range s.Checkins {
checkin := c
go checkin.Run()
}
}
}