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_instance.go 8.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package statelang
  18. import (
  19. "sync"
  20. "time"
  21. )
  22. type ExecutionStatus string
  23. const (
  24. // RU Running
  25. RU ExecutionStatus = "RU"
  26. // SU Succeed
  27. SU ExecutionStatus = "SU"
  28. // FA Failed
  29. FA ExecutionStatus = "FA"
  30. // UN Unknown
  31. UN ExecutionStatus = "UN"
  32. // SK Skipped
  33. SK ExecutionStatus = "SK"
  34. )
  35. type StateMachineInstance interface {
  36. ID() string
  37. SetID(id string)
  38. MachineID() string
  39. SetMachineID(machineID string)
  40. TenantID() string
  41. SetTenantID(tenantID string)
  42. ParentID() string
  43. SetParentID(parentID string)
  44. StartedTime() time.Time
  45. SetStartedTime(startedTime time.Time)
  46. EndTime() time.Time
  47. SetEndTime(endTime time.Time)
  48. StateList() []StateInstance
  49. State(stateId string) StateInstance
  50. PutState(stateId string, stateInstance StateInstance)
  51. Status() ExecutionStatus
  52. SetStatus(status ExecutionStatus)
  53. StateMap() map[string]StateInstance
  54. SetStateMap(stateMap map[string]StateInstance)
  55. CompensationStatus() ExecutionStatus
  56. SetCompensationStatus(compensationStatus ExecutionStatus)
  57. IsRunning() bool
  58. SetRunning(isRunning bool)
  59. UpdatedTime() time.Time
  60. SetUpdatedTime(updatedTime time.Time)
  61. BusinessKey() string
  62. SetBusinessKey(businessKey string)
  63. Exception() error
  64. SetException(err error)
  65. StartParams() map[string]interface{}
  66. SetStartParams(startParams map[string]interface{})
  67. EndParams() map[string]interface{}
  68. SetEndParams(endParams map[string]interface{})
  69. Context() map[string]interface{}
  70. PutContext(key string, value interface{})
  71. SetContext(context map[string]interface{})
  72. StateMachine() StateMachine
  73. SetStateMachine(stateMachine StateMachine)
  74. SerializedStartParams() interface{}
  75. SetSerializedStartParams(serializedStartParams interface{})
  76. SerializedEndParams() interface{}
  77. SetSerializedEndParams(serializedEndParams interface{})
  78. SerializedError() interface{}
  79. SetSerializedError(serializedError interface{})
  80. }
  81. type StateMachineInstanceImpl struct {
  82. id string
  83. machineId string
  84. tenantId string
  85. parentId string
  86. businessKey string
  87. startParams map[string]interface{}
  88. serializedStartParams interface{}
  89. startedTime time.Time
  90. endTime time.Time
  91. updatedTime time.Time
  92. exception error
  93. serializedError interface{}
  94. endParams map[string]interface{}
  95. serializedEndParams interface{}
  96. status ExecutionStatus
  97. compensationStatus ExecutionStatus
  98. isRunning bool
  99. context map[string]interface{}
  100. stateMachine StateMachine
  101. stateList []StateInstance
  102. stateMap map[string]StateInstance
  103. contextMutex sync.RWMutex // Mutex to protect concurrent access to context
  104. stateMutex sync.RWMutex // Mutex to protect concurrent access to stateList and stateMap
  105. }
  106. func NewStateMachineInstanceImpl() *StateMachineInstanceImpl {
  107. return &StateMachineInstanceImpl{
  108. startParams: make(map[string]interface{}),
  109. endParams: make(map[string]interface{}),
  110. stateList: make([]StateInstance, 0),
  111. stateMap: make(map[string]StateInstance)}
  112. }
  113. func (s *StateMachineInstanceImpl) ID() string {
  114. return s.id
  115. }
  116. func (s *StateMachineInstanceImpl) SetID(id string) {
  117. s.id = id
  118. }
  119. func (s *StateMachineInstanceImpl) MachineID() string {
  120. return s.machineId
  121. }
  122. func (s *StateMachineInstanceImpl) SetMachineID(machineID string) {
  123. s.machineId = machineID
  124. }
  125. func (s *StateMachineInstanceImpl) TenantID() string {
  126. return s.tenantId
  127. }
  128. func (s *StateMachineInstanceImpl) SetTenantID(tenantID string) {
  129. s.tenantId = tenantID
  130. }
  131. func (s *StateMachineInstanceImpl) ParentID() string {
  132. return s.parentId
  133. }
  134. func (s *StateMachineInstanceImpl) SetParentID(parentID string) {
  135. s.parentId = parentID
  136. }
  137. func (s *StateMachineInstanceImpl) StartedTime() time.Time {
  138. return s.startedTime
  139. }
  140. func (s *StateMachineInstanceImpl) SetStartedTime(startedTime time.Time) {
  141. s.startedTime = startedTime
  142. }
  143. func (s *StateMachineInstanceImpl) EndTime() time.Time {
  144. return s.endTime
  145. }
  146. func (s *StateMachineInstanceImpl) SetEndTime(endTime time.Time) {
  147. s.endTime = endTime
  148. }
  149. func (s *StateMachineInstanceImpl) StateList() []StateInstance {
  150. return s.stateList
  151. }
  152. func (s *StateMachineInstanceImpl) State(stateId string) StateInstance {
  153. s.stateMutex.RLock()
  154. defer s.stateMutex.RUnlock()
  155. return s.stateMap[stateId]
  156. }
  157. func (s *StateMachineInstanceImpl) PutState(stateId string, stateInstance StateInstance) {
  158. s.stateMutex.Lock()
  159. defer s.stateMutex.Unlock()
  160. stateInstance.SetStateMachineInstance(s)
  161. s.stateMap[stateId] = stateInstance
  162. s.stateList = append(s.stateList, stateInstance)
  163. }
  164. func (s *StateMachineInstanceImpl) Status() ExecutionStatus {
  165. return s.status
  166. }
  167. func (s *StateMachineInstanceImpl) SetStatus(status ExecutionStatus) {
  168. s.status = status
  169. }
  170. func (s *StateMachineInstanceImpl) StateMap() map[string]StateInstance {
  171. return s.stateMap
  172. }
  173. func (s *StateMachineInstanceImpl) SetStateMap(stateMap map[string]StateInstance) {
  174. s.stateMap = stateMap
  175. }
  176. func (s *StateMachineInstanceImpl) CompensationStatus() ExecutionStatus {
  177. return s.compensationStatus
  178. }
  179. func (s *StateMachineInstanceImpl) SetCompensationStatus(compensationStatus ExecutionStatus) {
  180. s.compensationStatus = compensationStatus
  181. }
  182. func (s *StateMachineInstanceImpl) IsRunning() bool {
  183. return s.isRunning
  184. }
  185. func (s *StateMachineInstanceImpl) SetRunning(isRunning bool) {
  186. s.isRunning = isRunning
  187. }
  188. func (s *StateMachineInstanceImpl) UpdatedTime() time.Time {
  189. return s.updatedTime
  190. }
  191. func (s *StateMachineInstanceImpl) SetUpdatedTime(updatedTime time.Time) {
  192. s.updatedTime = updatedTime
  193. }
  194. func (s *StateMachineInstanceImpl) BusinessKey() string {
  195. return s.businessKey
  196. }
  197. func (s *StateMachineInstanceImpl) SetBusinessKey(businessKey string) {
  198. s.businessKey = businessKey
  199. }
  200. func (s *StateMachineInstanceImpl) Exception() error {
  201. return s.exception
  202. }
  203. func (s *StateMachineInstanceImpl) SetException(err error) {
  204. s.exception = err
  205. }
  206. func (s *StateMachineInstanceImpl) StartParams() map[string]interface{} {
  207. return s.startParams
  208. }
  209. func (s *StateMachineInstanceImpl) SetStartParams(startParams map[string]interface{}) {
  210. s.startParams = startParams
  211. }
  212. func (s *StateMachineInstanceImpl) EndParams() map[string]interface{} {
  213. return s.endParams
  214. }
  215. func (s *StateMachineInstanceImpl) SetEndParams(endParams map[string]interface{}) {
  216. s.endParams = endParams
  217. }
  218. func (s *StateMachineInstanceImpl) Context() map[string]interface{} {
  219. return s.context
  220. }
  221. func (s *StateMachineInstanceImpl) PutContext(key string, value interface{}) {
  222. s.contextMutex.Lock()
  223. defer s.contextMutex.Unlock()
  224. s.context[key] = value
  225. }
  226. func (s *StateMachineInstanceImpl) SetContext(context map[string]interface{}) {
  227. s.context = context
  228. }
  229. func (s *StateMachineInstanceImpl) StateMachine() StateMachine {
  230. return s.stateMachine
  231. }
  232. func (s *StateMachineInstanceImpl) SetStateMachine(stateMachine StateMachine) {
  233. s.stateMachine = stateMachine
  234. s.machineId = stateMachine.ID()
  235. }
  236. func (s *StateMachineInstanceImpl) SerializedStartParams() interface{} {
  237. return s.serializedStartParams
  238. }
  239. func (s *StateMachineInstanceImpl) SetSerializedStartParams(serializedStartParams interface{}) {
  240. s.serializedStartParams = serializedStartParams
  241. }
  242. func (s *StateMachineInstanceImpl) SerializedEndParams() interface{} {
  243. return s.serializedEndParams
  244. }
  245. func (s *StateMachineInstanceImpl) SetSerializedEndParams(serializedEndParams interface{}) {
  246. s.serializedEndParams = serializedEndParams
  247. }
  248. func (s *StateMachineInstanceImpl) SerializedError() interface{} {
  249. return s.serializedError
  250. }
  251. func (s *StateMachineInstanceImpl) SetSerializedError(serializedError interface{}) {
  252. s.serializedError = serializedError
  253. }