Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit272c338

Browse files
fix: throttle prebuild notifications
1 parentd61353f commit272c338

File tree

12 files changed

+208
-8
lines changed

12 files changed

+208
-8
lines changed

‎coderd/database/dbauthz/dbauthz.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2636,6 +2636,10 @@ func (q *querier) GetTemplateParameterInsights(ctx context.Context, arg database
26362636
returnq.db.GetTemplateParameterInsights(ctx,arg)
26372637
}
26382638

2639+
func (q*querier)GetTemplatePrebuildNotificationCooldown(ctx context.Context,arg database.GetTemplatePrebuildNotificationCooldownParams) (database.TemplatePrebuildNotificationCooldown,error) {
2640+
panic("not implemented")
2641+
}
2642+
26392643
func (q*querier)GetTemplatePresetsWithPrebuilds(ctx context.Context,templateID uuid.NullUUID) ([]database.GetTemplatePresetsWithPrebuildsRow,error) {
26402644
// GetTemplatePresetsWithPrebuilds retrieves template versions with configured presets and prebuilds.
26412645
// Presets and prebuilds are part of the template, so if you can access templates - you can access them as well.
@@ -5123,6 +5127,10 @@ func (q *querier) UpsertTelemetryItem(ctx context.Context, arg database.UpsertTe
51235127
returnq.db.UpsertTelemetryItem(ctx,arg)
51245128
}
51255129

5130+
func (q*querier)UpsertTemplatePrebuildNotificationCooldown(ctx context.Context,arg database.UpsertTemplatePrebuildNotificationCooldownParams)error {
5131+
panic("not implemented")
5132+
}
5133+
51265134
func (q*querier)UpsertTemplateUsageStats(ctx context.Context)error {
51275135
iferr:=q.authorizeContext(ctx,policy.ActionUpdate,rbac.ResourceSystem);err!=nil {
51285136
returnerr

‎coderd/database/dbmem/dbmem.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6181,6 +6181,15 @@ func (q *FakeQuerier) GetTemplateParameterInsights(ctx context.Context, arg data
61816181
returnrows,nil
61826182
}
61836183

6184+
func (q*FakeQuerier)GetTemplatePrebuildNotificationCooldown(ctx context.Context,arg database.GetTemplatePrebuildNotificationCooldownParams) (database.TemplatePrebuildNotificationCooldown,error) {
6185+
err:=validateDatabaseType(arg)
6186+
iferr!=nil {
6187+
return database.TemplatePrebuildNotificationCooldown{},err
6188+
}
6189+
6190+
panic("not implemented")
6191+
}
6192+
61846193
func (*FakeQuerier)GetTemplatePresetsWithPrebuilds(_ context.Context,_ uuid.NullUUID) ([]database.GetTemplatePresetsWithPrebuildsRow,error) {
61856194
returnnil,ErrUnimplemented
61866195
}
@@ -12555,6 +12564,15 @@ func (q *FakeQuerier) UpsertTelemetryItem(_ context.Context, arg database.Upsert
1255512564
returnnil
1255612565
}
1255712566

12567+
func (q*FakeQuerier)UpsertTemplatePrebuildNotificationCooldown(ctx context.Context,arg database.UpsertTemplatePrebuildNotificationCooldownParams)error {
12568+
err:=validateDatabaseType(arg)
12569+
iferr!=nil {
12570+
returnerr
12571+
}
12572+
12573+
panic("not implemented")
12574+
}
12575+
1255812576
func (q*FakeQuerier)UpsertTemplateUsageStats(ctx context.Context)error {
1255912577
q.mutex.Lock()
1256012578
deferq.mutex.Unlock()

‎coderd/database/dbmetrics/querymetrics.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/database/dump.sql

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROPTABLE IF EXISTS template_prebuild_notification_cooldowns;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATETABLEtemplate_prebuild_notification_cooldowns (
2+
template_id UUIDNOT NULL,
3+
notification_typeTEXTNOT NULL,
4+
last_notification_sentTIMESTAMPTZNOT NULL,
5+
PRIMARY KEY (template_id, notification_type)
6+
);
7+
8+
COMMENT ON TABLE template_prebuild_notification_cooldowns IS'Tracks when prebuild failure notifications were last sent to prevent notification noise';
9+
COMMENT ON COLUMN template_prebuild_notification_cooldowns.notification_type IS'Type of notification: admin or author';

‎coderd/database/models.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/database/querier.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/database/queries.sql.go

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/database/queries/prebuilds.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,17 @@ INNER JOIN organizations o ON o.id = w.organization_id
188188
WHERE NOTt.deletedANDwpb.build_number=1
189189
GROUP BYt.name,tvp.name,o.name
190190
ORDER BYt.name,tvp.name,o.name;
191+
192+
-- name: GetTemplatePrebuildNotificationCooldown :one
193+
SELECT*FROM template_prebuild_notification_cooldowns
194+
WHERE template_id= $1AND notification_type= $2;
195+
196+
-- name: UpsertTemplatePrebuildNotificationCooldown :exec
197+
INSERT INTO template_prebuild_notification_cooldowns (
198+
template_id,
199+
notification_type,
200+
last_notification_sent
201+
)VALUES (
202+
$1, $2, NOW()
203+
)ON CONFLICT (template_id, notification_type) DOUPDATESET
204+
last_notification_sent= NOW();

‎coderd/database/unique_constraint.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎enterprise/coderd/prebuilds/reconcile.go

Lines changed: 82 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,11 @@ func (c *StoreReconciler) ReconcilePreset(ctx context.Context, ps prebuilds.Pres
470470
returnmultiErr.ErrorOrNil()
471471
}
472472

473+
const (
474+
NotificationTypeAdmin="admin"
475+
NotificationTypeAuthor="author"
476+
)
477+
473478
func (c*StoreReconciler)notifyPrebuildFailureLimitReached(ctx context.Context,ps prebuilds.PresetSnapshot)error {
474479
// nolint:gocritic // Necessary to query all the required data.
475480
ctx=dbauthz.AsSystemRestricted(ctx)
@@ -480,6 +485,35 @@ func (c *StoreReconciler) notifyPrebuildFailureLimitReached(ctx context.Context,
480485
returnnil
481486
}
482487

488+
// Send notification to template admins (7-day cooldown)
489+
err:=c.notifyTemplateAdmins(ctx,ps)
490+
iferr!=nil {
491+
returnxerrors.Errorf("notify template admins: %w",err)
492+
}
493+
494+
// Send notification to template version author (1-day cooldown)
495+
//err = c.notifyTemplateAuthor(ctx, ps)
496+
//if err != nil {
497+
//return xerrors.Errorf("notify template author: %w", err)
498+
//}
499+
500+
returnnil
501+
}
502+
503+
func (c*StoreReconciler)notifyTemplateAdmins(ctx context.Context,ps prebuilds.PresetSnapshot)error {
504+
// Check 7-day cooldown for admin notifications
505+
cooldown,err:=c.store.GetTemplatePrebuildNotificationCooldown(ctx, database.GetTemplatePrebuildNotificationCooldownParams{
506+
TemplateID:ps.Preset.TemplateID,
507+
NotificationType:NotificationTypeAdmin,
508+
})
509+
iferr==nil {
510+
// Cooldown exists, check if 7 days have passed
511+
iftime.Since(cooldown.LastNotificationSent)<7*24*time.Hour {
512+
returnnil// Still in cooldown
513+
}
514+
}
515+
516+
// Send notifications to template admins
483517
templateAdmins,err:=c.store.GetUsers(ctx, database.GetUsersParams{
484518
RbacRole: []string{codersdk.RoleTemplateAdmin},
485519
})
@@ -497,20 +531,60 @@ func (c *StoreReconciler) notifyPrebuildFailureLimitReached(ctx context.Context,
497531
},
498532
map[string]any{},
499533
"prebuilds_reconciler",
500-
// Associate this notification with all the related entities.
501534
ps.Preset.TemplateID,ps.Preset.TemplateVersionID,ps.Preset.ID,ps.Preset.OrganizationID,
502535
);err!=nil {
503-
c.logger.Error(ctx,
504-
"failed to send notification",
505-
slog.Error(err),
506-
slog.F("template_admin_id",templateAdmin.ID.String()),
507-
)
508-
536+
c.logger.Error(ctx,"failed to send notification",slog.Error(err),slog.F("template_admin_id",templateAdmin.ID.String()))
509537
continue
510538
}
511539
}
512540

513-
returnnil
541+
// Update cooldown timestamp
542+
returnc.store.UpsertTemplatePrebuildNotificationCooldown(ctx, database.UpsertTemplatePrebuildNotificationCooldownParams{
543+
TemplateID:ps.Preset.TemplateID,
544+
NotificationType:NotificationTypeAdmin,
545+
})
546+
}
547+
548+
func (c*StoreReconciler)notifyTemplateAuthor(ctx context.Context,ps prebuilds.PresetSnapshot)error {
549+
// Get template version to find the author
550+
templateVersion,err:=c.store.GetTemplateVersionByID(ctx,ps.Preset.TemplateVersionID)
551+
iferr!=nil {
552+
returnxerrors.Errorf("fetch template version: %w",err)
553+
}
554+
555+
// Check 1-day cooldown for author notifications
556+
cooldown,err:=c.store.GetTemplatePrebuildNotificationCooldown(ctx, database.GetTemplatePrebuildNotificationCooldownParams{
557+
TemplateID:ps.Preset.TemplateID,
558+
NotificationType:NotificationTypeAuthor,
559+
})
560+
iferr==nil {
561+
// Cooldown exists, check if 1 day has passed
562+
iftime.Since(cooldown.LastNotificationSent)<24*time.Hour {
563+
returnnil// Still in cooldown
564+
}
565+
}
566+
567+
// Send notification to template version author
568+
if_,err:=c.notifEnq.EnqueueWithData(ctx,templateVersion.CreatedBy,notifications.PrebuildFailureLimitReached,
569+
map[string]string{
570+
"org":ps.Preset.OrganizationName,
571+
"template":ps.Preset.TemplateName,
572+
"template_version":ps.Preset.TemplateVersionName,
573+
"preset":ps.Preset.Name,
574+
},
575+
map[string]any{},
576+
"prebuilds_reconciler",
577+
ps.Preset.TemplateID,ps.Preset.TemplateVersionID,ps.Preset.ID,ps.Preset.OrganizationID,
578+
);err!=nil {
579+
c.logger.Error(ctx,"failed to send notification to template author",slog.Error(err),slog.F("author_id",templateVersion.CreatedBy.String()))
580+
returnxerrors.Errorf("send notification to template author: %w",err)
581+
}
582+
583+
// Update cooldown timestamp
584+
returnc.store.UpsertTemplatePrebuildNotificationCooldown(ctx, database.UpsertTemplatePrebuildNotificationCooldownParams{
585+
TemplateID:ps.Preset.TemplateID,
586+
NotificationType:NotificationTypeAuthor,
587+
})
514588
}
515589

516590
func (c*StoreReconciler)CalculateActions(ctx context.Context,snapshot prebuilds.PresetSnapshot) ([]*prebuilds.ReconciliationActions,error) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp