Skip to content

Commit 911ee3c

Browse files
lvan100lianghuan
authored andcommitted
feat(gs): add RunWith and RunAsync method
1 parent 5d506e6 commit 911ee3c

File tree

11 files changed

+2527
-5
lines changed

11 files changed

+2527
-5
lines changed

doc/examples/miniapi/main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
package main
1818

1919
import (
20+
"context"
2021
"net/http"
2122

2223
"github.com/go-spring/spring-core/gs"
24+
"github.com/go-spring/spring-core/util/syslog"
2325
)
2426

2527
func main() {
@@ -34,7 +36,10 @@ func main() {
3436
// - Property Binding: Binds external configs (YAML, ENV) into structs.
3537
// - Dependency Injection: Wires beans automatically.
3638
// - Dynamic Refresh: Updates configs at runtime without restart.
37-
gs.Run()
39+
gs.RunWith(func(ctx context.Context) error {
40+
syslog.Infof("app started")
41+
return nil
42+
})
3843
}
3944

4045
//~ curl http://127.0.0.1:9090/echo

doc/examples/noweb/main.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,19 @@ package main
1818

1919
import (
2020
"github.com/go-spring/spring-core/gs"
21+
"github.com/go-spring/spring-core/util/syslog"
2122
)
2223

2324
func main() {
2425
// Disable the built-in HTTP service.
25-
gs.Web(false).Run()
26+
stop, err := gs.Web(false).RunAsync()
27+
if err != nil {
28+
syslog.Errorf("app run failed: %s", err.Error())
29+
}
30+
defer stop()
31+
32+
syslog.Infof("app started")
33+
select {}
2634
}
2735

2836
// ~ telnet 127.0.0.1 9090

gs/gs.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ func Web(enable bool) *AppStarter {
215215

216216
// Run runs the app and waits for an interrupt signal to exit.
217217
func (s *AppStarter) Run() {
218+
s.RunWith(nil)
219+
}
220+
221+
// RunWith runs the app with a given function and waits for an interrupt signal to exit.
222+
func (s *AppStarter) RunWith(fn func(ctx context.Context) error) {
218223
var err error
219224
defer func() {
220225
if err != nil {
@@ -226,14 +231,32 @@ func (s *AppStarter) Run() {
226231
return
227232
}
228233
B = nil
229-
err = gs_app.GS.Run()
234+
err = gs_app.GS.RunWith(fn)
235+
}
236+
237+
// RunAsync runs the app asynchronously and returns a function to stop the app.
238+
func (s *AppStarter) RunAsync() (func(), error) {
239+
if err := gs_app.GS.Start(); err != nil {
240+
return nil, err
241+
}
242+
return func() { gs_app.GS.Stop() }, nil
230243
}
231244

232245
// Run runs the app and waits for an interrupt signal to exit.
233246
func Run() {
234247
new(AppStarter).Run()
235248
}
236249

250+
// RunWith runs the app with a given function and waits for an interrupt signal to exit.
251+
func RunWith(fn func(ctx context.Context) error) {
252+
new(AppStarter).RunWith(fn)
253+
}
254+
255+
// RunAsync runs the app asynchronously and returns a function to stop the app.
256+
func RunAsync() (func(), error) {
257+
return new(AppStarter).RunAsync()
258+
}
259+
237260
// Exiting returns a boolean indicating whether the application is exiting.
238261
func Exiting() bool {
239262
return gs_app.GS.Exiting()

gs/internal/gs_app/app.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,24 @@ func NewApp() *App {
7474
// (e.g., SIGINT, SIGTERM). Upon receiving a signal, it initiates
7575
// a graceful shutdown.
7676
func (app *App) Run() error {
77-
app.C.Object(app)
77+
return app.RunWith(nil)
78+
}
7879

80+
// RunWith starts the application and listens for termination signals
81+
// (e.g., SIGINT, SIGTERM). Upon receiving a signal, it initiates
82+
// a graceful shutdown.
83+
func (app *App) RunWith(fn func(ctx context.Context) error) error {
7984
if err := app.Start(); err != nil {
8085
return err
8186
}
8287

88+
// runs the user-defined function
89+
if fn != nil {
90+
if err := fn(app.ctx); err != nil {
91+
return err
92+
}
93+
}
94+
8395
// listens for OS termination signals
8496
go func() {
8597
ch := make(chan os.Signal, 1)
@@ -99,9 +111,10 @@ func (app *App) Run() error {
99111
// loading, IoC container refreshing, dependency injection, and runs
100112
// runners, jobs and servers.
101113
func (app *App) Start() error {
102-
var p conf.Properties
114+
app.C.Object(app)
103115

104116
// loads the layered app properties
117+
var p conf.Properties
105118
{
106119
var err error
107120
if p, err = app.P.Refresh(); err != nil {

0 commit comments

Comments
 (0)