diff options
| author | Jeff Carr <[email protected]> | 2024-10-31 04:52:14 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2024-10-31 04:52:14 -0500 |
| commit | 67cb013c830db0da9918a125e2e4373993445939 (patch) | |
| tree | 5b7551808389da33bc303afea52266e1bfdf07a5 | |
| parent | 96f29d6f3b696347dea1c22a0f3bda834a136d32 (diff) | |
add time duration to cluster
Signed-off-by: Jeff Carr <[email protected]>
| -rw-r--r-- | add.go | 13 | ||||
| -rw-r--r-- | cluster.proto | 4 | ||||
| -rw-r--r-- | droplet.proto | 2 | ||||
| -rw-r--r-- | test.proto | 8 | ||||
| -rw-r--r-- | time.go | 64 |
5 files changed, 82 insertions, 9 deletions
@@ -2,8 +2,10 @@ package virtbuf import ( "fmt" + "time" "github.com/google/uuid" + "go.wit.com/log" ) // can the json protobuf output use a string and have a type handler @@ -129,6 +131,17 @@ func (epb *Events) AppendEvent(e *Event) { epb.Events = append(epb.Events, e) } +func (c *Cluster) ClusterStable() (bool, string) { + last := time.Since(c.Unstable.AsTime()) + if last > c.UnstableTimeout.AsDuration() { + // the cluster has not been stable for 133 seconds + log.Warn("clusterReady() is stable for ", FormatDuration(c.UnstableTimeout.AsDuration()), " secs") + return true, fmt.Sprintln("clusterReady() is stable ", FormatDuration(c.UnstableTimeout.AsDuration()), " secs") + } + log.Warn("clusterReady() is unstable for", FormatDuration(last)) + return false, "clusterReady() is unstable for " + FormatDuration(last) +} + // check the cluster and droplet to make sure it's ready to start func (c *Cluster) DropletReady(d *Droplet) (bool, string) { if c == nil { diff --git a/cluster.proto b/cluster.proto index 0c2616d..8658597 100644 --- a/cluster.proto +++ b/cluster.proto @@ -4,6 +4,8 @@ package virtbuf; import "droplet.proto"; import "hypervisor.proto"; import "event.proto"; +import "google/protobuf/timestamp.proto"; // Import the well-known type for Timestamp +import "google/protobuf/duration.proto"; // Import the well-known type for Timestamp message Cluster { int64 id = 1; @@ -16,4 +18,6 @@ message Cluster { Droplets d = 6; Hypervisors h = 7; Events e = 8; + google.protobuf.Timestamp unstable = 9; // the last time we heard anything from this droplet + google.protobuf.Duration unstable_timeout = 10; // the last time we heard anything from this droplet } diff --git a/droplet.proto b/droplet.proto index bed3e48..21333a0 100644 --- a/droplet.proto +++ b/droplet.proto @@ -1,7 +1,7 @@ syntax = "proto3"; package virtbuf; -import "google/protobuf/any.proto"; +// import "google/protobuf/any.proto"; import "google/protobuf/timestamp.proto"; // Import the well-known type for Timestamp message Droplets { diff --git a/test.proto b/test.proto deleted file mode 100644 index 8079240..0000000 --- a/test.proto +++ /dev/null @@ -1,8 +0,0 @@ -syntax = "proto3"; -package virtbuf; - -message Cluster { - int64 id = 1; - repeated string s = 2; - repeated int i = 3; -} @@ -0,0 +1,64 @@ +package virtbuf + +import ( + "fmt" + "time" +) + +func FormatDuration(d time.Duration) string { + result := "" + + // check if it's more than a year + years := int(d.Hours()) / (24 * 365) + if years > 0 { + result += fmt.Sprintf("%dy ", years) + return result + } + + // check if it's more than a day + days := int(d.Hours()) / 24 + if days > 0 { + result += fmt.Sprintf("%dd ", days) + return result + } + + // check if it's more than an hour + hours := int(d.Hours()) % 24 + if hours > 0 { + result += fmt.Sprintf("%dh ", hours) + return result + } + + // check if it's more than a minute + minutes := int(d.Minutes()) % 60 + if minutes > 0 { + result += fmt.Sprintf("%dm ", minutes) + return result + } + + // check if it's more than a second + seconds := int(d.Seconds()) % 60 + if seconds > 0 { + result += fmt.Sprintf("%ds", seconds) + return result + } + + // report in milliseconds + ms := int(d.Milliseconds()) + if ms > 100 { + // todo: print .3s, etc ? + return fmt.Sprintf("%1.2fs", seconds/1000) + } + result += fmt.Sprintf("%dms", ms) + return result +} + +func GetDurationStamp(t time.Time) string { + // Get the current time + currentTime := time.Now() + + // Calculate the duration between t current time + duration := currentTime.Sub(t) + + return FormatDuration(duration) +} |
