1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- package saga
- import (
- "sync"
- "time"
- )
- var sagaInstance *Saga
- var sagaInstanceMutex sync.Mutex
- type Saga struct {
- stateStoreName string
- name string
- orchestrators []*Orchestrator
- }
- func Init(stateStoreName string, sagaName string) {
- sagaInstanceMutex.Lock()
- defer sagaInstanceMutex.Unlock()
- if sagaInstance != nil {
- return
- }
- sagaInstance = new(Saga)
- sagaInstance.stateStoreName = stateStoreName
- sagaInstance.name = sagaName
- sagaInstance.orchestrators = make([]*Orchestrator, 0)
- }
- func Destroy() {
- sagaInstanceMutex.Lock()
- defer sagaInstanceMutex.Unlock()
- if sagaInstance == nil {
- return
- }
- for _, orchestrator := range sagaInstance.orchestrators {
- orchestrator.stop()
- }
- sagaInstance.orchestrators = nil
- sagaInstance.name = ""
- sagaInstance.stateStoreName = ""
- sagaInstance = nil
- }
- func GetInstance() *Saga {
- return sagaInstance
- }
- func (s *Saga) BuildOrchestrator(orchestratorName string, rollbackRetryPeriodSec time.Duration) *Orchestrator {
- sagaInstanceMutex.Lock()
- defer sagaInstanceMutex.Unlock()
- orchestrator := &Orchestrator{
- stateStoreName: s.stateStoreName,
- sagaName: s.name,
- name: orchestratorName,
- rollbackRetryPeriodSec: rollbackRetryPeriodSec,
- }
- sagaInstance.orchestrators = append(sagaInstance.orchestrators, orchestrator)
- return orchestrator
- }
- func (s *Saga) GetOrchestratorNeedRollback() ([]OrchestratorState, error) {
- return getSagaOrchestratorStates(s.stateStoreName, s.name)
- }
|