You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

statemachine.go 5.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package statelang
  2. import (
  3. "time"
  4. )
  5. type StateMachineStatus string
  6. const (
  7. Active StateMachineStatus = "Active"
  8. Inactive StateMachineStatus = "Inactive"
  9. )
  10. // RecoverStrategy : Recover Strategy
  11. type RecoverStrategy string
  12. const (
  13. //Compensate stateMachine
  14. Compensate RecoverStrategy = "Compensate"
  15. // Forward stateMachine
  16. Forward RecoverStrategy = "Forward"
  17. )
  18. func ValueOfRecoverStrategy(recoverStrategy string) (RecoverStrategy, bool) {
  19. switch recoverStrategy {
  20. case "Compensate":
  21. return Compensate, true
  22. case "Forward":
  23. return Forward, true
  24. default:
  25. var recoverStrategy RecoverStrategy
  26. return recoverStrategy, false
  27. }
  28. }
  29. type StateMachine interface {
  30. ID() string
  31. SetID(id string)
  32. Name() string
  33. SetName(name string)
  34. Comment() string
  35. SetComment(comment string)
  36. StartState() string
  37. SetStartState(startState string)
  38. Version() string
  39. SetVersion(version string)
  40. States() map[string]State
  41. State(stateName string) State
  42. TenantId() string
  43. SetTenantId(tenantId string)
  44. AppName() string
  45. SetAppName(appName string)
  46. Type() string
  47. SetType(typeName string)
  48. Status() StateMachineStatus
  49. SetStatus(status StateMachineStatus)
  50. RecoverStrategy() RecoverStrategy
  51. SetRecoverStrategy(recoverStrategy RecoverStrategy)
  52. IsPersist() bool
  53. SetPersist(persist bool)
  54. IsRetryPersistModeUpdate() bool
  55. SetRetryPersistModeUpdate(retryPersistModeUpdate bool)
  56. IsCompensatePersistModeUpdate() bool
  57. SetCompensatePersistModeUpdate(compensatePersistModeUpdate bool)
  58. Content() string
  59. SetContent(content string)
  60. CreateTime() time.Time
  61. SetCreateTime(createTime time.Time)
  62. }
  63. type StateMachineImpl struct {
  64. id string
  65. tenantId string
  66. appName string
  67. name string
  68. comment string
  69. version string
  70. startState string
  71. status StateMachineStatus
  72. recoverStrategy RecoverStrategy
  73. persist bool
  74. retryPersistModeUpdate bool
  75. compensatePersistModeUpdate bool
  76. typeName string
  77. content string
  78. createTime time.Time
  79. states map[string]State
  80. }
  81. func NewStateMachineImpl() *StateMachineImpl {
  82. stateMap := make(map[string]State)
  83. return &StateMachineImpl{
  84. appName: "SEATA",
  85. status: Active,
  86. typeName: "STATE_LANG",
  87. states: stateMap,
  88. }
  89. }
  90. func (s *StateMachineImpl) ID() string {
  91. return s.id
  92. }
  93. func (s *StateMachineImpl) SetID(id string) {
  94. s.id = id
  95. }
  96. func (s *StateMachineImpl) Name() string {
  97. return s.name
  98. }
  99. func (s *StateMachineImpl) SetName(name string) {
  100. s.name = name
  101. }
  102. func (s *StateMachineImpl) SetComment(comment string) {
  103. s.comment = comment
  104. }
  105. func (s *StateMachineImpl) Comment() string {
  106. return s.comment
  107. }
  108. func (s *StateMachineImpl) StartState() string {
  109. return s.startState
  110. }
  111. func (s *StateMachineImpl) SetStartState(startState string) {
  112. s.startState = startState
  113. }
  114. func (s *StateMachineImpl) Version() string {
  115. return s.version
  116. }
  117. func (s *StateMachineImpl) SetVersion(version string) {
  118. s.version = version
  119. }
  120. func (s *StateMachineImpl) States() map[string]State {
  121. return s.states
  122. }
  123. func (s *StateMachineImpl) State(stateName string) State {
  124. if s.states == nil {
  125. return nil
  126. }
  127. return s.states[stateName]
  128. }
  129. func (s *StateMachineImpl) TenantId() string {
  130. return s.tenantId
  131. }
  132. func (s *StateMachineImpl) SetTenantId(tenantId string) {
  133. s.tenantId = tenantId
  134. }
  135. func (s *StateMachineImpl) AppName() string {
  136. return s.appName
  137. }
  138. func (s *StateMachineImpl) SetAppName(appName string) {
  139. s.appName = appName
  140. }
  141. func (s *StateMachineImpl) Type() string {
  142. return s.typeName
  143. }
  144. func (s *StateMachineImpl) SetType(typeName string) {
  145. s.typeName = typeName
  146. }
  147. func (s *StateMachineImpl) Status() StateMachineStatus {
  148. return s.status
  149. }
  150. func (s *StateMachineImpl) SetStatus(status StateMachineStatus) {
  151. s.status = status
  152. }
  153. func (s *StateMachineImpl) RecoverStrategy() RecoverStrategy {
  154. return s.recoverStrategy
  155. }
  156. func (s *StateMachineImpl) SetRecoverStrategy(recoverStrategy RecoverStrategy) {
  157. s.recoverStrategy = recoverStrategy
  158. }
  159. func (s *StateMachineImpl) IsPersist() bool {
  160. return s.persist
  161. }
  162. func (s *StateMachineImpl) SetPersist(persist bool) {
  163. s.persist = persist
  164. }
  165. func (s *StateMachineImpl) IsRetryPersistModeUpdate() bool {
  166. return s.retryPersistModeUpdate
  167. }
  168. func (s *StateMachineImpl) SetRetryPersistModeUpdate(retryPersistModeUpdate bool) {
  169. s.retryPersistModeUpdate = retryPersistModeUpdate
  170. }
  171. func (s *StateMachineImpl) IsCompensatePersistModeUpdate() bool {
  172. return s.compensatePersistModeUpdate
  173. }
  174. func (s *StateMachineImpl) SetCompensatePersistModeUpdate(compensatePersistModeUpdate bool) {
  175. s.compensatePersistModeUpdate = compensatePersistModeUpdate
  176. }
  177. func (s *StateMachineImpl) Content() string {
  178. return s.content
  179. }
  180. func (s *StateMachineImpl) SetContent(content string) {
  181. s.content = content
  182. }
  183. func (s *StateMachineImpl) CreateTime() time.Time {
  184. return s.createTime
  185. }
  186. func (s *StateMachineImpl) SetCreateTime(createTime time.Time) {
  187. s.createTime = createTime
  188. }