-
Notifications
You must be signed in to change notification settings - Fork 348
/
worker.go
80 lines (71 loc) · 1.9 KB
/
worker.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
package apns
import (
"context"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/pkg/errors"
"github.com/micromdm/micromdm/mdm"
"github.com/micromdm/micromdm/platform/pubsub"
)
type WorkerStore interface {
Save(context.Context, *PushInfo) error
}
type Worker struct {
db WorkerStore
sub pubsub.Subscriber
logger log.Logger
}
func NewWorker(db WorkerStore, subscriber pubsub.Subscriber, logger log.Logger) *Worker {
return &Worker{
db: db,
sub: subscriber,
logger: logger,
}
}
func (w *Worker) Run(ctx context.Context) error {
const subscription = "pushinfo_worker"
tokenUpdateEvents, err := w.sub.Subscribe(ctx, subscription, mdm.TokenUpdateTopic)
if err != nil {
return errors.Wrapf(err,
"subscribing %s to %s topic", subscription, mdm.TokenUpdateTopic)
}
for {
var err error
select {
case <-ctx.Done():
return ctx.Err()
case event := <-tokenUpdateEvents:
err = w.updatePushInfoFromTokenUpdate(ctx, event.Message)
}
if err != nil {
level.Info(w.logger).Log(
"msg", "update pushinfo from event",
"err", err,
)
continue
}
}
}
func (w *Worker) updatePushInfoFromTokenUpdate(ctx context.Context, message []byte) error {
var ev mdm.CheckinEvent
if err := mdm.UnmarshalCheckinEvent(message, &ev); err != nil {
return errors.Wrap(err, "unmarshal pushinfo event")
}
info := PushInfo{
UDID: ev.Command.UDID,
Token: ev.Command.Token.String(),
PushMagic: ev.Command.PushMagic,
MDMTopic: ev.Command.Topic,
}
// UDID is the primary key for storing the APNS values.
// For MDM managed users, use the UserID instead,
// and for BYOD User Enrollment, use the EnrollmentID.
if ev.Command.UserID != "" {
info.UDID = ev.Command.UserID
}
if ev.Command.EnrollmentID != "" {
info.UDID = ev.Command.EnrollmentID
}
err := w.db.Save(ctx, &info)
return errors.Wrapf(err, "saving pushinfo for udid=%s", info.UDID)
}