@@ -14,7 +14,6 @@ import (
1414 "github.com/Code-Hex/go-infinity-channel"
1515 "github.com/oomol-lab/ovm/pkg/cli"
1616 "github.com/oomol-lab/ovm/pkg/logger"
17- "golang.org/x/sync/errgroup"
1817)
1918
2019type key string
@@ -48,7 +47,10 @@ type event struct {
4847
4948var e * event
5049
51- func Init (opt * cli.Context ) error {
50+ // see: https://github.com/Code-Hex/go-infinity-channel/issues/1
51+ var waitDone = make (chan struct {})
52+
53+ func Setup (opt * cli.Context ) error {
5254 log , err := logger .New (opt .LogPath , opt .Name + "-event" )
5355 if err != nil {
5456 return err
@@ -75,15 +77,7 @@ func Init(opt *cli.Context) error {
7577 channel : infinity .NewChannel [* datum ](),
7678 }
7779
78- return nil
79- }
80-
81- func Subscribe (g * errgroup.Group ) {
82- if e == nil {
83- return
84- }
85-
86- g .Go (func () error {
80+ go func () {
8781 for datum := range e .channel .Out () {
8882 uri := fmt .Sprintf ("http://ovm/notify?event=%s&message=%s" , datum .name , url .QueryEscape (datum .message ))
8983 e .log .Infof ("notify %s event to %s" , datum .name , uri )
@@ -98,14 +92,13 @@ func Subscribe(g *errgroup.Group) {
9892 }
9993
10094 if datum .message == string (Exit ) {
101- e .channel .Close ()
102- e = nil
103- return nil
95+ waitDone <- struct {}{}
96+ return
10497 }
10598 }
99+ }()
106100
107- return nil
108- })
101+ return nil
109102}
110103
111104func NotifyApp (name app ) {
@@ -117,6 +110,14 @@ func NotifyApp(name app) {
117110 name : kApp ,
118111 message : string (name ),
119112 }
113+
114+ // wait for the event to be processed
115+ // Exit event indicates the main process exit
116+ if string (name ) == string (Exit ) {
117+ <- waitDone
118+ close (waitDone )
119+ e .channel .Close ()
120+ }
120121}
121122
122123func NotifyError (err error ) {
0 commit comments