github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/cmd/subtool/poll.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/gob"
     5  	"fmt"
     6  	"os"
     7  	"time"
     8  
     9  	"github.com/Cloud-Foundations/Dominator/lib/format"
    10  	"github.com/Cloud-Foundations/Dominator/lib/log"
    11  	"github.com/Cloud-Foundations/Dominator/lib/srpc"
    12  	"github.com/Cloud-Foundations/Dominator/proto/sub"
    13  	"github.com/Cloud-Foundations/Dominator/sub/client"
    14  )
    15  
    16  func pollSubcommand(args []string, logger log.DebugLogger) error {
    17  	var err error
    18  	var srpcClient *srpc.Client
    19  	for iter := 0; *numPolls < 0 || iter < *numPolls; iter++ {
    20  		if iter > 0 {
    21  			time.Sleep(time.Duration(*interval) * time.Second)
    22  		}
    23  		if srpcClient == nil {
    24  			srpcClient = getSubClient(logger)
    25  		}
    26  		var request sub.PollRequest
    27  		var reply sub.PollResponse
    28  		request.ShortPollOnly = *shortPoll
    29  		pollStartTime := time.Now()
    30  		err = client.CallPoll(srpcClient, request, &reply)
    31  		fmt.Printf("Poll duration: %s\n", time.Since(pollStartTime))
    32  		if err != nil {
    33  			logger.Fatalf("Error calling: %s\n", err)
    34  		}
    35  		if *newConnection {
    36  			srpcClient.Close()
    37  			srpcClient = nil
    38  		}
    39  		fs := reply.FileSystem
    40  		if fs == nil {
    41  			if !*shortPoll {
    42  				fmt.Println("No FileSystem pointer")
    43  			}
    44  		} else {
    45  			fs.RebuildInodePointers()
    46  			if *debug {
    47  				fs.List(os.Stdout)
    48  			} else {
    49  				fmt.Println(fs)
    50  			}
    51  			fmt.Printf("Num objects: %d\n", len(reply.ObjectCache))
    52  			if *file != "" {
    53  				f, err := os.Create(*file)
    54  				if err != nil {
    55  					logger.Fatalf("Error creating: %s: %s\n", *file, err)
    56  				}
    57  				encoder := gob.NewEncoder(f)
    58  				encoder.Encode(fs)
    59  				f.Close()
    60  			}
    61  		}
    62  		if reply.LastSuccessfulImageName != "" {
    63  			fmt.Printf("Last successful image: \"%s\"\n",
    64  				reply.LastSuccessfulImageName)
    65  		}
    66  		if reply.FreeSpace != nil {
    67  			fmt.Printf("Free space: %s\n", format.FormatBytes(*reply.FreeSpace))
    68  		}
    69  	}
    70  	time.Sleep(time.Duration(*wait) * time.Second)
    71  	return nil
    72  }