-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
246 lines (210 loc) · 5.05 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"fmt"
"github.com/traPtitech/piscon-portal/service"
"log"
"os"
"strings"
"time"
"github.com/joho/godotenv"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
plugin "github.com/traPtitech/piscon-portal/aws"
"github.com/traPtitech/piscon-portal/model"
"github.com/traPtitech/piscon-portal/router"
sess "github.com/traPtitech/piscon-portal/session"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var (
checkTask chan struct{}
sendWorker chan *model.Task
checkInstance chan *model.Instance
db *gorm.DB
client model.ServerClient
)
const (
MAX_INSTANCE_NUMBER = 2
)
type Config plugin.Config
func main() {
sendWorker = make(chan *model.Task, 10)
checkTask = make(chan struct{})
checkInstance = make(chan *model.Instance)
go benchmarkWorker()
err := godotenv.Load()
if err != nil {
fmt.Println("Error loading .env file")
}
cfg, err := plugin.CreateDefaultConfig()
if err != nil {
log.Fatal(err)
}
client, err = plugin.New(*cfg)
if err != nil {
log.Fatal(err)
}
go instanceInfo(*cfg)
_db, err := establishConnection()
if err != nil {
panic(err)
}
//_db.LogMode(true)
_cl, err := _db.DB()
if err != nil {
log.Fatal(err)
}
defer _cl.Close()
db = _db
// db.LogMode(true)
db.AutoMigrate(&model.Task{}, &model.Message{}, &model.Result{}, &model.Instance{}, &model.Team{}, &model.User{}, &model.Question{})
tasks := []*model.Task{}
db.Not("state = 'done'").Find(&tasks)
for _, t := range tasks {
go func(task *model.Task) {
sendWorker <- task
}(t)
}
e := echo.New()
if err != nil {
log.Fatal(err)
}
s, err := sess.NewSession(_cl)
h := router.NewHandlers(client, db, checkInstance, sendWorker)
h.SetUp(e)
e.Use(middleware.CORS())
e.Use(session.Middleware(s.Store()))
e.Use(middleware.Recover())
e.Use(middleware.Logger())
e.Start(":4000")
}
func benchmarkWorker() {
for {
task := <-sendWorker
log.Println("recieve task")
task.State = "benchmark"
db.Save(task)
result := service.RunBenchmark(task)
db.Create(result)
task.State = "done"
db.Save(task)
}
}
// activeになったらdbにipアドレスとかを含めて登録
func instanceInfo(cfg plugin.Config) {
// 23時間ごとにtoken更新
t := time.NewTicker(23 * time.Hour)
for {
select {
case instance := <-checkInstance:
fmt.Println("receive instance")
go setupInstance(instance)
case <-t.C:
_client, err := plugin.New(cfg)
if err != nil {
log.Fatal(err)
}
client = _client
fmt.Println("Client created")
}
}
}
func setupInstance(_instance *model.Instance) {
instance := _instance
L:
for {
switch instance.Status {
case model.BUILDING:
log.Println("wait building")
instance = waitBuilding(instance)
case model.PRE_SHUTDOWN:
log.Println("pre shutdown")
instance.Status = model.SHUTDOWNING
time.Sleep(5 * time.Second)
client.StopInstance(instance.InstanceId)
case model.SHUTDOWNING:
log.Println("shutdowning")
instance = waitShutdown(instance)
case model.SHUTOFF:
log.Println("shutoff")
client.StartInstance(instance.Name)
instance.Status = model.STARTING
case model.STARTING:
log.Println("wait starting")
instance = waitStarting(instance)
case model.ACTIVE:
log.Println("write to db")
db.Where("instance_id = ?", instance.InstanceId).Updates(instance)
break L
}
}
}
func waitBuilding(instance *model.Instance) *model.Instance {
time.Sleep(10 * time.Second)
_instance, err := client.GetInstanceInfo(instance.InstanceId)
if err != nil {
fmt.Println(err)
}
if strings.ToUpper(_instance.Status) == model.ACTIVE {
instance.GlobalIPAddress = _instance.GlobalIPAddress
instance.Status = model.ACTIVE
}
return instance
}
func waitShutdown(instance *model.Instance) *model.Instance {
time.Sleep(10 * time.Second)
_instance, err := client.GetInstanceInfo(instance.InstanceId)
if err != nil {
fmt.Println(err)
}
if strings.ToUpper(_instance.Status) == model.SHUTOFF {
instance.Status = model.SHUTOFF
}
return instance
}
func waitStarting(instance *model.Instance) *model.Instance {
time.Sleep(10 * time.Second)
_instance, err := client.GetInstanceInfo(instance.InstanceId)
if err != nil {
fmt.Println(err)
}
if strings.ToUpper(_instance.Status) == model.ACTIVE {
instance.Status = model.ACTIVE
}
return instance
}
func establishConnection() (*gorm.DB, error) {
user := os.Getenv("MARIADB_USERNAME")
if user == "" {
user = "isucon"
}
port := os.Getenv("MARIADB_PORT")
if port == "" {
port = "3306"
}
pass := os.Getenv("MARIADB_PASSWORD")
if pass == "" {
pass = "isucon"
}
env := os.Getenv("ENV")
host := os.Getenv("MARIADB_HOSTNAME")
switch env {
case "prod":
if host == "" {
host = "db"
}
default:
if host == "" {
host = "db"
}
}
dbname := os.Getenv("MARIADB_DATABASE")
if dbname == "" {
dbname = "isucon"
}
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", user, pass, host, port, dbname) + "?parseTime=True&loc=Asia%2FTokyo&charset=utf8mb4"
log.Println(dsn)
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
return db, err
}