-
Notifications
You must be signed in to change notification settings - Fork 86
/
main.go
130 lines (113 loc) · 3.8 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"github.com/journeymidnight/yig/compression"
"github.com/journeymidnight/yig/crypto"
"math/rand"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"syscall"
"time"
"github.com/journeymidnight/yig/helper"
"github.com/journeymidnight/yig/iam"
"github.com/journeymidnight/yig/log"
"github.com/journeymidnight/yig/mods"
bus "github.com/journeymidnight/yig/mq"
"github.com/journeymidnight/yig/redis"
"github.com/journeymidnight/yig/storage"
)
func main() {
// Errors should cause panic so as to log to stderr for initialization functions
rand.Seed(time.Now().UnixNano())
helper.SetupConfig()
// yig log
logLevel := log.ParseLevel(helper.CONFIG.LogLevel)
helper.Logger = log.NewFileLogger(helper.CONFIG.LogPath, logLevel)
defer helper.Logger.Close()
helper.Logger.Info("YIG conf:", helper.CONFIG)
helper.Logger.Info("YIG instance ID:", helper.CONFIG.InstanceId)
// access log
helper.AccessLogger = log.NewFileLogger(helper.CONFIG.AccessLogPath, log.InfoLevel)
defer helper.AccessLogger.Close()
if helper.CONFIG.MetaCacheType > 0 || helper.CONFIG.EnableDataCache {
redis.Initialize()
defer redis.Close()
}
// Read all *.so from plugins directory, and fill the variable allPlugins
allPluginMap := mods.InitialPlugins()
kms := crypto.NewKMS(allPluginMap)
yig := storage.New(helper.CONFIG.MetaCacheType, helper.CONFIG.EnableDataCache, kms)
adminServerConfig := &adminServerConfig{
Address: helper.CONFIG.BindAdminAddress,
Logger: helper.Logger,
Yig: yig,
}
if redis.Pool() != nil && helper.CONFIG.CacheCircuitCheckInterval != 0 {
go yig.PingCache(time.Duration(helper.CONFIG.CacheCircuitCheckInterval) * time.Second)
}
// try to create message queue sender if message bus is enabled.
// message queue sender is singleton so create it beforehand.
mqSender, err := bus.InitMessageSender(allPluginMap)
if err != nil {
helper.Logger.Error("Failed to create message queue sender, err:", err)
panic("failed to create message bus sender")
}
if mqSender == nil {
helper.Logger.Error("Failed to create message queue sender, sender is nil.")
panic("failed to create message queue sender, sender is nil.")
}
helper.Logger.Info("Succeed to create message queue sender.")
// try to create compression if it is enabled.
if helper.CONFIG.EnableCompression == true {
compress, err := compression.InitCompression(allPluginMap)
if err != nil {
helper.Logger.Error("Failed to create compression unis, err:", err)
panic("failed to create compression unis")
}
if compress == nil {
helper.Logger.Error("Failed to create compression unis, unis is nil.")
panic("failed to create compression unis, unis is nil.")
}
helper.Logger.Info("Succeed to create compression unis.")
}
iam.InitializeIamClient(allPluginMap)
// Add pprof handler
if helper.CONFIG.EnablePProf {
go func() {
err := http.ListenAndServe("0.0.0.0:8730", nil)
helper.Logger.Error("Start pprof err:", err)
}()
}
startAdminServer(adminServerConfig)
apiServerConfig := &ServerConfig{
Address: helper.CONFIG.BindApiAddress,
KeyFilePath: helper.CONFIG.SSLKeyPath,
CertFilePath: helper.CONFIG.SSLCertPath,
Logger: helper.Logger,
ObjectLayer: yig,
}
startApiServer(apiServerConfig)
signal.Ignore()
signalQueue := make(chan os.Signal, 10)
signal.Notify(signalQueue, syscall.SIGINT, syscall.SIGTERM,
syscall.SIGQUIT, syscall.SIGHUP, syscall.SIGUSR1, syscall.SIGUSR2)
for {
s := <-signalQueue
switch s {
case syscall.SIGHUP:
// reload config file
helper.SetupConfig()
case syscall.SIGUSR1: // reopen log file, for log rotate
helper.Logger.ReopenLogFile()
case syscall.SIGUSR2: // reopen log file, for log rotate
helper.AccessLogger.ReopenLogFile()
default:
// stop YIG server, order matters
stopAdminServer()
stopApiServer()
yig.Stop()
return
}
}
}