-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
85 lines (73 loc) · 1.92 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
package main
import (
"time"
)
var domainAgentDict = make(map[string]*WebsiteAgent)
// mainLoop creates newly enabled agents and close newly disabled agents.
func mainLoop() {
agents := make(map[string]*WebsiteAgent) // enabled agents map
for {
enabledAgents := currentConf.EnabledWebsites
if len(enabledAgents) == 0 {
logger.Warning("[Main] No enabled website.")
}
// add newly enabled agents
for _, agentName := range enabledAgents {
if _, exists := agents[agentName]; exists {
continue
}
found := false
var a *WebsiteAgent
for _, agent := range allAgents {
if agent.Name() == agentName {
found = true
a = agent
}
}
if !found {
logger.Errorf("[Main] Unknown website agent name %s, ignoring it", agentName)
continue
}
logger.Infof("[Main] Enabling agent %s...", agentName)
ctx, cancelFunc := GetBrowserCtx()
agentConf := currentConf.AgentConfs[a.Name()]
err := a.Init(ctx, cancelFunc, agentConf)
if err != nil {
logger.Errorf("[Main] init agent %s failed: %v", a.Name(), err)
continue
}
agents[agentName] = a
for _, domain := range a.BaseDomains() {
domainAgentDict[domain] = a
}
a.StartRefreshingRSS() // a goroutine will be spawned to periodically refresh RSS and save HTML content to local.
time.Sleep(1 * time.Second)
}
// close newly disabled agents
var toDel []string
for agentName := range agents {
if ContainsStringSlice(enabledAgents, agentName) {
continue
}
toDel = append(toDel, agentName)
}
for _, agentName := range toDel {
logger.Infof("[Main] Disabling agent %s...", agentName)
a := agents[agentName]
err := a.Shutdown()
if err != nil {
logger.Errorf("[Main] shutdown agent %s failed: %v", agentName, err)
}
delete(agents, agentName)
}
time.Sleep(1 * time.Second)
}
}
func main() {
initLogger()
initConf()
initDB()
startSaver()
go mainLoop()
runHTTPServer()
}