-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
116 lines (98 loc) · 2.37 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
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
package main
import (
"context"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/rroy233/StickerDownloader/config"
"github.com/rroy233/StickerDownloader/db"
"github.com/rroy233/StickerDownloader/handler"
"github.com/rroy233/StickerDownloader/languages"
"github.com/rroy233/StickerDownloader/router"
"github.com/rroy233/StickerDownloader/statistics"
"github.com/rroy233/StickerDownloader/utils"
"gopkg.in/rroy233/logger.v2"
"log"
"os"
"os/signal"
"syscall"
"time"
)
var bot *tgbotapi.BotAPI
var cancel context.CancelFunc
var stopCtx context.Context
var cancelCh chan int
func main() {
//config
config.Init()
log.Println("[main]config=" + utils.JsonEncode(config.Get()))
//logger
logger.New(
&logger.Config{
StdOutput: true,
StoreLocalFile: true,
StoreRemote: config.Get().Logger.Report,
RemoteConfig: logger.RemoteConfigStruct{
RequestUrl: config.Get().Logger.ReportUrl,
QueryKey: config.Get().Logger.ReportQueryKey,
},
})
//language
languages.Init()
var err error
bot, err = tgbotapi.NewBotAPI(config.Get().General.BotToken)
if err != nil {
logger.FATAL.Fatalln(err.Error())
}
//init
time.Local = time.FixedZone("CST", 8*3600)
config.Init()
rdb := db.Init()
statistics.InitStatistic(rdb)
utils.Init(bot)
handler.Init(bot)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
stopCtx, cancel = context.WithCancel(context.Background())
cancelCh = make(chan int, config.Get().General.WorkerNum)
for i := 0; i < config.Get().General.WorkerNum; i++ {
go worker(stopCtx, updates, cancelCh)
}
logger.Info.Println(languages.Get(nil).System.Running)
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGKILL, syscall.SIGTERM)
<-sigCh
Stop()
logger.Info.Println(languages.Get(nil).System.StopRunning)
}
func Stop() {
cancel()
waitForDone(cancelCh)
//clean temp files
utils.CleanTmp()
//store statistics data into redis
statistics.Statistics.Save()
//close db
db.Close()
}
func worker(stopCtx context.Context, uc tgbotapi.UpdatesChannel, cancelCh chan int) {
for {
select {
case update := <-uc:
utils.Limiter.Take()
go router.Handle(update)
case <-stopCtx.Done():
cancelCh <- 1
return
}
}
}
func waitForDone(cancelCh chan int) {
num := 0
for {
if num == config.Get().General.WorkerNum {
break
}
<-cancelCh
num++
}
}