-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_running.go
More file actions
211 lines (173 loc) · 6.13 KB
/
service_running.go
File metadata and controls
211 lines (173 loc) · 6.13 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* This file is part of Golaxy Distributed Service Development Framework.
*
* Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Golaxy Distributed Service Development Framework. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (c) 2024 pangdogs.
*/
package core
import (
"sync"
"time"
"git.golaxy.org/core/extension"
"git.golaxy.org/core/service"
"git.golaxy.org/core/utils/async"
"git.golaxy.org/core/utils/corectx"
"git.golaxy.org/core/utils/exception"
"git.golaxy.org/core/utils/generic"
"github.com/elliotchance/pie/v2"
)
// Run 运行
func (svc *ServiceBehavior) Run() async.Future {
ctx := svc.ctx
select {
case <-ctx.Done():
exception.Panicf("%w: %w", ErrService, ctx.Err())
default:
}
if !svc.isRunning.CompareAndSwap(false, true) {
exception.Panicf("%w: already running", ErrService)
}
if parentCtx, ok := ctx.ParentContext().(corectx.Context); ok {
if !parentCtx.WaitGroup().Join(1) {
ctx.Terminate()
corectx.UnsafeContext(ctx).CloseWaitGroup()
corectx.UnsafeContext(ctx).ReturnTerminated()
return ctx.Terminated()
}
}
go svc.running()
return ctx.Terminated()
}
// Terminate 停止
func (svc *ServiceBehavior) Terminate() async.Future {
return svc.ctx.Terminate()
}
// Terminated 已停止
func (svc *ServiceBehavior) Terminated() async.Future {
return svc.ctx.Terminated()
}
func (svc *ServiceBehavior) running() {
ctx := svc.ctx
svc.emitEventRunningEvent(service.RunningEvent_Starting)
svc.emitEventRunningEvent(service.RunningEvent_Started)
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
loop:
for {
select {
case <-ctx.Done():
break loop
case <-ticker.C:
svc.emitEventRunningEvent(service.RunningEvent_Heartbeat)
}
}
svc.emitEventRunningEvent(service.RunningEvent_Terminating)
corectx.UnsafeContext(ctx).CloseWaitGroup()
ctx.WaitGroup().Wait()
svc.emitEventRunningEvent(service.RunningEvent_Terminated)
if parentCtx, ok := ctx.ParentContext().(corectx.Context); ok {
parentCtx.WaitGroup().Done()
}
corectx.UnsafeContext(ctx).ReturnTerminated()
}
func (svc *ServiceBehavior) emitEventRunningEvent(runningEvent service.RunningEvent, args ...any) {
svc.onBeforeContextRunningEvent(svc.ctx, runningEvent, args...)
service.UnsafeContext(svc.ctx).EmitEventRunningEvent(runningEvent, args...)
svc.onAfterContextRunningEvent(svc.ctx, runningEvent, args...)
}
func (svc *ServiceBehavior) onBeforeContextRunningEvent(ctx service.Context, runningEvent service.RunningEvent, args ...any) {
switch runningEvent {
case service.RunningEvent_Starting:
svc.initEntityPT()
svc.initComponentPT()
svc.initAddIn()
}
}
func (svc *ServiceBehavior) onAfterContextRunningEvent(ctx service.Context, runningEvent service.RunningEvent, args ...any) {
switch runningEvent {
case service.RunningEvent_Terminated:
svc.shutAddIn()
}
}
func (svc *ServiceBehavior) initEntityPT() {
go func() {
for entityPT := range svc.ctx.EntityLib().Watch(svc.ctx) {
svc.emitEventRunningEvent(service.RunningEvent_EntityPTDeclared, entityPT)
}
}()
}
func (svc *ServiceBehavior) initComponentPT() {
go func() {
for compPT := range svc.ctx.EntityLib().ComponentLib().Watch(svc.ctx) {
svc.emitEventRunningEvent(service.RunningEvent_ComponentPTDeclared, compPT)
}
}()
}
func (svc *ServiceBehavior) initAddIn() {
var wg sync.WaitGroup
wg.Add(1)
go func() {
for event := range service.UnsafeContext(svc.ctx).AddInManager().WatchEvent(svc.ctx.Terminated().Context(nil)) {
switch e := event.(type) {
case *extension.EventServiceAddInSnapshot:
for _, status := range e.Statuses {
svc.activateAddIn(status)
}
wg.Done()
case *extension.EventServiceInstallAddIn:
select {
case <-svc.ctx.Done():
continue
default:
svc.activateAddIn(e.Status)
}
case *extension.EventServiceUninstallAddIn:
svc.deactivateAddIn(e.Status)
}
}
}()
wg.Wait()
}
func (svc *ServiceBehavior) shutAddIn() {
addInManager := service.UnsafeContext(svc.ctx).AddInManager()
for _, status := range pie.Reverse(extension.UnsafeServiceAddInManager(addInManager).List()) {
addInManager.Uninstall(status.Name())
<-status.WaitState(extension.AddInState_Unloaded).Done()
}
}
func (svc *ServiceBehavior) activateAddIn(status extension.ServiceAddInStatus) {
extension.UnsafeServiceAddInStatus(status).DoInstallOnce(func() {
svc.emitEventRunningEvent(service.RunningEvent_AddInActivating, status)
if cb, ok := status.InstanceFace().Iface.(LifecycleAddInInit); ok {
generic.CastAction2(cb.Init).Call(svc.ctx.AutoRecover(), svc.ctx.ReportError(), svc.ctx, nil)
} else if cb, ok := status.InstanceFace().Iface.(LifecycleServiceAddInInit); ok {
generic.CastAction1(cb.Init).Call(svc.ctx.AutoRecover(), svc.ctx.ReportError(), svc.ctx)
}
extension.UnsafeServiceAddInStatus(status).Started()
svc.emitEventRunningEvent(service.RunningEvent_AddInActivated, status)
})
}
func (svc *ServiceBehavior) deactivateAddIn(status extension.ServiceAddInStatus) {
extension.UnsafeServiceAddInStatus(status).DoUninstallOnce(func() {
svc.emitEventRunningEvent(service.RunningEvent_AddInDeactivating, status)
if cb, ok := status.InstanceFace().Iface.(LifecycleAddInShut); ok {
generic.CastAction2(cb.Shut).Call(svc.ctx.AutoRecover(), svc.ctx.ReportError(), svc.ctx, nil)
} else if cb, ok := status.InstanceFace().Iface.(LifecycleServiceAddInShut); ok {
generic.CastAction1(cb.Shut).Call(svc.ctx.AutoRecover(), svc.ctx.ReportError(), svc.ctx)
}
svc.emitEventRunningEvent(service.RunningEvent_AddInDeactivated, status)
extension.UnsafeServiceAddInStatus(status).Stopped()
})
}