-
Notifications
You must be signed in to change notification settings - Fork 10
/
conservator.go
135 lines (113 loc) · 2.68 KB
/
conservator.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Package conservator herds service bots for external services.
package conservator
import (
"log"
"sync"
"github.com/antihax/evedata/internal/redisqueue"
"github.com/bwmarrin/discordgo"
"github.com/garyburd/redigo/redis"
"github.com/jmoiron/sqlx"
nsq "github.com/nsqio/go-nsq"
)
var NOTIFICATION_TYPES = []string{"kill", "war", "locator", "application", "structure"}
// Conservator Handles our little bot.
type Conservator struct {
stop chan bool
redis *redis.Pool
outQueue *redisqueue.RedisQueue
db *sqlx.DB
consumerAddresses []string
consumers map[string]*nsq.Consumer
wg *sync.WaitGroup
discord *discordgo.Session
discordToken string
solarSystems map[int32]float32
// Base Data
services sync.Map
channels sync.Map
shares sync.Map
warsMap map[int32]*sync.Map
// Notification data
notifications map[string]map[int32][]Share
notificationLock map[string]*sync.RWMutex
}
// NewConservator Service.
func NewConservator(redis *redis.Pool, db *sqlx.DB, addresses []string, discordToken string) *Conservator {
// Setup a new artifice
s := &Conservator{
stop: make(chan bool),
db: db,
redis: redis,
discordToken: discordToken,
consumerAddresses: addresses,
consumers: make(map[string]*nsq.Consumer),
warsMap: make(map[int32]*sync.Map),
wg: &sync.WaitGroup{},
outQueue: redisqueue.NewRedisQueue(
redis,
"evedata-hammer",
),
}
s.notifications = make(map[string]map[int32][]Share)
s.notificationLock = make(map[string]*sync.RWMutex)
for _, t := range NOTIFICATION_TYPES {
s.notifications[t] = make(map[int32][]Share)
s.notificationLock[t] = &sync.RWMutex{}
}
return s
}
// Close the conservator service
func (s *Conservator) Close() {
close(s.stop)
for _, h := range s.consumers {
h.Stop()
}
s.wg.Wait()
}
// Run the conservator service
func (s *Conservator) Run() {
var err error
s.discord, err = s.setupDiscord()
if err != nil {
log.Fatal(err)
}
// Load solarSystems
s.solarSystems, err = s.getSolarSystems()
if err != nil {
log.Fatal(err)
}
// Run the API
err = s.runRPC()
if err != nil {
log.Fatal(err)
}
if err = s.loadServices(); err != nil {
log.Fatal(err)
}
if err = s.loadChannels(); err != nil {
log.Fatal(err)
}
if err = s.loadShares(); err != nil {
log.Fatal(err)
}
if err = s.registerHandlers(); err != nil {
log.Fatal(err)
}
// update data
go s.updateData()
// Loop until we stop
for {
select {
case <-s.stop:
return
}
}
}
func inSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}