-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingularity.go
54 lines (47 loc) · 1.34 KB
/
singularity.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
package singularity
import "sync"
//NewSingularity creates a new singularity instance.
func NewSingularity() *Singularity {
n := &Singularity{
shutdown: make(chan int),
log: defaultLogger,
}
return n
}
//Singularity ... //TODO Complete
type Singularity struct {
sync.Mutex
Teams []SlackInstance
shutdown chan int
log func(level int, message string, i ...interface{})
}
//Shutdown leaves everything. It blocks as each team has to quit.
func (singularity *Singularity) Shutdown() {
singularity.Lock()
defer singularity.Unlock()
for i := 0; i < len(singularity.Teams); i++ {
singularity.Teams[i].quitShutdown() //Warning, this blocks.
singularity.Teams = append(singularity.Teams[:i], singularity.Teams[i+1:]...)
i--
}
singularity.shutdown <- 0
}
//RemoveTeam ...
func (singularity *Singularity) RemoveTeam(token string) {
singularity.Lock()
defer singularity.Unlock()
for i := 0; i < len(singularity.Teams); i++ { //a[:i], a[i+1:]...
if singularity.Teams[i].token == token {
singularity.Teams = append(singularity.Teams[:i], singularity.Teams[i+1:]...)
i--
}
}
}
//HardShutdown doesn't go to each team's shutdown.
func (singularity *Singularity) HardShutdown() {
singularity.shutdown <- 0
}
//WaitForShutdown blocks until shutdown.
func (singularity *Singularity) WaitForShutdown() int {
return <-singularity.shutdown
}