-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
69 lines (59 loc) · 1.25 KB
/
main.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
package main
import (
sl "github.com/djosephsen/slacker/slackerlib"
"os/signal"
"syscall"
)
func main() {
//make a bot
bot := new(sl.Sbot)
if err := bot.Init(); err != nil {
sl.Logger.Error(err)
return
}
brain := *bot.Brain
defer brain.Close()
//start the read, write and broker threads
go bot.WriteThread.Start(bot)
go bot.Broker.Start(bot)
//Read in and register all the handlers, chores, filters, and hooks
if err := initPlugins(bot); err != nil {
sl.Logger.Error(err)
return
}
//run startup-hooks
if bot.StartupHooks != nil {
for _, h := range bot.StartupHooks {
go h.Run(bot)
}
}
// Start the chores
if bot.Chores != nil {
for _, c := range bot.Chores {
c.Start(bot)
}
}
// Loop
signal.Notify(bot.SigChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
stop := false
for !stop {
select {
case sig := <-bot.SigChan:
switch sig {
case syscall.SIGINT, syscall.SIGTERM:
stop = true
}
}
}
// Stop listening for new signals
signal.Stop(bot.SigChan)
// Run Shutdownn Hooks
if bot.ShutdownHooks != nil {
for _, h := range bot.ShutdownHooks {
h.Run(bot)
}
}
//wait for the write thread to stop (so the shutdown hooks have a chance to run)
bot.WriteThread.RunChan <- true
<-bot.SyncChan
}