Exemplo n.º 1
0
Arquivo: saga.go Projeto: lysu/go-saga
// 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 {
	subTxDef := s.sec.MustFindSubTxDef(subTxID)
	log := &Log{
		Type:    ActionStart,
		SubTxID: subTxID,
		Time:    time.Now(),
		Params:  MarshalParam(s.sec, args),
	}
	err := LogStorage().AppendLog(s.logID, log.mustMarshal())
	if err != nil {
		panic("Add log Failure")
	}

	params := make([]reflect.Value, 0, len(args)+1)
	params = append(params, reflect.ValueOf(s.context))
	for _, arg := range args {
		params = append(params, reflect.ValueOf(arg))
	}
	result := subTxDef.action.Call(params)
	if isReturnError(result) {
		s.Abort()
		return s
	}

	log = &Log{
		Type:    ActionEnd,
		SubTxID: subTxID,
		Time:    time.Now(),
	}
	err = LogStorage().AppendLog(s.logID, log.mustMarshal())
	if err != nil {
		panic("Add log Failure")
	}
	return s
}
Exemplo n.º 2
0
Arquivo: saga.go Projeto: lysu/go-saga
func (s *Saga) startSaga() {
	log := &Log{
		Type: SagaStart,
		Time: time.Now(),
	}
	err := LogStorage().AppendLog(s.logID, log.mustMarshal())
	if err != nil {
		panic("Add log Failure")
	}
}
Exemplo n.º 3
0
Arquivo: saga.go Projeto: lysu/go-saga
// EndSaga finishes a Saga's execution.
func (s *Saga) EndSaga() {
	log := &Log{
		Type: SagaEnd,
		Time: time.Now(),
	}
	err := LogStorage().AppendLog(s.logID, log.mustMarshal())
	if err != nil {
		panic("Add log Failure")
	}
	err = LogStorage().Cleanup(s.logID)
	if err != nil {
		panic("Clean up topic failure")
	}
}