-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron.go
377 lines (306 loc) · 9.63 KB
/
cron.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package main
import (
"bytes"
"context"
"database/sql"
"fmt"
"os"
"sync"
"text/template"
"time"
"github.com/ayinke-llc/hermes"
"github.com/ayinke-llc/malak"
"github.com/ayinke-llc/malak/config"
"github.com/ayinke-llc/malak/internal/datastore/postgres"
"github.com/ayinke-llc/malak/internal/pkg/email"
"github.com/ayinke-llc/malak/server"
"github.com/google/uuid"
"github.com/spf13/cobra"
"github.com/uptrace/bun"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
)
func addCronCommand(c *cobra.Command, cfg *config.Config) {
cmd := &cobra.Command{
Use: "cron",
}
cmd.AddCommand(sendScheduledUpdates(c, cfg))
c.AddCommand(cmd)
}
// TODO(adelowo): test at scale before beta mvp release. Email rate scale and errors syncing
func sendScheduledUpdates(c *cobra.Command, cfg *config.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "updates-send",
Short: `Send scheduled updates`,
RunE: func(cmd *cobra.Command, args []string) error {
var logger *zap.Logger
var err error
switch cfg.Logging.Mode {
case config.LogModeProd:
logger, err = zap.NewProduction()
if err != nil {
fmt.Printf(`{"error":%s}`, err)
os.Exit(1)
}
case config.LogModeDev:
logger, err = zap.NewDevelopment()
if err != nil {
fmt.Printf(`{"error":%s}`, err)
os.Exit(1)
}
}
// ignoring on purpose
h, _ := os.Hostname()
logger = logger.With(zap.String("host", h),
zap.String("app", "malak"),
zap.String("component", "cron.updates-send"))
cleanupOtelResources := server.InitOTELCapabilities(hermes.DeRef(cfg), logger)
defer cleanupOtelResources()
emailClient, err := getEmailProvider(*cfg)
if err != nil {
logger.Fatal("could not set up email client",
zap.Error(err))
}
defer emailClient.Close()
// Doing this here as I do not see another place where we
// need to reuse this bit of code
// if for some reason, we have to send updates from
// another place.
// Just encapsulate this so we don't duplicate code
//
// LOGIC
//
// 1. Fetch pending scheduled updates
// 2. Fetch the recipients of each updates
// 3. Send to each recipient
var tracer = otel.Tracer("malak.cron")
ctx, span := tracer.Start(context.Background(), "updates-send")
defer span.End()
db, err := postgres.New(cfg, logger)
if err != nil {
logger.Fatal("could not set up database connection",
zap.Error(err))
}
defer db.Close()
var scheduledUpdates = make([]*malak.UpdateSchedule, 0)
err = db.NewSelect().Model(&scheduledUpdates).
Limit(30).
Where("status = ?", malak.UpdateSendScheduleScheduled).
Scan(ctx)
if err != nil {
span.RecordError(err)
logger.Error("could not fetch pending scheduled updates from db to process",
zap.Error(err))
return err
}
if len(scheduledUpdates) == 0 {
logger.Info("no scheduled updates to process")
os.Exit(0)
}
span.SetAttributes(
attribute.Int("number_of_scheduled_updates", len(scheduledUpdates)))
updateRepo := postgres.NewUpdatesRepository(db)
g, ctx := errgroup.WithContext(ctx)
var wg sync.WaitGroup
for _, scheduledUpdate := range scheduledUpdates {
g.Go(func() error {
ctx, span := tracer.Start(ctx, "sending")
defer span.End()
scheduledUpdate.Status = malak.UpdateSendScheduleProcessing
_, err = db.NewUpdate().Model(scheduledUpdate).Where("id = ? ", scheduledUpdate.ID).
Exec(ctx)
if err != nil {
span.RecordError(err)
logger.Error("could not update schedule status to processing",
zap.Error(err))
return err
}
update := &malak.Update{}
err := db.NewSelect().Model(update).
Where("id = ?", scheduledUpdate.UpdateID).
Scan(ctx)
if err != nil {
span.RecordError(err)
logger.Error("could not fetch update from database",
zap.Error(err))
return err
}
templatedFile, err := template.New("template").
Parse(email.UpdateHTMLEmailTemplate)
if err != nil {
span.RecordError(err)
logger.Error("could not create html template",
zap.Error(err))
return err
}
var b = new(bytes.Buffer)
err = templatedFile.Execute(b, map[string]string{
"Content": update.Content.HTML(),
})
if err != nil {
span.RecordError(err)
logger.Error("could not parse html template",
zap.Error(err))
return err
}
logger = logger.With(zap.String("update_id", update.ID.String()))
schedule, err := updateRepo.GetSchedule(ctx, scheduledUpdate.ID)
if err != nil {
span.RecordError(err)
logger.Error("could not fetch update schedule from database",
zap.Error(err))
return err
}
type contacts struct {
malak.UpdateRecipient
Contact *malak.Contact `json:"contact" bun:"rel:has-one,join:contact_id=id"`
bun.BaseModel `bun:"table:update_recipients"`
}
var contactsFromDB = make([]contacts, 0)
err = db.NewSelect().Model(&contactsFromDB).
Limit(10).
Where("update_id = ?", schedule.UpdateID).
Where("status = ?", malak.RecipientStatusPending).
Relation("Contact").
Scan(ctx)
if err != nil {
span.RecordError(err)
logger.Error("could not fetch recipients for update",
zap.Error(err))
return err
}
if len(contactsFromDB) == 0 {
// mark as sent
logger.Info("no recipients for this update")
scheduledUpdate.Status = malak.UpdateSendScheduleSent
_, err = db.NewUpdate().Model(scheduledUpdate).Where("id = ? ", scheduledUpdate.ID).
Exec(ctx)
if err != nil {
span.RecordError(err)
logger.Error("could not update schedule status",
zap.Error(err))
return err
}
return nil
}
title := fmt.Sprintf("[TEST] %s", update.Title)
if scheduledUpdate.UpdateType == malak.UpdateTypeLive {
title = update.Title
}
// TODO(adelowo): future version should include batching
// We cannot batch right now because Resend provider does not send tags when you send a batched email
// Without tags, there is no way to properly update the right recipient stat
//
// API calls will most likely be throttled and we will have to deal with lot of failures
// But we will figure that out
//
// This cron needs a lot of clean up anyways especially with synchronizations amongst others
wg.Add(len(contactsFromDB))
for _, contact := range contactsFromDB {
go func() {
defer wg.Done()
time.Sleep(time.Second)
opts := email.SendOptions{
HTML: b.String(),
Sender: cfg.Email.Sender,
Recipient: contact.Contact.Email,
Subject: title,
DKIM: struct {
Sign bool
PrivateKey []byte
}{
Sign: false,
PrivateKey: []byte(""),
},
}
var status = malak.RecipientStatusSent
emailID, err := emailClient.Send(ctx, opts)
if err != nil {
logger.Error("could not send email", zap.Error(err),
zap.String("recipient_reference", contact.Reference.String()))
status = malak.RecipientStatusFailed
return
}
err = db.RunInTx(ctx, &sql.TxOptions{}, func(ctx context.Context, tx bun.Tx) error {
_, err := tx.NewUpdate().
Model(&malak.UpdateRecipient{}).
Where("reference = ?", contact.Reference.String()).
Set("status = ?", status).
Exec(ctx)
if err != nil {
return err
}
emailLog := &malak.UpdateRecipientLog{
ProviderID: emailID,
RecipientID: contact.ID,
Reference: malak.NewReferenceGenerator().Generate(malak.EntityTypeRecipientLog),
Provider: emailClient.Name(),
ID: uuid.New(),
}
_, err = tx.NewInsert().
Model(emailLog).
Exec(ctx)
if err != nil {
return err
}
stats := &malak.UpdateRecipientStat{
Reference: malak.NewReferenceGenerator().Generate(malak.EntityTypeRecipientStat),
RecipientID: contact.ID,
HasReaction: false,
IsDelivered: status == malak.RecipientStatusSent,
}
_, err = tx.NewInsert().
Model(stats).
Exec(ctx)
return err
})
if err != nil {
logger.Error("could not update database", zap.Error(err),
zap.String("email_id", emailID),
zap.String("recipient_reference", contact.Reference.String()))
status = malak.RecipientStatusFailed
return
}
}()
}
wg.Wait()
scheduledUpdate.Status = malak.UpdateSendScheduleSent
err = db.RunInTx(ctx, &sql.TxOptions{}, func(ctx context.Context, tx bun.Tx) error {
_, err = tx.NewUpdate().Model(scheduledUpdate).
Where("id = ? ", scheduledUpdate.ID).
Exec(ctx)
if err != nil {
return err
}
if update.Status == malak.UpdateStatusSent {
return nil
}
update.Status = malak.UpdateStatusSent
update.SentBy = schedule.ScheduledBy
_, err = tx.NewUpdate().
Model(update).
Where("id = ?", update.ID).
Exec(ctx)
return err
})
if err != nil {
span.RecordError(err)
logger.Error("could not update schedule status",
zap.Error(err))
return err
}
return nil
})
}
if err := g.Wait(); err != nil {
logger.Error("could not send updates", zap.Error(err))
return err
}
return nil
},
}
cmd.Flags().Int64("n", 10, "number of scheduled updates to process")
return cmd
}