Skip to content

Commit 7e8c1f8

Browse files
committed
#200 #198 initialize part before console command, import path and wait group for event async
1 parent 0e588ec commit 7e8c1f8

File tree

5 files changed

+32
-13
lines changed

5 files changed

+32
-13
lines changed

aah.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -477,12 +477,19 @@ func (a *Application) ValidateValue(v interface{}, rules string) bool {
477477
// If anything goes wrong during an initialize process, it would return an error.
478478
func (a *Application) Run(args []string) error {
479479
var err error
480+
a.settings.SetImportPath(args) // only needed for development via CLI
480481
if err = a.initPath(); err != nil {
481482
return err
482483
}
483484
if err = a.initConfig(); err != nil {
484485
return err
485486
}
487+
if err = a.settings.Refresh(a.Config()); err != nil {
488+
return err
489+
}
490+
if err = a.initLog(); err != nil {
491+
return err
492+
}
486493
a.initCli()
487494
return a.cli.Run(args)
488495
}
@@ -564,14 +571,7 @@ func (a *Application) initApp() error {
564571
for event := range a.EventStore().subscribers {
565572
a.EventStore().sortEventSubscribers(event)
566573
}
567-
// publish `OnInit` server event
568-
a.EventStore().PublishSync(&Event{Name: EventOnInit})
569-
if err = a.settings.Refresh(a.Config()); err != nil {
570-
return err
571-
}
572-
if err = a.initLog(); err != nil {
573-
return err
574-
}
574+
a.EventStore().PublishSync(&Event{Name: EventOnInit}) // publish `OnInit` server event
575575
if err = a.initI18n(); err != nil {
576576
return err
577577
}

aah_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,10 @@ func newTestApp(t *testing.T, importPath string) *Application {
394394
assert.Nil(t, err, "app initPath failure")
395395
err = a.initConfig()
396396
assert.Nil(t, err, "app initConfig failure")
397+
err = a.settings.Refresh(a.Config())
398+
assert.Nil(t, err, "app settings failure")
399+
err = a.initLog()
400+
assert.Nil(t, err, "app log failure")
397401
err = a.initApp()
398402
assert.Nil(t, err, "app init failure")
399403

commands.go

-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ func (a *Application) cliCmdRun() console.Command {
101101
},
102102
Action: func(c *console.Context) error {
103103
a.Log().Infof("aah framework v%s, requires >= go1.11", a.BuildInfo().AahVersion)
104-
a.settings.ImportPath = c.String("importpath")
105104

106105
// External config file
107106
extCfgFile := c.String("config")

event.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -227,23 +227,29 @@ func (es *EventStore) Publish(e *Event) {
227227
return
228228
}
229229
es.a.Log().Debugf("Publishing event '%s' in asynchronous mode", e.Name)
230+
wg := sync.WaitGroup{}
230231
for idx, ec := range es.subscribers[e.Name] {
231232
if ec.CallOnce {
232233
if !ec.published {
233-
go func(event *Event, ecb EventCallbackFunc) {
234+
wg.Add(1)
235+
go func(w *sync.WaitGroup, event *Event, ecb EventCallbackFunc) {
236+
defer w.Done()
234237
ecb(event)
235-
}(e, ec.Callback)
238+
}(&wg, e, ec.Callback)
236239
es.mu.Lock()
237240
es.subscribers[e.Name][idx].published = true
238241
es.mu.Unlock()
239242
}
240243
continue
241244
}
242245

243-
go func(event *Event, ecb EventCallbackFunc) {
246+
wg.Add(1)
247+
go func(w *sync.WaitGroup, event *Event, ecb EventCallbackFunc) {
248+
defer w.Done()
244249
ecb(event)
245-
}(e, ec.Callback)
250+
}(&wg, e, ec.Callback)
246251
}
252+
wg.Wait()
247253
}
248254

249255
// PublishSync method publishes events to subscribed callbacks synchronously.

internal/settings/settings.go

+10
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,16 @@ func (s *Settings) Refresh(cfg *config.Config) error {
165165
return nil
166166
}
167167

168+
// SetImportPath method process import path and sets it into settings instance.
169+
func (s *Settings) SetImportPath(args []string) {
170+
for i, arg := range args {
171+
if arg == "--importpath" {
172+
s.ImportPath = args[i+1]
173+
break
174+
}
175+
}
176+
}
177+
168178
// SetEnvProfile method is to set application environment profile value.
169179
func (s *Settings) setEnvProfile(p string) error {
170180
if !strings.HasPrefix(p, ProfilePrefix) {

0 commit comments

Comments
 (0)