Skip to content

Commit

Permalink
feat: notification delay & event_queue delay
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Sep 17, 2024
1 parent ca2b9a8 commit d04d339
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 6 deletions.
1 change: 0 additions & 1 deletion connection/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions models/event_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Event struct {
Name string `json:"name"`
CreatedAt time.Time `json:"created_at"`
Properties types.JSONStringMap `json:"properties"`
Delay *time.Duration `json:"delay,omitempty"`
Error *string `json:"error,omitempty"`
Attempts int `json:"attempts"`
LastAttempt *time.Time `json:"last_attempt"`
Expand Down
1 change: 1 addition & 0 deletions models/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Notification struct {
Namespace string `json:"namespace,omitempty"`
Events pq.StringArray `json:"events" gorm:"type:[]text"`
Title string `json:"title,omitempty"`
WaitFor *time.Duration `json:"wait_for,omitempty"`
Template string `json:"template,omitempty"`
Filter string `json:"filter,omitempty"`
PersonID *uuid.UUID `json:"person_id,omitempty"`
Expand Down
1 change: 1 addition & 0 deletions postq/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func fetchEvents(ctx context.Context, tx *gorm.DB, watchEvents []string, batchSi
WHERE id IN (
SELECT id FROM event_queue
WHERE
(delay IS NULL OR created_at + (delay * INTERVAL '1 second' / 1000000000) <= NOW()) AND
attempts <= @MaxAttempts AND
name = ANY(@Events) AND
(last_attempt IS NULL OR last_attempt <= NOW() - INTERVAL '1 SECOND' * @BaseDelay * POWER(attempts, @Exponent))
Expand Down
9 changes: 7 additions & 2 deletions schema/notifications.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ table "notifications" {
default = sql("generate_ulid()")
}
column "name" {
null = false
type = text
null = false
type = text
default = sql("generate_ulid()") # temporary default value to make the migration possible. we can remove this later.
}
column "namespace" {
Expand Down Expand Up @@ -54,6 +54,11 @@ table "notifications" {
null = true
type = text
}
column "wait_for" {
null = true
type = bigint
comment = "duration in nanoseconds"
}
column "group_by" {
null = true
type = sql("text[]")
Expand Down
5 changes: 5 additions & 0 deletions schema/system.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ table "event_queue" {
null = true
type = text
}
column "delay" {
null = true
type = bigint
comment = "wait for this duration (nanoseconds) before consuming"
}
column "created_at" {
null = false
type = timestamptz
Expand Down
1 change: 0 additions & 1 deletion shell/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions types/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d04d339

Please sign in to comment.