Skip to content

Commit

Permalink
Replace astilectron event listeners by defer cleanup function
Browse files Browse the repository at this point in the history
-> fix Selenium ressources cleaning on astilectron app closing/crashing 
-> clean proxy-login-automator instance
  • Loading branch information
hbollon committed Apr 7, 2021
1 parent b09bd57 commit f6e6662
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 36 deletions.
17 changes: 1 addition & 16 deletions cmd/igopher/gui-bundler/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (

func main() {
igopher.CheckEnvironment()
defer igopher.BotStruct.SeleniumStruct.CleanUp()

if err := bootstrap.Run(bootstrap.Options{
Asset: Asset,
Expand All @@ -37,22 +38,6 @@ func main() {
// Add message handler
igopher.HandleMessages(ws[0])

// Add a listener on Astilectron crash event for selenium cleaning
a.On(astilectron.EventNameAppCrash, func(e astilectron.Event) (deleteListener bool) {
logrus.Error("Electron app has crashed!")
igopher.BotStruct.SeleniumStruct.CloseSelenium()
igopher.BotStruct.SeleniumStruct.Proxy.StopForwarderProxy()
return
})

// Add a listener on Astilectron close event for selenium cleaning
a.On(astilectron.EventNameAppClose, func(e astilectron.Event) (deleteListener bool) {
logrus.Debug("Electron app was closed")
igopher.BotStruct.SeleniumStruct.CloseSelenium()
igopher.BotStruct.SeleniumStruct.Proxy.StopForwarderProxy()
return
})

return nil
},
RestoreAssets: RestoreAssets,
Expand Down
17 changes: 1 addition & 16 deletions cmd/igopher/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (

func main() {
igopher.CheckEnvironment()
defer igopher.BotStruct.SeleniumStruct.CleanUp()

var w *astilectron.Window
// Create astilectron
Expand All @@ -45,22 +46,6 @@ func main() {
// Handle signals
a.HandleSignals()

// Add a listener on Astilectron crash event for selenium cleaning
a.On(astilectron.EventNameAppCrash, func(e astilectron.Event) (deleteListener bool) {
log.Error("Electron app has crashed!")
igopher.BotStruct.SeleniumStruct.CloseSelenium()
igopher.BotStruct.SeleniumStruct.Proxy.StopForwarderProxy()
return
})

// Add a listener on Astilectron close event for selenium cleaning
a.On(astilectron.EventNameAppClose, func(e astilectron.Event) (deleteListener bool) {
log.Debug("Electron app was closed")
igopher.BotStruct.SeleniumStruct.CloseSelenium()
igopher.BotStruct.SeleniumStruct.Proxy.StopForwarderProxy()
return
})

// Start
if err = a.Start(); err != nil {
log.Fatal(fmt.Errorf("main: starting astilectron failed: %w", err))
Expand Down
10 changes: 8 additions & 2 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ func (s *Selenium) SigTermCleaning() {
for {
select {
case <-sig:
s.CloseSelenium()
s.Proxy.StopForwarderProxy()
s.CleanUp()
os.Exit(1)
case <-s.SigTermRoutineExit:
s.SigTermRoutineExit = nil
Expand All @@ -181,6 +180,13 @@ func (s *Selenium) SigTermCleaning() {
}()
}

// CleanUp clean app ressources including Selenium stuff and proxy-login-automator instance (if exist)
func (s *Selenium) CleanUp() {
s.CloseSelenium()
s.Proxy.StopForwarderProxy()
logrus.Info("IGopher's ressources successfully cleared!")
}

/* Browser methods */

// IsElementPresent check if an element is present on the current webpage
Expand Down
3 changes: 1 addition & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ func randomMillisecondDuration(min, max float64) time.Duration {

// Fatal closes all selenium stuff and call logrus fatal with error printing
func (s *Selenium) Fatal(msg string, err error) {
s.CloseSelenium()
s.Proxy.StopForwarderProxy()
s.CleanUp()
logrus.Fatal(msg, err)
}

Expand Down

0 comments on commit f6e6662

Please sign in to comment.