forked from TheCacophonyProject/event-reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
139 lines (119 loc) · 3.05 KB
/
main.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
event-reporter - report events to the Cacophony Project API.
Copyright (C) 2018, The Cacophony Project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"log"
"time"
arg "github.com/alexflint/go-arg"
"github.com/TheCacophonyProject/go-api"
"github.com/TheCacophonyProject/event-reporter/eventstore"
)
var version = "No version provided"
type argSpec struct {
ConfigFile string `arg:"-c,--config" help:"path to thermal-uploader configuration file"`
DBPath string `arg:"-d,--db" help:"path to state database"`
Interval time.Duration `arg:"--interval" help:"time between event reports"`
}
func (argSpec) Version() string {
return version
}
func procArgs() argSpec {
// Set argument default values.
args := argSpec{
ConfigFile: "/etc/thermal-uploader.yaml",
DBPath: "/var/run/event-reporter.db",
Interval: 30 * time.Minute,
}
arg.MustParse(&args)
return args
}
func main() {
err := runMain()
if err != nil {
log.Fatal(err.Error())
}
}
func runMain() error {
args := procArgs()
log.SetFlags(0) // Removes default timestamp flag
log.Printf("running version: %s", version)
store, err := eventstore.Open(args.DBPath)
if err != nil {
return err
}
defer store.Close()
apiClient, err := api.Open(args.ConfigFile)
if err != nil {
return err
}
err = StartService(store)
if err != nil {
return err
}
for {
events, err := store.All()
if err != nil {
return err
}
log.Printf("%d events to send", len(events))
sendEvents(apiClient, store, events)
time.Sleep(args.Interval)
}
}
func sendEvents(
apiClient *api.CacophonyAPI,
store *eventstore.EventStore,
events []eventstore.EventTimes,
) {
var tempErrs []error
var permErrs []error
for _, event := range events {
err := apiClient.ReportEvent(event.Details, event.Timestamps)
if err != nil {
if api.IsPermanentError(err) {
permErrs = append(permErrs, err)
store.Discard(event)
} else {
tempErrs = append(tempErrs, err)
}
} else {
store.Discard(event)
}
}
logLastErrors("temporary", tempErrs)
logLastErrors("permanent", permErrs)
}
func logLastErrors(label string, errs []error) {
if len(errs) < 1 {
return
}
log.Printf("%d %s error%s occurred during reporting. Most recent:", len(errs), label, plural(len(errs)))
for _, err := range last5Errs(errs) {
log.Printf(" %v", err)
}
}
func last5Errs(errs []error) []error {
i := len(errs) - 5
if i < 0 {
i = 0
}
return errs[i:]
}
func plural(n int) string {
if n == 1 {
return ""
}
return "s"
}