this is such stupid code because I haven't switched to protobuf

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-10-16 20:43:01 -05:00
parent 1c77ec7e63
commit 142d9ab1cb
4 changed files with 77 additions and 61 deletions

View File

@ -9,7 +9,9 @@ all:
./virtigo --version ./virtigo --version
./virtigo --hosts farm01 farm02 farm03 ./virtigo --hosts farm01 farm02 farm03
start: start-all-droplets:
curl --silent http://localhost:8080/start?start=git.wit.org
curl --silent http://localhost:8080/start?start=go.wit.com
curl --silent http://localhost:8080/start?start=rdate.wit.com curl --silent http://localhost:8080/start?start=rdate.wit.com
@#curl --silent http://localhost:8080/start?start=jcarr @#curl --silent http://localhost:8080/start?start=jcarr
@# ./virtigo --start jcarr @# ./virtigo --start jcarr

View File

@ -1,6 +1,8 @@
package main package main
import ( import (
"fmt"
"math/rand"
"time" "time"
"go.wit.com/lib/gui/shell" "go.wit.com/lib/gui/shell"
@ -24,45 +26,95 @@ func (h *HyperT) RestartDaemon() {
} }
// checks if the cluster is ready and stable // checks if the cluster is ready and stable
func clusterReady() bool { func clusterReady() (bool, string) {
last := time.Since(me.unstable) last := time.Since(me.unstable)
if last > 133*time.Second { if last > 133*time.Second {
// the cluster has not been stable for 133 seconds // the cluster has not been stable for 133 seconds
log.Warn("clusterReady() is stable for 133s") log.Warn("clusterReady() is stable for 133s")
return true return true, "clusterReady() is stable for 133s"
} }
log.Warn("clusterReady() is unstable for", shell.FormatDuration(last)) log.Warn("clusterReady() is unstable for", shell.FormatDuration(last))
return false return false, "clusterReady() is unstable for " + shell.FormatDuration(last)
} }
func (d *DropletT) dropletReady() bool { func (d *DropletT) dropletReady() (bool, string) {
if d.CurrentState == "ON" { if d.CurrentState == "ON" {
log.Warn("EVENT start droplet is already ON") return false, "EVENT start droplet is already ON"
return false
} }
if d.starts > 2 { if d.starts > 2 {
log.Warn("EVENT start droplet has already been started", d.starts, "times") // reason := "EVENT start droplet has already been started " + d.starts + " times"
return false return false, fmt.Sprintln("EVENT start droplet has already been started ", d.starts, " times")
} }
return true return true, ""
} }
func (h *HyperT) Start(d *DropletT) { func (h *HyperT) Start(d *DropletT) (bool, string) {
if ! clusterReady() { ready, result := clusterReady()
return if !ready {
return false, result
} }
if ! d.dropletReady() { ready, result = d.dropletReady()
return if !ready {
return false, result
} }
url := "http://" + h.Hostname + ":2520/start?start=" + d.Hostname url := "http://" + h.Hostname + ":2520/start?start=" + d.Hostname
s := shell.Wget(url) s := shell.Wget(url)
log.Warn("EVENT start droplet url:", url) result = "EVENT start droplet url: " + url + "\n"
log.Warn("EVENT start droplet response:", s) result += "EVENT start droplet response: " + s.String()
// increment the counter for a start attempt working // increment the counter for a start attempt working
d.starts += 1 d.starts += 1
// mark the cluster as unstable so droplet starts can be throttled // mark the cluster as unstable so droplet starts can be throttled
me.unstable = time.Now() me.unstable = time.Now()
return true, result
}
func Start(name string) (bool, string) {
var result string
dur := time.Since(me.unstable) // how long has the cluster been stable?
result = fmt.Sprintln("should start droplet", name, "here. grid stable for:", shell.FormatDuration(dur))
if dur < 17*time.Second {
result += "grid is still too unstable"
return false, result
}
d := findDroplet(name)
if d == nil {
result += "can't start unknown droplet"
return false, result
}
// make the list of hypervisors that are active and can start new droplets
var pool []*HyperT
for _, h := range me.hypers {
result += fmt.Sprintln("could start droplet on", name, "on", h.Hostname, h.Active)
if d.hyperPreferred == h.Hostname {
// the config file says this droplet should run on this hypervisor
a, b := h.Start(d)
return a, result + b
}
if h.Active != true {
continue
}
pool = append(pool, h)
}
// left here as an example of how to actually do random numbers
// it's complete mathematical chaos. Randomness is simple when
// human interaction occurs -- which is exactly what happens most
// of the time. most random shit is bullshit. all you really need
// is exactly this to make sure the random functions work as they
// should. Probably, just use this everywhere in all cases. --jcarr
rand.Seed(time.Now().UnixNano())
a := 0
b := len(pool)
n := a + rand.Intn(b-a)
result += fmt.Sprintln("pool has", len(pool), "members", "rand =", n)
h := pool[n]
startbool, startresult := h.Start(d)
return startbool, result + startresult
} }

48
http.go
View File

@ -2,7 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"math/rand"
"net/http" "net/http"
"strings" "strings"
"time" "time"
@ -86,48 +85,11 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
if tmp == "/start" { if tmp == "/start" {
start := r.URL.Query().Get("start") start := r.URL.Query().Get("start")
log.Info("Handling URL:", tmp, "start droplet", start) // log.Warn("Handling URL:", tmp, "start droplet", start)
dur := time.Since(me.unstable) // how long has the cluster been stable? b, result := Start(start)
log.Warn("Start returned =", b, "result =", result)
fmt.Fprintln(w, "should start droplet here", start, shell.FormatDuration(dur)) fmt.Fprintln(w, "Start() returned", b)
if dur < 17*time.Second { fmt.Fprintln(w, "result:", result)
fmt.Fprintln(w, "grid is still to unstable")
return
}
d := findDroplet(start)
if d == nil {
fmt.Fprintln(w, "can't start unknown droplet", start)
return
}
// make the list of hypervisors that are active and can start new droplets
var pool []*HyperT
for _, h := range me.hypers {
fmt.Fprintln(w, "could start droplet on", start, "on", h.Hostname, h.Active)
if d.hyperPreferred == h.Hostname {
// the config file says this droplet should run on this hypervisor
h.Start(d)
return
}
if h.Active != true {
continue
}
pool = append(pool, h)
}
// left here as an example of how to actually do random numbers
// it's complete mathematical chaos. Randomness is simple when
// human interaction occurs -- which is exactly what happens most
// of the time. most random shit is bullshit. all you really need
// is exactly this to make sure the random functions work as they
// should. Probably, just use this everywhere in all cases. --jcarr
rand.Seed(time.Now().UnixNano())
a := 0
b := len(pool)
n := a + rand.Intn(b-a)
fmt.Fprintln(w, "pool has", len(pool), "members", "rand =", n)
h := pool[n]
h.Start(d)
return return
} }