Remove time.Ticker and adjust polling mechanism
[pstop.git] / main.go
diff --git a/main.go b/main.go
index 4825729..927a151 100644 (file)
--- a/main.go
+++ b/main.go
@@ -22,6 +22,7 @@ import (
        "github.com/sjmudd/pstop/lib"
        "github.com/sjmudd/pstop/state"
        "github.com/sjmudd/pstop/version"
+       "github.com/sjmudd/pstop/wait_info"
 )
 
 const (
@@ -83,16 +84,36 @@ func usage() {
 // rather than giving an error message if the requires P_S tables can't
 // be found.
 func validate_mysql_version(dbh *sql.DB) error {
-       lib.Logger.Println("validate_mysql_version()")
+       var tables = [...]string{
+               "performance_schema.file_summary_by_instance",
+               "performance_schema.table_io_waits_summary_by_table",
+               "performance_schema.table_lock_waits_summary_by_table",
+       }
 
-       _, mysql_version := lib.SelectGlobalVariableByVariableName(dbh, "VERSION")
+       lib.Logger.Println("validate_mysql_version()")
 
+       lib.Logger.Println("- Getting MySQL version")
+       err, mysql_version := lib.SelectGlobalVariableByVariableName(dbh, "VERSION")
+       if err != nil {
+               return err
+       }
        lib.Logger.Println("- mysql_version: '" + mysql_version + "'")
+
        if !re_valid_version.MatchString(mysql_version) {
                err := errors.New(lib.MyName() + " does not work with MySQL version " + mysql_version)
                return err
        }
-       lib.Logger.Println("- MySQL version is valid, continuing")
+       lib.Logger.Println("OK: MySQL version is valid, continuing")
+
+       lib.Logger.Println("Checking access to required tables:")
+       for i := range tables {
+               if err := lib.CheckTableAccess(dbh, tables[i]); err == nil {
+                       lib.Logger.Println("OK: " + tables[i] + " found")
+               } else {
+                       return err
+               }
+       }
+       lib.Logger.Println("OK: all table checks passed")
 
        return nil
 }
@@ -130,7 +151,9 @@ func main() {
        }
 
        var state state.State
-       interval := time.Second
+       var wi wait_info.WaitInfo
+       wi.SetWaitInterval(time.Second)
+
        sigChan := make(chan os.Signal, 1)
        done := make(chan struct{})
        defer close(done)
@@ -138,8 +161,6 @@ func main() {
 
        signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
 
-       ticker := time.NewTicker(interval) // generate a periodic signal
-
        state.Setup(dbh)
 
        finished := false
@@ -151,8 +172,9 @@ func main() {
                case sig := <-sigChan:
                        fmt.Println("Caught a signal", sig)
                        done <- struct{}{}
-               case <-ticker.C:
+               case <-wi.WaitNextPeriod():
                        state.Collect()
+                       wi.CollectedNow()
                        state.Display()
                case event := <-termboxChan:
                        // switch on event type
@@ -167,15 +189,11 @@ func main() {
                                }
                                switch event.Ch {
                                case '-': // decrease the interval if > 1
-                                       if interval > time.Second {
-                                               ticker.Stop()
-                                               interval -= time.Second
-                                               ticker = time.NewTicker(interval)
+                                       if wi.WaitInterval() > time.Second {
+                                               wi.SetWaitInterval(wi.WaitInterval() - time.Second)
                                        }
                                case '+': // increase interval by creating a new ticker
-                                       ticker.Stop()
-                                       interval += time.Second
-                                       ticker = time.NewTicker(interval)
+                                       wi.SetWaitInterval(wi.WaitInterval() + time.Second)
                                case 'h': // help
                                        state.SetHelp(!state.Help())
                                case 'q': // quit
@@ -196,6 +214,5 @@ func main() {
                }
        }
        state.Cleanup()
-       ticker.Stop()
        lib.Logger.Println("Terminating " + lib.MyName())
 }