generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
notify.go
182 lines (163 loc) · 5.08 KB
/
notify.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
package dal
import (
"context"
"encoding"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/alecthomas/types"
"github.com/jackc/pgx/v5"
"github.com/jpillora/backoff"
"github.com/TBD54566975/ftl/backend/common/log"
"github.com/TBD54566975/ftl/backend/common/model"
"github.com/TBD54566975/ftl/backend/schema"
)
// NotificationPayload is a row from the database.
//
//sumtype:decl
type NotificationPayload interface{ notification() }
// A Notification from the database.
type Notification[T NotificationPayload, Key any, KeyP interface {
*Key
encoding.TextUnmarshaler
}] struct {
Deleted types.Option[Key] // If present the object was deleted.
Message types.Option[T]
}
func (n Notification[T, Key, KeyP]) String() string {
if key, ok := n.Deleted.Get(); ok {
return fmt.Sprintf("deleted %v", key)
}
return fmt.Sprintf("message %v", n.Message)
}
// DeploymentNotification is a notification from the database when a deployment changes.
type DeploymentNotification = Notification[Deployment, model.DeploymentName, *model.DeploymentName]
// See JSON structure in SQL schema
type event struct {
Table string `json:"table"`
Action string `json:"action"`
New string `json:"new,omitempty"`
Old string `json:"old,omitempty"`
// Optional field for conveying deletion metadata.
Deleted json.RawMessage `json:"deleted,omitempty"`
}
func (d *DAL) runListener(ctx context.Context, conn *pgx.Conn) {
logger := log.FromContext(ctx)
logger.Infof("Starting DB listener")
// Wait for the notification channel to be ready.
retry := backoff.Backoff{}
for {
_, err := conn.Exec(ctx, "LISTEN notify_events")
if err == nil {
break
}
logger.Errorf(err, "failed to LISTEN notify_events")
time.Sleep(retry.Duration())
}
retry.Reset()
// Main loop for listening to notifications.
for {
delay := time.Millisecond * 100
logger.Debugf("Waiting for notification")
notification, err := waitForNotification(ctx, conn)
if err == nil {
logger.Debugf("Publishing notification: %s", notification)
err = d.publishNotification(ctx, notification, logger)
}
if err != nil {
logger.Errorf(err, "Failed to receive notification")
delay = retry.Duration()
} else {
retry.Reset()
}
select {
case <-ctx.Done():
return
case <-time.After(delay):
}
}
}
func (d *DAL) publishNotification(ctx context.Context, notification event, logger *log.Logger) error {
switch notification.Table {
case "deployments":
deployment, err := decodeNotification(notification, func(key model.DeploymentName) (Deployment, types.Option[model.DeploymentName], error) {
row, err := d.db.GetDeployment(ctx, key)
if err != nil {
return Deployment{}, types.None[model.DeploymentName](), translatePGError(err)
}
moduleSchema, err := schema.ModuleFromBytes(row.Deployment.Schema)
if err != nil {
return Deployment{}, types.None[model.DeploymentName](), err
}
return Deployment{
CreatedAt: row.Deployment.CreatedAt,
Name: row.Deployment.Name,
Module: row.ModuleName,
Schema: moduleSchema,
MinReplicas: int(row.Deployment.MinReplicas),
Language: row.Language,
}, types.None[model.DeploymentName](), nil
})
if err != nil {
return err
}
logger.Debugf("Deployment notification: %s", deployment)
d.DeploymentChanges.Publish(deployment)
default:
panic(fmt.Sprintf("unknown table %q in DB notification", notification.Table))
}
return nil
}
// This function takes a notification from the database and translates it into
// a concrete Notification value.
//
// The translate function is called to translate the key into a concrete value
// OR a delete notification.
func decodeNotification[K any, T NotificationPayload, KP interface {
*K
encoding.TextUnmarshaler
}](notification event, translate func(key K) (T, types.Option[K], error)) (Notification[T, K, KP], error) {
var (
deleted types.Option[K]
message types.Option[T]
)
if notification.Action == "DELETE" {
var deletedKey K
var deletedKeyP KP = &deletedKey
if err := deletedKeyP.UnmarshalText([]byte(notification.Old)); err != nil {
return Notification[T, K, KP]{}, fmt.Errorf("%s: %w", "failed to unmarshal notification key", err)
}
deleted = types.Some(deletedKey)
} else {
var newKey K
var newKeyP KP = &newKey
if err := newKeyP.UnmarshalText([]byte(notification.New)); err != nil {
return Notification[T, K, KP]{}, fmt.Errorf("%s: %w", "failed to unmarshal notification key", err)
}
var msg T
var err error
msg, deleted, err = translate(newKey)
if err != nil {
return Notification[T, K, KP]{}, fmt.Errorf("%s: %w", "failed to translate database notification", err)
}
if !deleted.Ok() {
message = types.Some(msg)
}
}
return Notification[T, K, KP]{Deleted: deleted, Message: message}, nil
}
func waitForNotification(ctx context.Context, conn *pgx.Conn) (event, error) {
notification, err := conn.WaitForNotification(ctx)
if err != nil {
return event{}, err
}
ev := event{}
dec := json.NewDecoder(strings.NewReader(notification.Payload))
dec.DisallowUnknownFields()
err = dec.Decode(&ev)
if err != nil {
return event{}, err
}
return ev, nil
}