@@ -14,7 +14,6 @@ import (
14
14
"github.com/Code-Hex/go-infinity-channel"
15
15
"github.com/oomol-lab/ovm/pkg/cli"
16
16
"github.com/oomol-lab/ovm/pkg/logger"
17
- "golang.org/x/sync/errgroup"
18
17
)
19
18
20
19
type key string
@@ -48,7 +47,10 @@ type event struct {
48
47
49
48
var e * event
50
49
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 {
52
54
log , err := logger .New (opt .LogPath , opt .Name + "-event" )
53
55
if err != nil {
54
56
return err
@@ -75,15 +77,7 @@ func Init(opt *cli.Context) error {
75
77
channel : infinity .NewChannel [* datum ](),
76
78
}
77
79
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 () {
87
81
for datum := range e .channel .Out () {
88
82
uri := fmt .Sprintf ("http://ovm/notify?event=%s&message=%s" , datum .name , url .QueryEscape (datum .message ))
89
83
e .log .Infof ("notify %s event to %s" , datum .name , uri )
@@ -98,14 +92,13 @@ func Subscribe(g *errgroup.Group) {
98
92
}
99
93
100
94
if datum .message == string (Exit ) {
101
- e .channel .Close ()
102
- e = nil
103
- return nil
95
+ waitDone <- struct {}{}
96
+ return
104
97
}
105
98
}
99
+ }()
106
100
107
- return nil
108
- })
101
+ return nil
109
102
}
110
103
111
104
func NotifyApp (name app ) {
@@ -117,6 +110,14 @@ func NotifyApp(name app) {
117
110
name : kApp ,
118
111
message : string (name ),
119
112
}
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
+ }
120
121
}
121
122
122
123
func NotifyError (err error ) {
0 commit comments