-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
146 lines (133 loc) ยท 4.75 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"context"
"errors"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/sirupsen/logrus"
"github.com/umi0410/ezconfig"
"net/http"
"os"
"os/signal"
"runtime"
"strings"
"sync"
"time"
)
var (
// ์ด๋ฏธ์ง ๋ณํ ์์
์์ฒญ์ ์ฒ๋ฆฌํ๋ ์์ปค
TransformerWorkers []*Transformer
// ์
๋ก๋ ์์
์์ฒญ์ ์ฒ๋ฆฌํ๋ ์์ปค
UploaderWorker Uploader
// TransformerWorkers์ ์์
์ด ๋ชจ๋ ๋ง๋ฌด๋ฆฌ๋์๋์ง๋ฅผ ๊ด๋ฆฌํ๋ WaitGroup
transformerWorkersWG = new(sync.WaitGroup)
)
func init() {
workingDir, err := os.Getwd()
if err != nil {
logrus.Error(err)
}
logrus.SetReportCaller(true)
logrus.SetFormatter(&logrus.TextFormatter{
DisableColors: false,
DisableQuote: true,
ForceColors: true,
// line์ ๊น๋ํ๊ฒ ๋ณด์ฌ์ค.
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
filename := strings.Replace(f.File, workingDir+"/", "", -1)
return fmt.Sprintf("%s()", f.Function), fmt.Sprintf("%s:%d", filename, f.Line)
},
FullTimestamp: false,
TimestampFormat: "2006/01/03 15:04:05",
})
ezconfig.LoadConfig("KHUMU", Config, []string{"./config", os.Getenv("KHUMU_CONFIG_PATH")})
}
func main() {
logrus.Printf("KHUMU_ENVIRONMENT=%s", os.Getenv("KHUMU_ENVIRONMENT"))
InitTaskChannels()
StartTransformerWorkers()
StartUploaderWorker()
e := NewEcho()
// echo ์๋ฒ ์คํ
go func() {
err := e.Start(fmt.Sprintf("%s:%d", Config.Host, Config.Port))
if err != nil {
if errors.Is(err, http.ErrServerClosed) {
logrus.Info("์๋ฒ๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์ข
๋ฃ๋์์ต๋๋ค.")
} else {
logrus.Error(err)
}
}
}()
// echo์ graceful shutdown ์์
// https://echo.labstack.com/cookbook/graceful-shutdown/
// os์ SIGINT ์ ํธ๋ฅผ ๋ฐ์ ๊ฒฝ์ฐ echo๋ฅผ grafully shutdownํ๋ค. (ํ์ฌ ์๋ connection์ ์ ์ง)
// echo ์๋ฒ๋ ํ์ฌ ์ฐ๊ฒฐ๋์ด์๋ ์ปค๋ฅ์
๊น์ง๋ง ์ฒ๋ฆฌํ๊ณ ์ข
๋ฃ๋์ง๋ง
// ์ด๋ฏธ์ง ๋ณํ์ ์์ธ ์์
์ ๋ชจ๋ ์ฒ๋ฆฌํ๊ณ ์ ์ฒด ์๋น์ค๊ฐ ์ข
๋ฃ๋๋ค.
shutDownChan := make(chan os.Signal, 1)
signal.Notify(shutDownChan, os.Interrupt)
<-shutDownChan
if err := e.Shutdown(context.Background()); err != nil {
logrus.Error(err)
}
// Transformer๋ค์๊ฒ SIGINT๊ฐ ๋ฐ์ํ์์ ์ ํ
for i := 0; i < len(TransformerWorkers); i++ {
go func(transformerIdx int) {
logrus.Infof("Transformer %d์๊ฒ ์ข
๋ฃ ์ ํธ๋ฅผ ๋ณด๋
๋๋ค.", transformerIdx)
TransformerWorkers[transformerIdx].Quit <- struct{}{}
}(i)
}
select {
case <-allTransformerWorkersCompleted():
logrus.Infof("๋ชจ๋ ์์ปค๋ค์ด ์์
์ ์ข
๋ฃํ์ฌ ์๋ฒ๋ฅผ ์์ ํ๊ฒ ์ข
๋ฃํฉ๋๋ค. ํน์ ๋ชจ๋ฅผ ๋ฏธ์๋ฃ๋ ์
๋ก๋ ์์
์ ์ํด %d์ด๋ฅผ ๋๊ธฐํฉ๋๋ค.", Config.GracefulShutdown.UploaderTimeout)
time.Sleep(time.Duration(Config.GracefulShutdown.UploaderTimeout) * time.Second)
os.Exit(0)
case <-time.After(time.Duration(Config.GracefulShutdown.MaxTimeout) * time.Second):
logrus.Errorf("Graceful shutdown์ Max timeout์ธ %d์ด๊ฐ ๊ฒฝ๊ณผ๋์์์๋ ์์
์ ๋ชจ๋ ์๋ฃํ์ง ๋ชปํ์ต๋๋ค. ๊ฐ์ ๋ก ์ข
๋ฃํฉ๋๋ค. (๊ฐ๋ฐ ํ๊ฒฝ์์ ํธ์์ transformer์ loop delay๋ณด๋ค ์งง๊ฒ ๊ธฐ๋ค๋ฆฌ๋ ๊ฒฝ์ฐ ๋ฐ์ํ ์๋ ์์.)", Config.GracefulShutdown.MaxTimeout)
os.Exit(1)
}
}
func StartTransformerWorkers() {
num := Config.NumOfTransformerWorkers
TransformerWorkers = make([]*Transformer, num)
for i := 0; i < num; i++ {
TransformerWorkers[i] = NewTransformer(ResizeTaskChan, ThumbnailTaskChan, UploadTaskChan, make(chan interface{}), transformerWorkersWG)
go TransformerWorkers[i].Start()
logrus.Info("Started TransformerWorker", i)
}
}
func StartUploaderWorker() {
if Config.Storage.Disk.Enabled {
UploaderWorker = &DiskUploader{
UploadTaskChan: UploadTaskChan,
Quit: make(chan interface{}),
}
} else if Config.Storage.Aws.Enabled {
sess, err := session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Region: aws.String("ap-northeast-2"),
},
})
if err != nil {
logrus.Fatal(err)
}
UploaderWorker = NewS3Uploader(UploadTaskChan, make(chan interface{}), sess)
} else {
logrus.Fatal("Unsupported storage kind.")
}
go UploaderWorker.Start()
logrus.Info("Started UploaderWorker. ", UploaderWorker)
}
// ๋ชจ๋ Transformer๊ฐ ์์
์ ์ข
๋ฃํ๋ฉด channel์ ๊ฐ์ ์ ๋ฌํฉ๋๋ค.
// channel์ returnํ๊ธฐ ๋๋ฌธ์ select๋ฌธ์ ํตํด timeout์ด๋ default๋ฅผ ์ด์ฉํ๊ธฐ ํธ๋ผํฉ๋๋ค.
func allTransformerWorkersCompleted() <-chan struct{} {
logrus.Info("๋ชจ๋ ์์ปค๋ค์ด ์์
์ ์ข
๋ฃํ๋์ง ํ์ธํฉ๋๋ค.")
isAllCompleted := make(chan struct{})
go func() {
transformerWorkersWG.Wait()
logrus.Info("๋ชจ๋ ์์ปค๋ค์ด ์์
์ ์ข
๋ฃํ์ต๋๋ค.")
isAllCompleted <- struct{}{}
}()
return isAllCompleted
}