-
Notifications
You must be signed in to change notification settings - Fork 25
/
types.go
84 lines (73 loc) · 1.85 KB
/
types.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
package aggregator
import "time"
const h24 = time.Duration(24) * time.Hour
// Action is used to manage a process
type Action uint8
const (
// StartAction is the action to start a process
StartAction Action = iota
// StopAction is the action to stop a process
StopAction
)
// String converts an action to a readable string
func (a Action) String() string {
switch a {
case StartAction:
return "start"
case StopAction:
return "stop"
}
return ""
}
// Status is the process status
type Status uint8
const (
// RunningStatus is the status of a running process
RunningStatus Status = iota
// StoppedStatus is the status of a stoppedprocess
StoppedStatus
)
// String converts a status to a readable string
func (s Status) String() string {
switch s {
case RunningStatus:
return "running"
case StoppedStatus:
return "stopped"
}
return ""
}
// FeedStatus status of a feed
type FeedStatus struct {
CheckedAt time.Time `json:"checked_at"`
EtagHeader string `json:"etag_header"`
LastModifiedHeader string `json:"last_modified_header"`
ExpiresHeader string `json:"expires_header"`
ErrorMsg string `json:"error_message"`
ErrorCount int `json:"error_count"`
}
// Err set error status message and counter
// Reset if err is null
func (fs *FeedStatus) Err(err error) {
if err != nil {
fs.ErrorCount++
fs.ErrorMsg = err.Error()
} else {
fs.ErrorCount = 0
fs.ErrorMsg = ""
}
}
// ComputeNextCheckDate computes next date to check regarding some rules
// The duration is multiply the number of error.
// The limit is h24
func (fs *FeedStatus) ComputeNextCheckDate(base time.Duration) time.Time {
deadline := time.Now().Add(h24)
if fs.ErrorCount > 0 {
base = base * time.Duration(fs.ErrorCount)
}
result := fs.CheckedAt.Add(base)
if result.After(deadline) {
result = deadline
}
return result
}