-
Notifications
You must be signed in to change notification settings - Fork 4
/
trigger.go
185 lines (151 loc) · 5.05 KB
/
trigger.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package trigger
import (
"context"
"fmt"
"github.com/Interhyp/metadata-service/internal/acorn/config"
"github.com/Interhyp/metadata-service/internal/acorn/service"
auzerolog "github.com/StephanHCB/go-autumn-logging-zerolog"
librepo "github.com/StephanHCB/go-backend-service-common/acorns/repository"
"github.com/StephanHCB/go-backend-service-common/web/middleware/requestid"
"github.com/robfig/cron/v3"
"github.com/rs/zerolog/log"
"time"
)
type Impl struct {
Configuration librepo.Configuration
CustomConfiguration config.CustomConfiguration
Logging librepo.Logging
Timestamp librepo.Timestamp
Updater service.Updater
LoggingCtx context.Context
Cron *cron.Cron
SkipStart bool
}
func New(
configuration librepo.Configuration,
customConfig config.CustomConfiguration,
logging librepo.Logging,
timestamp librepo.Timestamp,
updater service.Updater,
) service.Trigger {
return &Impl{
Configuration: configuration,
CustomConfiguration: customConfig,
Logging: logging,
Timestamp: timestamp,
Updater: updater,
}
}
func (s *Impl) IsTrigger() bool {
return true
}
func (s *Impl) Setup() error {
ctx := auzerolog.AddLoggerToCtx(context.Background())
if err := s.SetupTriggerCronjob(ctx); err != nil {
s.Logging.Logger().Ctx(ctx).Error().WithErr(err).Print("failed to set up trigger. BAILING OUT")
return err
}
s.Logging.Logger().Ctx(ctx).Info().Print("performing initial cache population...")
if err := s.PerformWithCancel(context.Background()); err != nil {
s.Logging.Logger().Ctx(ctx).Error().WithErr(err).Print("initial cache population failed. BAILING OUT")
return err
}
if !s.SkipStart {
s.Logging.Logger().Ctx(ctx).Info().Print("starting event receiver...")
if err := s.Updater.StartReceivingEvents(ctx); err != nil {
s.Logging.Logger().Ctx(ctx).Error().WithErr(err).Print("failed to start event receiver. BAILING OUT")
return err
}
s.Logging.Logger().Ctx(ctx).Info().Print("starting cron job...")
if err := s.StartCronjob(ctx); err != nil {
s.Logging.Logger().Ctx(ctx).Error().WithErr(err).Print("failed to start cron job. BAILING OUT")
return err
}
}
s.Logging.Logger().Ctx(ctx).Info().Print("successfully set up trigger")
return nil
}
func (s *Impl) Teardown() {
ctx := auzerolog.AddLoggerToCtx(context.Background())
s.Logging.Logger().Ctx(ctx).Info().Print("stopping cron job...")
if err := s.StopCronjob(ctx); err != nil {
s.Logging.Logger().Ctx(ctx).Error().WithErr(err).Print("failed to stop cron job. Continuing with teardown.")
// do NOT abort tear down cycle
return
}
s.Logging.Logger().Ctx(ctx).Info().Print("successfully tore down trigger")
}
// --- implement cron.Logger ---
func (s *Impl) Info(msg string, keysAndValues ...interface{}) {
logMessage := msg
for i, kv := range keysAndValues {
if i%2 == 0 {
logMessage += fmt.Sprintf(" %v=", kv)
} else {
logMessage += fmt.Sprintf("%v", kv)
}
}
s.Logging.Logger().Ctx(s.LoggingCtx).Info().Print("cronjob: " + logMessage)
}
func (s *Impl) Error(err error, msg string, keysAndValues ...interface{}) {
logMessage := msg
for i, kv := range keysAndValues {
if i%2 == 0 {
logMessage += fmt.Sprintf(" %v=", kv)
} else {
logMessage += fmt.Sprintf("%v", kv)
}
}
// no you don't get to log as Error severity!
s.Logging.Logger().Ctx(s.LoggingCtx).Warn().WithErr(err).Print("cronjob: " + logMessage)
}
// --- cron job ---
func (s *Impl) SetupTriggerCronjob(ctx context.Context) error {
s.LoggingCtx = ctx
s.Cron = cron.New(
cron.WithLogger(s),
cron.WithChain(
cron.SkipIfStillRunning(s),
),
)
cronSpec := fmt.Sprintf("*/%s * * * *", s.CustomConfiguration.UpdateJobIntervalCronPart())
_, err := s.Cron.AddFunc(cronSpec, func() { _ = s.PerformWithCancel(context.Background()) })
return err
}
func (s *Impl) StartCronjob(_ context.Context) error {
s.Cron.Start()
return nil
}
func (s *Impl) StopCronjob(_ context.Context) error {
stillRunningCtx := s.Cron.Stop()
select {
case <-stillRunningCtx.Done():
// all jobs have ended
break
case <-time.After(30 * time.Second):
// grace period end
}
return nil
}
func (s *Impl) PerformWithCancel(ctx context.Context) error {
// add custom request id
requestId := requestid.NewRequestID()
ctx = context.WithValue(ctx, requestid.RequestIDKey, requestId)
// add logger
loggerWithReqId := log.Logger.With().Str("trace.id", requestId).Logger()
ctx = loggerWithReqId.WithContext(ctx)
// add timeout
seconds := s.CustomConfiguration.UpdateJobTimeoutSeconds()
ctx, cancel := context.WithTimeout(ctx, time.Duration(seconds)*time.Second)
defer cancel()
started := time.Now()
s.Logging.Logger().Ctx(ctx).Info().Print("starting update")
err := s.Updater.PerformFullUpdate(ctx)
tookMs := time.Now().Sub(started).Milliseconds()
if err != nil {
s.Logging.Logger().Ctx(ctx).Warn().WithErr(err).Printf("finished periodic update with errors (%d ms runtime) - not all information was updated", tookMs)
} else {
s.Logging.Logger().Ctx(ctx).Info().Printf("finished update OK (%d ms runtime)", tookMs)
}
return err
}