-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjob_create_matches.go
More file actions
161 lines (127 loc) · 4.68 KB
/
job_create_matches.go
File metadata and controls
161 lines (127 loc) · 4.68 KB
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
package bot
import (
"context"
"time"
"github.com/hashicorp/go-hclog"
"github.com/pkg/errors"
"github.com/slack-go/slack"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"gorm.io/gorm"
"github.com/chat-roulettte/chat-roulette/internal/database/models"
"github.com/chat-roulettte/chat-roulette/internal/o11y/attributes"
)
// chatRoulettePair is a pair of participants for chat-roulette
type chatRoulettePair struct {
Participant string
Partner string
}
// CreateMatchesParams are the parameters for the CREATE_MATCHES job.
type CreateMatchesParams struct {
ChannelID string `json:"channel_id"`
RoundID int32 `json:"round_id"`
}
// CreateMatches creates matches between active participants for a round of chat-roulette.
func CreateMatches(ctx context.Context, db *gorm.DB, client *slack.Client, p *CreateMatchesParams) error {
logger := hclog.FromContext(ctx).With(
attributes.SlackChannelID, p.ChannelID,
attributes.RoundID, p.RoundID,
)
// Wait for member jobs to completed before retrieving participants
logger.Info("waiting up to 30 seconds for in-flight member jobs to complete")
if err := waitOnMemberJobs(ctx, db, p.ChannelID); err != nil {
return err
}
// Retrieve matches for this round of chat-roulette
dbCtx, cancel := context.WithTimeout(ctx, 300*time.Millisecond)
defer cancel()
logger.Info("retrieving matches for this round of chat-roulette")
var matches []chatRoulettePair
result := db.WithContext(dbCtx).
Raw("SELECT * FROM GetRandomMatchesV2(?)", p.ChannelID).
Scan(&matches)
if result.Error != nil {
message := "failed to retrieve matches for chat-roulette"
logger.Error(message, "error", result.Error)
return errors.Wrap(result.Error, message)
}
logger.Debug("retrieved matches for chat-roulette", "matches", result.RowsAffected)
var unpaired int
for _, pair := range matches {
// Queue a NOTIFY_MEMBER job for any participants who did not get matched
if pair.Partner == "" {
params := &NotifyMemberParams{
ChannelID: p.ChannelID,
UserID: pair.Participant,
}
dbCtx, cancel = context.WithTimeout(ctx, 500*time.Millisecond)
defer cancel()
if err := QueueNotifyMemberJob(dbCtx, db, params); err != nil {
message := "failed to add CREATE_PAIR job to the queue"
logger.Error(message, "error", result.Error)
return errors.Wrap(result.Error, message)
}
logger.Info("queued NOTIFY_MEMBER job for this unmatched participant")
unpaired++
continue
}
// Create a database record in the matches table for each pair and queue a CREATE_PAIR job
newMatch := &models.Match{
RoundID: p.RoundID,
}
dbCtx, cancel := context.WithTimeout(ctx, 300*time.Millisecond)
defer cancel()
if err := db.WithContext(dbCtx).Create(newMatch).Error; err != nil {
logger.Error("failed to create new match record in the database", "error", err)
return err
}
logger.Info("added new match to the database", attributes.MatchID, newMatch.ID)
params := &CreatePairParams{
MatchID: newMatch.ID,
ChannelID: p.ChannelID,
Participant: pair.Participant,
Partner: pair.Partner,
}
dbCtx, cancel = context.WithTimeout(ctx, 500*time.Millisecond)
defer cancel()
if err := QueueCreatePairJob(dbCtx, db, params); err != nil {
message := "failed to add CREATE_PAIR job to the queue"
logger.Error(message, "error", err)
return errors.Wrap(err, message)
}
logger.Info("queued CREATE_PAIR job for this match", attributes.MatchID, newMatch.ID)
}
pairsCount := len(matches) - unpaired
participantsCount := (pairsCount*2 + unpaired)
logger.Info("paired active participants for chat-roulette", "participants", participantsCount, "pairs", pairsCount, "unpaired", unpaired)
trace.SpanFromContext(ctx).SetAttributes(
attribute.Int("participants", participantsCount),
attribute.Int("pairs", pairsCount),
attribute.Int("unpaired", unpaired),
)
// Queue a REPORT_MATCHES job
params := &ReportMatchesParams{
ChannelID: p.ChannelID,
RoundID: p.RoundID,
Participants: participantsCount,
Pairs: pairsCount,
Unpaired: unpaired,
}
dbCtx, cancel = context.WithTimeout(ctx, 300*time.Millisecond)
defer cancel()
if err := QueueReportMatchesJob(dbCtx, db, params); err != nil {
message := "failed to add REPORT_MATCHES job to the queue"
logger.Error(message, "error", err)
return errors.Wrap(err, message)
}
return nil
}
// QueueCreateMatchesJob adds a new CREATE_MATCHES job to the queue.
func QueueCreateMatchesJob(ctx context.Context, db *gorm.DB, p *CreateMatchesParams) error {
job := models.GenericJob[*CreateMatchesParams]{
JobType: models.JobTypeCreateMatches,
Priority: models.JobPriorityLow,
Params: p,
}
return QueueJob(ctx, db, job)
}