summaryrefslogtreecommitdiff
path: root/event.go
blob: 2956b0e3ff05092604c61113a262375653076615 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main

import (
	"time"

	"go.wit.com/lib/gui/shell"
	"go.wit.com/log"
)

func (d *DropletT) Start() {
	log.Info("a new virtual machine is running")
}

func (h *HyperT) RestartDaemon() {
	url := "http://" + h.Hostname + ":2520/kill"
	s := shell.Wget(url)
	log.Info("EVENT RestartDaemon", url, s)
	h.lastpoll = time.Now()
	h.killcount += 1

	dur := time.Since(h.lastpoll) // Calculate the elapsed time
	log.Info("KILLED DAEMON", h.Hostname, shell.FormatDuration(dur), "curl", url)
	me.killcount += 1
}

// checks if the cluster is ready and stable
func clusterReady() bool {
	last := time.Since(me.unstable)
	if last > 133*time.Second {
		// the cluster has not been stable for 133 seconds
		log.Warn("clusterReady() is stable for 133s")
		return true
	}
	log.Warn("clusterReady() is unstable for", shell.FormatDuration(last))
	return false
}

func (d *DropletT) dropletReady() bool {
	if d.CurrentState == "ON" {
		log.Warn("EVENT start droplet is already ON")
		return false
	}
	if d.starts > 2 {
		log.Warn("EVENT start droplet has already been started", d.starts, "times")
		return false
	}
	return true
}

func (h *HyperT) Start(d *DropletT) {
	if ! clusterReady() {
		return
	}
	if ! d.dropletReady() {
		return
	}

	url := "http://" + h.Hostname + ":2520/start?start=" + d.Hostname
	s := shell.Wget(url)
	log.Warn("EVENT start droplet url:", url)
	log.Warn("EVENT start droplet response:", s)

	// increment the counter for a start attempt working
	d.starts += 1

	// mark the cluster as unstable so droplet starts can be throttled
	me.unstable = time.Now()
}