-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatcher.go
101 lines (84 loc) · 3 KB
/
dispatcher.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
package dispatcher
import (
"fmt"
"log/slog"
"sync"
"time"
"github.com/samber/lo"
"github.com/yash492/statusy/cmd/dispatcher/worker"
"github.com/yash492/statusy/pkg/domain"
"github.com/yash492/statusy/pkg/queue"
"github.com/yash492/statusy/pkg/schema"
"github.com/yash492/statusy/pkg/types"
)
func New(queue *queue.Queue, wg *sync.WaitGroup) {
workerMap := worker.New()
for {
incident, err := queue.Consume()
if err != nil {
continue
}
dispatchIncident(incident, workerMap)
}
}
func dispatchIncident(incident queue.IncidentPayload, workerMap worker.WorkerMap) {
workerEvent, err := fetchSubscriptionContext(incident.IncidentUpdate.ID, incident.State)
if err != nil {
slog.Error(err.Error())
return
}
wg := &sync.WaitGroup{}
for workerName, eventMap := range workerMap {
slog.Info("dispatching event", "worker_name", workerName, "event_type", incident.State, "incident_update_id", incident.IncidentUpdate.ID, "incident_id", workerEvent.IncidentID)
worker, ok := eventMap[workerEvent.EventType]
if !ok {
return
}
wg.Add(1)
go func(w types.WorkerEvent, wg *sync.WaitGroup) {
defer wg.Done()
err := worker.Do(w)
if err != nil {
slog.Error(err.Error())
}
// Limiting the request speed as not to cross rate limits
// and giving ample breathing room for services to process the request
time.Sleep(1 * time.Second)
}(workerEvent, wg)
}
wg.Wait()
}
func fetchSubscriptionContext(incidentID uint, eventType string) (types.WorkerEvent, error) {
subscriptions, err := domain.Subscription.GetForIncidentUpdates(incidentID)
if err != nil {
slog.Error(err.Error())
return types.WorkerEvent{}, err
}
if len(subscriptions) < 1 {
slog.Error("no subscriptions found")
return types.WorkerEvent{}, fmt.Errorf("no subscriptions were found for %v", incidentID)
}
components := lo.Map(subscriptions, func(subscription schema.SubscriptionForIncidentUpdate, _ int) types.ComponentsWithNameAndID {
return types.ComponentsWithNameAndID{
Name: subscription.ComponentName.String,
ID: (uint(subscription.ComponentID.Int64)),
}
})
workerEvent := types.WorkerEvent{
Components: components,
ServiceID: subscriptions[0].ServiceID,
ServiceName: subscriptions[0].ServiceName,
IncidentID: subscriptions[0].IncidentID,
IncidentName: subscriptions[0].IncidentName,
IncidentLink: subscriptions[0].IncidentLink,
IncidentImpact: subscriptions[0].IncidentImpact.String,
IncidentUpdate: subscriptions[0].IncidentUpdate,
IncidentUpdateProviderStatus: subscriptions[0].IncidentUpdateProviderStatus,
IncidentUpdateStatus: subscriptions[0].IncidentUpdateStatus,
IsAllComponents: subscriptions[0].IsAllComponents,
EventType: eventType,
IncidentUpdateStatusTime: subscriptions[0].IncidentUpdateStatusTime,
IncidentUpdateID: subscriptions[0].IncidentUpdateID,
}
return workerEvent, nil
}