-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathinternalagent_states.go
More file actions
158 lines (131 loc) · 4.43 KB
/
internalagent_states.go
File metadata and controls
158 lines (131 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package core
// InternalAgentState is internal agent state interface
type InternalAgentState interface {
Register([]Event) error
Ready() error
InitError(errorType string) error
ExitError(errorType string) error
Name() string
}
// InternalAgentStartedState is the initial state of an internal agent
type InternalAgentStartedState struct {
disallowEverything
agent *InternalAgent
}
// Register an agent with the platform when agent is in started state
func (s *InternalAgentStartedState) Register(events []Event) error {
for _, e := range events {
if err := s.agent.subscribeUnsafe(e); err != nil {
return err
}
}
s.agent.setStateUnsafe(s.agent.RegisteredState)
return nil
}
// Name return state's human friendly name
func (s *InternalAgentStartedState) Name() string {
return AgentStartedStateName
}
// InternalAgentRegisteredState is the state of an agent that registered with the platform but has not reported ready (next)
type InternalAgentRegisteredState struct {
disallowEverything
agent *InternalAgent
initFlow InitFlowSynchronization
}
// Ready - agent has called next and is now successfully initialized
func (s *InternalAgentRegisteredState) Ready() error {
s.agent.setStateUnsafe(s.agent.ReadyState)
s.initFlow.AgentReady()
s.agent.ManagedThread.SuspendUnsafe()
if s.agent.currentState != s.agent.ReadyState {
return ErrConcurrentStateModification
}
s.agent.setStateUnsafe(s.agent.RunningState)
return nil
}
// InitError - agent can transition to InitErrorState if it failed to initialize
func (s *InternalAgentRegisteredState) InitError(errorType string) error {
s.agent.setStateUnsafe(s.agent.InitErrorState)
s.agent.errorType = errorType
return nil
}
// ExitError - agent called /exit/error
func (s *InternalAgentRegisteredState) ExitError(errorType string) error {
s.agent.setStateUnsafe(s.agent.ExitErrorState)
s.agent.errorType = errorType
return nil
}
// Name return state's human friendly name
func (s *InternalAgentRegisteredState) Name() string {
return AgentRegisteredStateName
}
// InternalAgentReadyState is the state of an agent that reported ready to the platform
type InternalAgentReadyState struct {
disallowEverything
agent *InternalAgent
}
// ExitError - agent called /exit/error
func (s *InternalAgentReadyState) ExitError(errorType string) error {
s.agent.setStateUnsafe(s.agent.ExitErrorState)
s.agent.errorType = errorType
return nil
}
// Name return state's human friendly name
func (s *InternalAgentReadyState) Name() string {
return AgentReadyStateName
}
// InternalAgentRunningState is the state of an agent that is currently processing an event
type InternalAgentRunningState struct {
disallowEverything
agent *InternalAgent
invokeFlow InvokeFlowSynchronization
}
// Ready - agent can transition from ready to ready
func (s *InternalAgentRunningState) Ready() error {
s.agent.setStateUnsafe(s.agent.ReadyState)
s.invokeFlow.AgentReady()
s.agent.ManagedThread.SuspendUnsafe()
if s.agent.currentState != s.agent.ReadyState {
return ErrConcurrentStateModification
}
s.agent.setStateUnsafe(s.agent.RunningState)
return nil
}
// ExitError - agent called /exit/error
func (s *InternalAgentRunningState) ExitError(errorType string) error {
s.agent.setStateUnsafe(s.agent.ExitErrorState)
s.agent.errorType = errorType
return nil
}
// Name return state's human friendly name
func (s *InternalAgentRunningState) Name() string {
return AgentRunningStateName
}
// InternalAgentInitErrorState is a terminal state where agent has reported /init/error
type InternalAgentInitErrorState struct {
disallowEverything
}
// Name return state's human friendly name
func (s *InternalAgentInitErrorState) Name() string {
return AgentInitErrorStateName
}
// InitError - multiple calls are allowed, but only the first submitted error is accepted
func (s *InternalAgentInitErrorState) InitError(errorType string) error {
// no-op
return nil
}
// InternalAgentExitErrorState is a terminal state where agent has reported /exit/error
type InternalAgentExitErrorState struct {
disallowEverything
}
// Name return state's human friendly name
func (s *InternalAgentExitErrorState) Name() string {
return AgentExitErrorStateName
}
// ExitError - multiple calls are allowed, but only the first submitted error is accepted
func (s *InternalAgentExitErrorState) ExitError(errorType string) error {
// no-op
return nil
}