-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.go
67 lines (55 loc) · 1.86 KB
/
channel.go
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
package fleet
import (
"log"
"sync"
"time"
"github.com/KarpelesLab/goupd"
)
var channelUpdateLock sync.Mutex
// SwitchChannel will signal the whole fleet to switch to the given channel for
// the currently running software. This can be used to switch between stable
// and beta versions and the like. Attempting to switch to a non-existing
// channel will trigger errors across the fleet.
func (a *Agent) SwitchChannel(channel string) error {
// let's switch the running channel of the whole fleet
return a.feedDbSetBC([]byte("global"), []byte("channel"), []byte(channel), DbNow())
}
func (a *Agent) channelSet() {
// ensure db contains "channel" value
v, err := a.dbSimpleGet([]byte("global"), []byte("channel"))
if err == nil {
channel := string(v)
if channel != goupd.CHANNEL {
go a.performChannelUpdateLater()
}
return
}
// set a value with a DbStamp in the past in order to be overwritten if there is something better out there
// we do not broadcast in order to avoid weird stuff from happening
a.feedDbSet([]byte("global"), []byte("channel"), []byte(goupd.CHANNEL), DbStamp(time.Unix(0, 0))) // Jan 1st 1970
}
func (a *Agent) notifyChannelChange(channel string) {
if goupd.CHANNEL == channel {
return
}
// perform update later
go a.performChannelUpdateLater()
}
func (a *Agent) performChannelUpdateLater() {
// this is called when we probably need to update channel, but should wait for the db to sync first just in case
time.Sleep(15 * time.Second)
channelUpdateLock.Lock()
defer channelUpdateLock.Unlock()
v, err := a.dbSimpleGet([]byte("global"), []byte("channel"))
if err != nil {
log.Printf("fleet: failed to check current value for global:channel: %s", err)
return
}
channel := string(v)
if channel == goupd.CHANNEL {
// turns out we don't have to do anything
return
}
// let's perform the update
goupd.SwitchChannel(channel)
}