代码拉取完成,页面将自动刷新
package kk_executor
import (
"errors"
"gitee.com/cruvie/kk_go_kit/kk_log"
"gitee.com/cruvie/kk_go_kit/kk_minio"
"gitee.com/cruvie/kk_go_kit/kk_stage"
"gorm.io/gorm"
"log/slog"
)
// Executor 失败时执行回滚程序
func Executor(stage *kk_stage.Stage, do func() error, failBack func() error) error {
newLog := kk_log.NewLog(&kk_log.LogOption{TraceId: stage.TraceId})
if do == nil {
panic("Executor do is nil")
}
doErr := do()
doErrLog := newLog.Copy().Error(doErr)
if doErr != nil {
slog.Info("Executor begin to roll back", doErrLog.Args()...)
if failBack != nil {
failBackErr := failBack()
if failBackErr != nil {
failBackErrLog := newLog.Copy().Error(failBackErr)
slog.Error("Executor fail to roll back", failBackErrLog.Args()...)
return failBackErrLog.ToError()
}
}
slog.Info("Executor success to roll back", newLog.Copy().Args()...)
}
return doErrLog.ToError()
}
// SyncExecutor sync executor
type SyncExecutor struct {
Stage *kk_stage.Stage
DB *gorm.DB
DBDo func(tx *gorm.DB) error
MinIOObjects []kk_minio.OPObject
}
func (e *SyncExecutor) check() {
if e.Stage == nil {
panic("Executor stage is nil")
}
if e.DBDo != nil && e.DB == nil {
panic("Executor DB is nil")
}
}
func (e *SyncExecutor) Execute() error {
e.check()
transaction := func() *gorm.DB {
if e.DBDo != nil {
return e.DB.Begin()
}
return nil
}()
err := Executor(e.Stage, func() error {
{
if e.DBDo != nil {
err := e.DBDo(transaction)
if err != nil {
return err
}
}
}
{
//upload opObjects
if len(e.MinIOObjects) > 0 {
err := kk_minio.BatchOperator(e.MinIOObjects, kk_minio.UploadFileStream)
if err != nil {
return err
}
}
}
{
//commit transaction finally
if transaction != nil {
if err := transaction.Commit().Error; err != nil {
return err
}
}
}
return nil
}, func() error {
var errs error
{
if transaction != nil {
if err := transaction.Rollback().Error; err != nil {
errs = errors.Join(errs, err)
}
}
}
{
//delete opObjects
if len(e.MinIOObjects) > 0 {
err := kk_minio.BatchOperator(e.MinIOObjects, kk_minio.RemoveObject)
errs = errors.Join(errs, err)
err = kk_minio.BatchOperator(e.MinIOObjects, kk_minio.RemoveIncompleteObject)
errs = errors.Join(errs, err)
}
}
return errs
})
return err
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。