-
Notifications
You must be signed in to change notification settings - Fork 3
/
update.go
77 lines (63 loc) · 1.65 KB
/
update.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
package scheduled
import (
"time"
"github.com/bwmarrin/discordgo"
"github.com/ritsec/ops-bot-iii/helpers"
"github.com/ritsec/ops-bot-iii/logging"
"github.com/robfig/cron"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)
// updates repo, build binary, and exists if update is available
func updateOBIII(s *discordgo.Session, ctx ddtrace.SpanContext) {
span := tracer.StartSpan(
"commands.scheduled.update:updateOBIII",
tracer.ResourceName("Scheduled.Update:updateOBIII"),
tracer.ChildOf(ctx),
)
defer span.Finish()
logging.Debug(s, "Checking for update", nil, span)
update, err := helpers.UpdateMainBranch()
if err != nil {
logging.Error(s, err.Error(), nil, span)
return
}
if update {
logging.Critical(s, "Update available; updating", nil, span)
err = helpers.BuildOBIII()
if err != nil {
logging.Error(s, err.Error(), nil, span)
return
}
err = helpers.Exit()
if err != nil {
logging.Error(s, err.Error(), nil, span)
return
}
} else {
logging.Debug(s, "No update available", nil, span)
}
}
// checks for update every day at 2am and runs if available
func Update(s *discordgo.Session, quit chan interface{}) error {
span := tracer.StartSpan(
"commands.scheduled.update:Update",
tracer.ResourceName("Scheduled.Update"),
)
defer span.Finish()
est, err := time.LoadLocation("America/New_York")
if err != nil {
logging.Error(s, err.Error(), nil, span)
return err
}
c := cron.NewWithLocation(est)
// every day at 2am
err = c.AddFunc("0 0 2 * * *", func() { updateOBIII(s, span.Context()) })
if err != nil {
return err
}
c.Start()
<-quit
c.Stop()
return nil
}