From d011f1f38b0f0be1db1a3ea270689be951accdac Mon Sep 17 00:00:00 2001 From: Alvin Rizki <4lvin.rizki@gmail.com> Date: Sun, 15 Sep 2019 14:07:38 +0700 Subject: [PATCH 1/2] fix issue action executed after compensation triggered --- saga.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/saga.go b/saga.go index aef2483..2a01d25 100644 --- a/saga.go +++ b/saga.go @@ -11,10 +11,11 @@ import ( "reflect" "time" - "github.com/lysu/go-saga/storage" - "golang.org/x/net/context" "log" "os" + + "github.com/lysu/go-saga/storage" + "golang.org/x/net/context" ) const LogPrefix = "saga_" @@ -38,10 +39,11 @@ func SetLogger(l *log.Logger) { // Saga presents current execute transaction. // A Saga constituted by small sub-transactions. type Saga struct { - id uint64 - logID string - context context.Context - sec *ExecutionCoordinator + id uint64 + logID string + context context.Context + sec *ExecutionCoordinator + abortStatus bool } func (s *Saga) startSaga() { @@ -58,6 +60,10 @@ func (s *Saga) startSaga() { // ExecSub executes a sub-transaction for given subTxID(which define in SEC initialize) and arguments. // it returns current Saga. func (s *Saga) ExecSub(subTxID string, args ...interface{}) *Saga { + if s.abortStatus { + return s + } + subTxDef := s.sec.MustFindSubTxDef(subTxID) log := &Log{ Type: ActionStart, @@ -125,6 +131,8 @@ func (s *Saga) Abort() { if err != nil { panic("Add log Failure") } + + s.abortStatus = true for i := len(logs) - 1; i >= 0; i-- { logData := logs[i] log := mustUnmarshalLog(logData) From 1340ef108f4ff66f1835741618efda25c47811dd Mon Sep 17 00:00:00 2001 From: Alvin Rizki <4lvin.rizki@gmail.com> Date: Sun, 15 Sep 2019 15:38:53 +0700 Subject: [PATCH 2/2] add IsAbort() function --- saga.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/saga.go b/saga.go index 2a01d25..e294778 100644 --- a/saga.go +++ b/saga.go @@ -60,7 +60,7 @@ func (s *Saga) startSaga() { // ExecSub executes a sub-transaction for given subTxID(which define in SEC initialize) and arguments. // it returns current Saga. func (s *Saga) ExecSub(subTxID string, args ...interface{}) *Saga { - if s.abortStatus { + if s.IsAborted() { return s } @@ -100,7 +100,7 @@ func (s *Saga) ExecSub(subTxID string, args ...interface{}) *Saga { } // EndSaga finishes a Saga's execution. -func (s *Saga) EndSaga() { +func (s *Saga) EndSaga() *Saga { log := &Log{ Type: SagaEnd, Time: time.Now(), @@ -113,6 +113,13 @@ func (s *Saga) EndSaga() { if err != nil { panic("Clean up topic failure") } + + return s +} + +// IsAborted return status if saga is aborted or not +func (s *Saga) IsAborted() bool { + return s.abortStatus } // Abort stop and compensate to rollback to start situation.