Use post commit hook for post-create plugin hooks (#2920)

This commit is contained in:
WithoutPants
2022-09-19 14:53:06 +10:00
committed by GitHub
parent 0359ce2ed8
commit 2564351265
12 changed files with 109 additions and 88 deletions

View File

@@ -1,50 +0,0 @@
package sqlite
import (
"context"
"github.com/stashapp/stash/pkg/txn"
)
type hookManager struct {
postCommitHooks []txn.TxnFunc
postRollbackHooks []txn.TxnFunc
}
func (m *hookManager) register(ctx context.Context) context.Context {
return context.WithValue(ctx, hookManagerKey, m)
}
func (db *Database) hookManager(ctx context.Context) *hookManager {
m, ok := ctx.Value(hookManagerKey).(*hookManager)
if !ok {
return nil
}
return m
}
func (db *Database) executePostCommitHooks(ctx context.Context) {
m := db.hookManager(ctx)
for _, h := range m.postCommitHooks {
// ignore errors
_ = h(ctx)
}
}
func (db *Database) executePostRollbackHooks(ctx context.Context) {
m := db.hookManager(ctx)
for _, h := range m.postRollbackHooks {
// ignore errors
_ = h(ctx)
}
}
func (db *Database) AddPostCommitHook(ctx context.Context, hook txn.TxnFunc) {
m := db.hookManager(ctx)
m.postCommitHooks = append(m.postCommitHooks, hook)
}
func (db *Database) AddPostRollbackHook(ctx context.Context, hook txn.TxnFunc) {
m := db.hookManager(ctx)
m.postRollbackHooks = append(m.postRollbackHooks, hook)
}

View File

@@ -17,7 +17,6 @@ type key int
const (
txnKey key = iota + 1
dbKey
hookManagerKey
)
func (db *Database) WithDatabase(ctx context.Context) (context.Context, error) {
@@ -42,9 +41,6 @@ func (db *Database) Begin(ctx context.Context) (context.Context, error) {
return nil, fmt.Errorf("beginning transaction: %w", err)
}
hookMgr := &hookManager{}
ctx = hookMgr.register(ctx)
return context.WithValue(ctx, txnKey, tx), nil
}
@@ -58,9 +54,6 @@ func (db *Database) Commit(ctx context.Context) error {
return err
}
// execute post-commit hooks
db.executePostCommitHooks(ctx)
return nil
}
@@ -74,9 +67,6 @@ func (db *Database) Rollback(ctx context.Context) error {
return err
}
// execute post-rollback hooks
db.executePostRollbackHooks(ctx)
return nil
}