-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
200 lines (166 loc) · 4.42 KB
/
server.go
File metadata and controls
200 lines (166 loc) · 4.42 KB
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
package server
import (
"context"
"errors"
"io"
"github.com/aws/aws-sdk-go/aws/session"
colorable "github.com/mattn/go-colorable"
"github.com/rai-project/auth"
"github.com/rai-project/aws"
"github.com/rai-project/broker"
"github.com/rai-project/broker/sqs"
"github.com/rai-project/config"
"github.com/rai-project/docker"
"github.com/rai-project/model"
"github.com/rai-project/pubsub"
"github.com/rai-project/serializer"
"github.com/rai-project/serializer/json"
"github.com/rai-project/uuid"
)
type Server struct {
ID string
awsSession *session.Session
options Options
broker broker.Broker
pubsubConn pubsub.Connection
profile auth.Profile
isConnected bool
serializer serializer.Serializer
jobSubscriber broker.Subscriber
dispatcher *Dispatcher
publishers map[string]pubsub.Publisher
// BeforeShutdown is an optional callback function that is called
// before the server is closed.
BeforeShutdown func()
// AfterShutdown is an optional callback function that is called
// after the server is closed.
AfterShutdown func()
}
type nopWriterCloser struct {
io.Writer
}
func (nopWriterCloser) Close() error { return nil }
func New(opts ...Option) (*Server, error) {
nworkers := Config.NumberOfWorkers
stdout, stderr := colorable.NewColorableStdout(), colorable.NewColorableStderr()
if !config.App.Color {
stdout = colorable.NewNonColorable(stdout)
stderr = colorable.NewNonColorable(stderr)
}
options := Options{
stdout: nopWriterCloser{stdout},
stderr: nopWriterCloser{stderr},
numworkers: nworkers,
jobQueueName: Config.ClientJobQueueName,
containerBuildDirectory: DefaultContainerBuildDirectory,
containerSourceDirectory: DefaultContainerSourceDirectory,
clientUploadBucketName: Config.ClientUploadBucketName,
clientUploadDestinationDirectory: Config.ClientUploadDestinationDirectory,
clientAppName: Config.ClientAppName,
context: context.Background(),
timelimit: Config.ClientJobTimeLimit,
}
for _, o := range opts {
o(&options)
}
id := uuid.NewV4()
// Create an AWS session
awsSession, err := aws.NewSession(
aws.Region(aws.AWSRegionUSEast1),
aws.AccessKey(aws.Config.AccessKey),
aws.SecretKey(aws.Config.SecretKey),
)
if err != nil {
return nil, err
}
go docker.PeriodicCleanupDeadContainers()
return &Server{
ID: id,
isConnected: false,
options: options,
awsSession: awsSession,
serializer: json.New(),
}, nil
}
func (s *Server) jobHandler(pub broker.Publication) error {
jobRequest := new(model.JobRequest)
msg := pub.Message()
if msg == nil {
return errors.New("recieved a nil message")
}
body := msg.Body
err := s.serializer.Unmarshal(body, jobRequest)
if err != nil {
log.WithError(err).WithField("id", msg.ID).Error("failed to parse job request")
return err
}
if jobRequest.PushQ() {
buildImage := jobRequest.BuildSpecification.Commands.BuildImage
push := buildImage.Push
if push.ImageName == "" {
push.ImageName = buildImage.ImageName
}
}
work, err := NewWorkerRequest(jobRequest, s.options)
if err != nil {
return err
}
s.dispatcher.workQueue <- work
return nil
}
func (s *Server) publishSubscribe() error {
log.Debug("Server subscribed to ", s.options.jobQueueName, " queue")
brkr, err := sqs.New(
sqs.QueueName(s.options.jobQueueName),
broker.Serializer(json.New()),
sqs.Session(s.awsSession),
)
if err != nil {
return err
}
subscriber, err := brkr.Subscribe(
"rai",
s.jobHandler,
broker.AutoAck(true),
)
if err != nil {
return err
}
s.jobSubscriber = subscriber
s.broker = brkr
return nil
}
func (s *Server) Connect() error {
s.dispatcher = StartDispatcher(s.options.numworkers)
if err := s.publishSubscribe(); err != nil {
return err
}
if err := s.broker.Connect(); err != nil {
return err
}
s.isConnected = true
return nil
}
func (s *Server) Disconnect() error {
if !s.isConnected {
return nil
}
log.Debug("shutting down server")
defer func() {
if s.AfterShutdown != nil {
s.AfterShutdown()
}
s.isConnected = false
}()
if s.BeforeShutdown != nil {
s.BeforeShutdown()
}
for k, pub := range s.publishers {
pub.End(k)
}
s.jobSubscriber.Unsubscribe()
return s.broker.Disconnect()
}
func (s *Server) Close() error {
return s.Disconnect()
}