Skip to content
This repository has been archived by the owner on Apr 30, 2023. It is now read-only.

fix issue action executed after compensation triggered #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions saga.go
Original file line number Diff line number Diff line change
Expand Up @@ -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_"
Expand All @@ -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() {
Expand All @@ -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.IsAborted() {
return s
}

subTxDef := s.sec.MustFindSubTxDef(subTxID)
log := &Log{
Type: ActionStart,
Expand Down Expand Up @@ -94,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(),
Expand All @@ -107,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.
Expand All @@ -125,6 +138,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)
Expand Down