-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.gen.go
108 lines (91 loc) · 2.1 KB
/
events.gen.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
// Code generated by github.com/dashotv/golem. DO NOT EDIT.
package app
import (
"context"
"fmt"
"time"
"github.com/nats-io/nats.go"
"go.uber.org/zap"
"github.com/dashotv/fae"
"github.com/dashotv/mercury"
)
func init() {
initializers = append(initializers, setupEvents)
healthchecks["events"] = checkEvents
starters = append(starters, startEvents)
}
type EventsChannel string
type EventsTopic string
func setupEvents(app *Application) error {
events, err := NewEvents(app)
if err != nil {
return err
}
app.Events = events
return nil
}
func startEvents(ctx context.Context, app *Application) error {
go app.Events.Start()
return nil
}
func checkEvents(app *Application) error {
switch app.Events.Merc.Status() {
case nats.CONNECTED:
return nil
default:
return fae.Errorf("nats status: %s", app.Events.Merc.Status())
}
}
type Events struct {
App *Application
Merc *mercury.Mercury
Log *zap.SugaredLogger
Releases chan *Release
}
func NewEvents(app *Application) (*Events, error) {
m, err := mercury.New("runic", app.Config.NatsURL)
if err != nil {
return nil, err
}
e := &Events{
App: app,
Merc: m,
Log: app.Log.Named("events"),
Releases: make(chan *Release),
}
if err := e.Merc.Sender("runic.releases", e.Releases); err != nil {
return nil, err
}
return e, nil
}
func (e *Events) Start() error {
e.Log.Debugf("starting events...")
// no receivers
return nil
}
func (e *Events) Send(topic EventsTopic, data any) error {
f := func() interface{} { return e.doSend(topic, data) }
err, ok := WithTimeout(f, time.Second*5)
if !ok {
e.Log.Errorf("timeout sending: %s", topic)
return fmt.Errorf("timeout sending: %s", topic)
}
if err != nil {
e.Log.Errorf("sending: %s", err)
return fae.Wrap(err.(error), "events.send")
}
return nil
}
func (e *Events) doSend(topic EventsTopic, data any) error {
switch topic {
case "runic.releases":
m, ok := data.(*Release)
if !ok {
return fae.Errorf("events.send: wrong data type: %t", data)
}
e.Releases <- m
default:
e.Log.Warnf("events.send: unknown topic: %s", topic)
}
return nil
}