/
worker.go
111 lines (101 loc) · 2.37 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
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
package event
import (
"context"
"errors"
"sync"
"time"
"github.com/artifacthub/hub/internal/hub"
"github.com/artifacthub/hub/internal/util"
"github.com/jackc/pgx/v4"
"github.com/rs/zerolog/log"
)
const (
pauseOnEmptyQueue = 30 * time.Second
pauseOnError = 10 * time.Second
)
// Worker is in charge of handling events that happen in the Hub.
type Worker struct {
svc *Services
}
// NewWorker creates a new Worker instance.
func NewWorker(svc *Services) *Worker {
return &Worker{
svc: svc,
}
}
// Run is the main loop of the worker. It calls processEvent periodically until
// it's asked to stop via the context provided.
func (w *Worker) Run(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
for {
err := w.processEvent(ctx)
switch {
case err == nil:
select {
case <-ctx.Done():
return
default:
}
case errors.Is(err, pgx.ErrNoRows):
select {
case <-time.After(pauseOnEmptyQueue):
case <-ctx.Done():
return
}
default:
select {
case <-time.After(pauseOnError):
case <-ctx.Done():
return
}
}
}
}
// processEvent gets a pending event from the database and processes it.
func (w *Worker) processEvent(ctx context.Context) error {
return util.DBTransact(ctx, w.svc.DB, func(tx pgx.Tx) error {
// Get pending event to process
e, err := w.svc.EventManager.GetPending(ctx, tx)
if err != nil {
if !errors.Is(err, pgx.ErrNoRows) {
log.Error().Err(err).Msg("error getting pending event")
}
return err
}
// Register event notifications
// Email notifications
users, err := w.svc.SubscriptionManager.GetSubscriptors(ctx, e)
if err != nil {
log.Error().Err(err).Msg("error getting subscriptors")
return err
}
for _, u := range users {
n := &hub.Notification{
Event: e,
User: u,
}
if err := w.svc.NotificationManager.Add(ctx, tx, n); err != nil {
log.Error().Err(err).Msg("error adding notification")
return err
}
}
// Webhook notifications
webhooks, err := w.svc.WebhookManager.GetSubscribedTo(ctx, e)
if err != nil {
log.Error().Err(err).Msg("error getting webhooks")
return err
}
for _, wh := range webhooks {
n := &hub.Notification{
Event: e,
Webhook: wh,
}
err := w.svc.NotificationManager.Add(ctx, tx, n)
if err != nil {
log.Error().Err(err).Msg("error adding notification")
return err
}
}
return nil
})
}