mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 20:34:37 +03:00
Use post commit hook for post-create plugin hooks (#2920)
This commit is contained in:
@@ -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)
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user