-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
259 changed files
with
150,556 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package eventing | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/jackc/pgx" | ||
|
||
"github.com/TheLab-ms/profile/internal/conf" | ||
) | ||
|
||
const migration = ` | ||
CREATE TABLE IF NOT EXISTS profile_events ( | ||
id integer primary key, | ||
time timestamp not null, | ||
email text not null, | ||
reason text not null, | ||
message text not null | ||
); | ||
CREATE INDEX IF NOT EXISTS idx_profile_events_time ON profile_events (time); | ||
` | ||
|
||
type Sink struct { | ||
buffer chan *event | ||
} | ||
|
||
func NewSink(env *conf.Env) (*Sink, error) { | ||
s := &Sink{} | ||
if env.EventPsqlAddr == "" { | ||
return s, nil | ||
} | ||
|
||
db, err := pgx.Connect(pgx.ConnConfig{ | ||
Host: env.EventPsqlAddr, | ||
User: env.EventPsqlUsername, | ||
Password: env.EventPsqlPassword, | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("constructing db client: %w", err) | ||
} | ||
|
||
_, err = db.Exec(migration) | ||
if err != nil { | ||
return nil, fmt.Errorf("db migration: %w", err) | ||
} | ||
|
||
// Flush messages out to postgres | ||
s.buffer = make(chan *event, env.EventBufferLength) | ||
go func() { | ||
defer db.Close() | ||
|
||
for event := range s.buffer { | ||
_, err := db.Exec("INSERT INTO profile_events (time, email, reason, message) VALUES ($1, $2, $3, $4)", event.Timestamp, event.Email, event.Reason, event.Message) | ||
if err != nil { | ||
log.Printf("error while flushing event to postgres: %s", err) // it would be a good idea to retry here | ||
} | ||
|
||
// don't send messages too often | ||
// batching would be nice, this is easier to implement | ||
time.Sleep(time.Second) | ||
} | ||
}() | ||
|
||
return s, nil | ||
} | ||
|
||
func (s *Sink) Publish(email, reason, templ string, args ...any) { | ||
if s.buffer == nil { | ||
return | ||
} | ||
s.buffer <- &event{ | ||
Timestamp: time.Now(), | ||
Email: email, | ||
Reason: reason, | ||
Message: fmt.Sprintf(templ, args...), | ||
} | ||
} | ||
|
||
type event struct { | ||
Timestamp time.Time | ||
Email string | ||
Reason string | ||
Message string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.