Files
hatchet/pkg/repository/callbacks.go
T
matt 058968c06b Refactor: Attempt II at removing pgtype.UUID everywhere + convert string UUIDs into uuid.UUID (#2894)
* fix: add type override in sqlc.yaml

* chore: gen sqlc

* chore: big find and replace

* chore: more

* fix: clean up bunch of outdated `.Valid` refs

* refactor: remove `sqlchelpers.uuidFromStr()` in favor of `uuid.MustParse()`

* refactor: remove uuidToStr

* fix: lint

* fix: use pointers for null uuids

* chore: clean up more null pointers

* chore: clean up a bunch more

* fix: couple more

* fix: some types on the api

* fix: incorrectly non-null param

* fix: more nullable params

* fix: more refs

* refactor: start replacing tenant id strings with uuids

* refactor: more tenant id uuid casting

* refactor: fix a bunch more

* refactor: more

* refactor: more

* refactor: is that all of them?!

* fix: panic

* fix: rm scans

* fix: unwind some broken things

* chore: tests

* fix: rebase issues

* fix: more tests

* fix: nil checks

* Refactor: Make all UUIDs into `uuid.UUID` (#2897)

* refactor: remove a bunch more string uuids

* refactor: pointers and lists

* refactor: fix all the refs

* refactor: fix a few more

* fix: config loader

* fix: revert some changes

* fix: tests

* fix: test

* chore: proto

* fix: durable listener

* fix: some more string types

* fix: python health worker sleep

* fix: remove a bunch of `MustParse`s from the various gRPC servers

* fix: rm more uuid.MustParse calls

* fix: rm mustparse from api

* fix: test

* fix: merge issues

* fix: handle a bunch more uses of `MustParse` everywhere

* fix: nil id for worker label

* fix: more casting in the oss

* fix: more id parsing

* fix: stringify jwt opt

* fix: couple more bugs in untyped calls

* fix: more types

* fix: broken test

* refactor: implement `GetKeyUuid`

* chore: regen sqlc

* chore: replace pgtype.UUID again

* fix: bunch more type errors

* fix: panic
2026-02-03 11:02:59 -05:00

100 lines
2.0 KiB
Go

package repository
import (
"github.com/google/uuid"
"github.com/rs/zerolog"
)
type TenantScopedCallback[T any] func(uuid.UUID, T) error
func (c TenantScopedCallback[T]) Do(l *zerolog.Logger, tenantId uuid.UUID, v T) {
// wrap in panic recover to avoid panics in the callback
defer func() {
if r := recover(); r != nil {
if l != nil {
l.Error().Interface("panic", r).Msg("panic in callback")
}
}
}()
go func() {
err := c(tenantId, v)
if err != nil {
l.Error().Err(err).Msg("callback failed")
}
}()
}
type UnscopedCallback[T any] func(T) error
func (c UnscopedCallback[T]) Do(l *zerolog.Logger, v T) {
go func() {
defer func() {
if r := recover(); r != nil {
if l != nil {
l.Error().Interface("panic", r).Msg("panic in callback")
}
}
}()
err := c(v)
if err != nil {
l.Error().Err(err).Msg("callback failed")
}
}()
}
type CallbackOptFunc[T any] func(*TenantCallbackOpts[T])
func WithPreCommitCallback[T any](cb TenantScopedCallback[T]) CallbackOptFunc[T] {
return func(opts *TenantCallbackOpts[T]) {
opts.cbs = append(opts.cbs, cb)
}
}
func WithPostCommitCallback[T any](cb TenantScopedCallback[T]) CallbackOptFunc[T] {
return func(opts *TenantCallbackOpts[T]) {
opts.cbs = append(opts.cbs, cb)
}
}
func RunPreCommit[T any](l *zerolog.Logger, tenantId uuid.UUID, v T, opts []CallbackOptFunc[T]) {
// initialize the opts
o := &TenantCallbackOpts[T]{
cbs: make([]TenantScopedCallback[T], 0),
}
// apply the opts
for _, opt := range opts {
opt(o)
}
o.Run(l, tenantId, v)
}
func RunPostCommit[T any](l *zerolog.Logger, tenantId uuid.UUID, v T, opts []CallbackOptFunc[T]) {
// initialize the opts
o := &TenantCallbackOpts[T]{
cbs: make([]TenantScopedCallback[T], 0),
}
// apply the opts
for _, opt := range opts {
opt(o)
}
o.Run(l, tenantId, v)
}
type TenantCallbackOpts[T any] struct {
cbs []TenantScopedCallback[T]
}
func (o *TenantCallbackOpts[T]) Run(l *zerolog.Logger, tenantId uuid.UUID, v T) {
for _, cb := range o.cbs {
cb.Do(l, tenantId, v)
}
}