Skip to content

Commit ab02a57

Browse files
lvan100lianghuan
authored andcommitted
refactor(gs_app): wait job exiting
1 parent e4f5575 commit ab02a57

File tree

2 files changed

+12
-54
lines changed

2 files changed

+12
-54
lines changed

gs/internal/gs_app/app.go

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"sync"
2626
"sync/atomic"
2727
"syscall"
28-
"time"
2928

3029
"github.com/go-spring/log"
3130
"github.com/go-spring/spring-core/conf"
@@ -55,8 +54,6 @@ type App struct {
5554

5655
EnableJobs bool `value:"${spring.app.enable-jobs:=true}"`
5756
EnableServers bool `value:"${spring.app.enable-servers:=true}"`
58-
59-
ShutDownTimeout time.Duration `value:"${spring.app.shutdown-timeout:=15s}"`
6057
}
6158

6259
// NewApp creates and initializes a new application instance.
@@ -137,7 +134,9 @@ func (app *App) Start() error {
137134
// runs all jobs
138135
if app.EnableJobs {
139136
for _, job := range app.Jobs {
137+
app.wg.Add(1)
140138
goutil.GoFunc(func() {
139+
defer app.wg.Done()
141140
defer func() {
142141
if r := recover(); r != nil {
143142
app.ShutDown()
@@ -188,29 +187,16 @@ func (app *App) Start() error {
188187
// Stop gracefully shuts down the application, ensuring all servers and
189188
// resources are properly closed.
190189
func (app *App) Stop() {
191-
ctx, cancel := context.WithTimeout(context.Background(), app.ShutDownTimeout)
192-
defer cancel()
193-
194-
waitChan := make(chan struct{})
195-
goutil.GoFunc(func() {
196-
for _, svr := range app.Servers {
197-
goutil.GoFunc(func() {
198-
if err := svr.Shutdown(ctx); err != nil {
199-
log.Errorf(context.Background(), log.TagAppDef, "shutdown server failed: %v", err)
200-
}
201-
})
202-
}
203-
app.wg.Wait()
204-
app.C.Close()
205-
waitChan <- struct{}{}
206-
})
207-
208-
select {
209-
case <-waitChan:
210-
log.Infof(context.Background(), log.TagAppDef, "shutdown complete")
211-
case <-ctx.Done():
212-
log.Infof(context.Background(), log.TagAppDef, "shutdown timeout")
190+
for _, svr := range app.Servers {
191+
goutil.GoFunc(func() {
192+
if err := svr.Shutdown(app.ctx); err != nil {
193+
log.Errorf(context.Background(), log.TagAppDef, "shutdown server failed: %v", err)
194+
}
195+
})
213196
}
197+
app.wg.Wait()
198+
app.C.Close()
199+
log.Infof(context.Background(), log.TagAppDef, "shutdown complete")
214200
}
215201

216202
// Exiting returns a boolean indicating whether the application is exiting.

gs/internal/gs_app/app_test.go

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func TestApp(t *testing.T) {
232232

233233
app.C.Object(r).AsJob().Name("j1")
234234
}
235-
j2Wait := make(chan struct{})
235+
j2Wait := make(chan struct{}, 1)
236236
{
237237
m := gsmock.NewManager()
238238
r := gs.NewJobMockImpl(m)
@@ -272,7 +272,6 @@ func TestApp(t *testing.T) {
272272
}
273273
go func() {
274274
time.Sleep(50 * time.Millisecond)
275-
assert.That(t, app.ShutDownTimeout).Equal(time.Second * 15)
276275
assert.That(t, app.EnableJobs).True()
277276
assert.That(t, app.EnableServers).True()
278277
assert.That(t, len(app.Jobs)).Equal(2)
@@ -287,33 +286,6 @@ func TestApp(t *testing.T) {
287286
assert.ThatString(t, logBuf.String()).Contains("shutdown complete")
288287
})
289288

290-
t.Run("shutdown timeout", func(t *testing.T) {
291-
Reset()
292-
t.Cleanup(Reset)
293-
294-
_ = gs_conf.SysConf.Set("spring.app.shutdown-timeout", "10ms")
295-
app := NewApp()
296-
297-
m := gsmock.NewManager()
298-
r := gs.NewServerMockImpl(m)
299-
r.MockShutdown().ReturnDefault()
300-
r.MockListenAndServe().Handle(func(sig gs.ReadySignal) error {
301-
<-sig.TriggerAndWait()
302-
time.Sleep(time.Second)
303-
return nil
304-
})
305-
306-
app.C.Object(r).AsServer()
307-
go func() {
308-
time.Sleep(50 * time.Millisecond)
309-
app.ShutDown()
310-
}()
311-
err := app.Run()
312-
assert.That(t, err).Nil()
313-
time.Sleep(50 * time.Millisecond)
314-
assert.ThatString(t, logBuf.String()).Contains("shutdown timeout")
315-
})
316-
317289
t.Run("shutdown error", func(t *testing.T) {
318290
Reset()
319291
t.Cleanup(Reset)

0 commit comments

Comments
 (0)