github.com/Cloud-Foundations/Dominator@v0.3.4/cmd/subtool/poll.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/gob"
     5  	"encoding/json"
     6  	"fmt"
     7  	"os"
     8  	"path/filepath"
     9  	"time"
    10  
    11  	"github.com/Cloud-Foundations/Dominator/lib/format"
    12  	"github.com/Cloud-Foundations/Dominator/lib/log"
    13  	"github.com/Cloud-Foundations/Dominator/lib/srpc"
    14  	"github.com/Cloud-Foundations/Dominator/proto/sub"
    15  	"github.com/Cloud-Foundations/Dominator/sub/client"
    16  )
    17  
    18  type encoderType interface {
    19  	Encode(v interface{}) error
    20  }
    21  
    22  func pollSubcommand(args []string, logger log.DebugLogger) error {
    23  	var err error
    24  	var srpcClient *srpc.Client
    25  	for iter := 0; *numPolls < 0 || iter < *numPolls; iter++ {
    26  		if iter > 0 {
    27  			time.Sleep(time.Duration(*interval) * time.Second)
    28  		}
    29  		if srpcClient == nil {
    30  			srpcClient = getSubClient(logger)
    31  		}
    32  		var request sub.PollRequest
    33  		var reply sub.PollResponse
    34  		request.ShortPollOnly = *shortPoll
    35  		pollStartTime := time.Now()
    36  		err = client.CallPoll(srpcClient, request, &reply)
    37  		fmt.Printf("Poll duration: %s, ScanCount: %d, GenerationCount: %d\n",
    38  			time.Since(pollStartTime), reply.ScanCount, reply.GenerationCount)
    39  		if err != nil {
    40  			logger.Fatalf("Error calling: %s\n", err)
    41  		}
    42  		if *newConnection {
    43  			srpcClient.Close()
    44  			srpcClient = nil
    45  		}
    46  		fs := reply.FileSystem
    47  		if fs == nil {
    48  			if !*shortPoll {
    49  				fmt.Println("No FileSystem pointer")
    50  			}
    51  		} else {
    52  			fs.RebuildInodePointers()
    53  			if *debug {
    54  				fs.List(os.Stdout)
    55  			} else {
    56  				fmt.Println(fs)
    57  			}
    58  			fmt.Printf("Num objects: %d\n", len(reply.ObjectCache))
    59  			if *file != "" {
    60  				f, err := os.Create(*file)
    61  				if err != nil {
    62  					logger.Fatalf("Error creating: %s: %s\n", *file, err)
    63  				}
    64  				var encoder encoderType
    65  				if filepath.Ext(*file) == ".json" {
    66  					e := json.NewEncoder(f)
    67  					e.SetIndent("", "    ")
    68  					encoder = e
    69  				} else {
    70  					encoder = gob.NewEncoder(f)
    71  				}
    72  				encoder.Encode(fs)
    73  				f.Close()
    74  			}
    75  		}
    76  		if reply.InitialImageName != "" {
    77  			fmt.Printf("Initial image: \"%s\"\n", reply.InitialImageName)
    78  		}
    79  		if reply.LastSuccessfulImageName != "" {
    80  			fmt.Printf("Last successful image: \"%s\"\n",
    81  				reply.LastSuccessfulImageName)
    82  		}
    83  		if reply.LastNote != "" {
    84  			fmt.Printf("Last note: \"%s\"\n", reply.LastNote)
    85  		}
    86  		if reply.LastWriteError != "" {
    87  			fmt.Printf("Last write error: %s\n", reply.LastWriteError)
    88  		}
    89  		if reply.FreeSpace != nil {
    90  			fmt.Printf("Free space: %s\n", format.FormatBytes(*reply.FreeSpace))
    91  		}
    92  		if reply.SystemUptime != nil {
    93  			fmt.Printf("System uptime: %s\n",
    94  				format.Duration(*reply.SystemUptime))
    95  		}
    96  		if reply.DisruptionState != sub.DisruptionStateAnytime {
    97  			fmt.Printf("Disruption state: %s\n", reply.DisruptionState)
    98  		}
    99  		if reply.LockedByAnotherClient {
   100  			fmt.Printf("Locked by another client\n")
   101  		}
   102  		if lockedFor := time.Until(reply.LockedUntil); lockedFor > 0 {
   103  			fmt.Printf("Locked for: %s\n", format.Duration(lockedFor))
   104  		}
   105  
   106  	}
   107  	time.Sleep(time.Duration(*wait) * time.Second)
   108  	return nil
   109  }