-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathworker.go
374 lines (342 loc) · 10.3 KB
/
worker.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package main
import (
"ai-developer/app/client"
gitness_git_provider "ai-developer/app/client/git_provider"
"ai-developer/app/client/workspace"
"ai-developer/app/config"
"ai-developer/app/constants"
"ai-developer/app/monitoring"
"ai-developer/app/repositories"
"ai-developer/app/services"
"ai-developer/app/services/filestore"
"ai-developer/app/services/filestore/impl"
"ai-developer/app/services/git_providers"
"ai-developer/app/tasks"
"context"
"fmt"
"log"
"net/http"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/hibiken/asynq"
"github.com/knadh/koanf/v2"
"go.uber.org/dig"
"go.uber.org/zap"
"gorm.io/gorm"
)
func main() {
config.InitLogger()
c := dig.New()
appConfig, err := config.LoadConfig()
if err != nil {
panic(err)
}
_ = c.Provide(func() *zap.Logger {
return config.Logger
})
_ = c.Provide(func() *koanf.Koanf {
return appConfig
})
_ = c.Provide(func() *http.Client {
return &http.Client{}
})
_ = c.Provide(client.NewHttpClient)
//Provide Context
_ = c.Provide(func() context.Context {
return context.Background()
})
// Provide Redis Client
err = c.Provide(config.InitRedis)
if err != nil {
log.Println("Error providing Redis client:", err)
panic(err)
}
fmt.Println("Now Redis related repositories....")
// Provide GORM DB instance
err = c.Provide(func() *gorm.DB {
return config.InitDB()
})
if err != nil {
panic(err)
}
if err = c.Provide(config.NewWorkspaceServiceConfig); err != nil {
config.Logger.Error("Error providing workspace service config", zap.Error(err))
panic(err)
}
if err = c.Provide(config.NewAWSConfig); err != nil {
config.Logger.Error("Error providing AWS config", zap.Error(err))
panic(err)
}
if err = c.Provide(config.NewFileStoreConfig); err != nil {
config.Logger.Error("Error providing FileStore config", zap.Error(err))
panic(err)
}
if err = c.Provide(config.NewLocalFileStoreConfig); err != nil {
config.Logger.Error("Error providing FileStore config", zap.Error(err))
panic(err)
}
if err = c.Provide(config.NewS3FileStoreConfig); err != nil {
config.Logger.Error("Error providing FileStore config", zap.Error(err))
panic(err)
}
if err = c.Provide(config.NewAwsSession); err != nil {
config.Logger.Error("Error providing FileStore config", zap.Error(err))
panic(err)
}
if err = c.Provide(func(
awsConfig *config.AWSConfig,
storeConfig *config.FileStoreConfig,
localFileStoreConfig *config.LocalFileStoreConfig,
s3fileStoreConfig *config.S3FileStoreConfig,
awsSession *session.Session,
logger *zap.Logger,
) filestore.FileStore {
if storeConfig.GetFileStoreType() == constants.LOCAL {
config.Logger.Info("Using local file store")
lfs := impl.NewLocalFileStore(localFileStoreConfig, logger)
return lfs
} else {
config.Logger.Info("Using s3 file store")
s3fs := impl.NewS3FileSystem(awsSession, s3fileStoreConfig, logger)
return s3fs
}
}); err != nil {
config.Logger.Error("Error providing FileStore", zap.Error(err))
panic(err)
}
//Provide Repositories
err = c.Provide(repositories.NewExecutionRepository)
if err != nil {
log.Println("Error providing execution repository:", err)
panic(err)
}
//executionOutputRepo *repositories.ExecutionOutputRepository,
err = c.Provide(repositories.NewExecutionOutputRepository)
if err != nil {
log.Println("Error providing execution output repository:", err)
panic(err)
}
//pullRequestRepo *repositories.PullRequestRepository,
err = c.Provide(repositories.NewPullRequestRepository)
if err != nil {
log.Println("Error providing pull request repository:", err)
panic(err)
}
//ActivityLogRepository
err = c.Provide(repositories.NewActivityLogRepository)
if err != nil {
log.Println("Error providing activity log repository:", err)
panic(err)
}
//ProjectRepository
err = c.Provide(repositories.NewProjectRepository)
if err != nil {
log.Println("Error providing project repository:", err)
panic(err)
}
//OrganisationRepository
err = c.Provide(repositories.NewOrganisationRepository)
if err != nil {
log.Println("Error providing organisation repository:", err)
panic(err)
}
//StoryRepository
err = c.Provide(repositories.NewStoryRepository)
if err != nil {
log.Println("Error providing story repository:", err)
panic(err)
}
//Story Instructions Repository
err = c.Provide(repositories.NewStoryInstructionRepository)
if err != nil {
log.Println("Error providing story instruction repository:", err)
panic(err)
}
//StoryTestCaseRepository
err = c.Provide(repositories.NewStoryTestCaseRepository)
if err != nil {
log.Println("Error providing story test case repository:", err)
panic(err)
}
//StoryFileRepository
err = c.Provide(repositories.NewStoryFileRepository)
if err != nil {
log.Println("Error providing story file repository:", err)
panic(err)
}
//ExecutionStepRepository
err = c.Provide(repositories.NewExecutionStepRepository)
if err != nil {
log.Println("Error providing execution step repository:", err)
panic(err)
}
//NewProjectConnectionsRepository
err = c.Provide(repositories.NewProjectConnectionsRepository)
if err != nil {
log.Println("Error providing project connections repository:", err)
panic(err)
}
//Pull Request Repository
err = c.Provide(repositories.NewPullRequestCommentsRepository)
if err != nil {
log.Println("Error providing pull request comments repository:", err)
panic(err)
}
//NewLLMAPIKeyRepository
err = c.Provide(repositories.NewLLMAPIKeyRepository)
if err != nil {
log.Println("Error providing LLM API Key repository:", err)
panic(err)
}
fmt.Printf("Worker - Providing workspace service client...")
err = c.Provide(workspace.NewWorkspaceServiceClient)
if err != nil {
log.Println("Error providing workspace service:", err)
panic(err)
}
err = c.Provide(services.NewActivityLogService)
if err != nil {
log.Println("Error providing activity log service:", err)
panic(err)
}
err = c.Provide(services.NewStoryService)
if err != nil {
log.Println("Error providing story service:", err)
panic(err)
}
err = c.Provide(services.NewExecutionService)
if err != nil {
log.Println("Error providing execution service:", err)
panic(err)
}
err = c.Provide(services.NewProjectService)
if err != nil {
log.Println("Error providing project service:", err)
panic(err)
}
err = c.Provide(services.NewExecutionStepService)
if err != nil {
log.Println("Error providing execution step service:", err)
panic(err)
}
err = c.Provide(services.NewExecutionOutputService)
if err != nil {
fmt.Println("Error providing ExecutionOutputService: ", err)
panic(err)
}
err = c.Provide(services.NewPullRequestService)
if err != nil {
fmt.Printf("Error providing PullRequestService: %v\n", err)
panic(err)
}
//NewLLMAPIKeyService
err = c.Provide(services.NewLLMAPIKeyService)
if err != nil {
fmt.Println("Error providing LLM API Key Service: ", err)
panic(err)
}
// Provide GitnessClient
err = c.Provide(func(logger *zap.Logger, slackAlert *monitoring.SlackAlert) *gitness_git_provider.GitnessClient {
return gitness_git_provider.NewGitnessClient(config.GitnessURL(), config.GitnessToken(), client.NewHttpClient(), logger, slackAlert)
})
if err != nil {
panic(err)
}
// Provide GitnessService
err = c.Provide(func(client *gitness_git_provider.GitnessClient) *git_providers.GitnessService {
return git_providers.NewGitnessService(client)
})
err = c.Provide(services.NewProjectNotificationService)
if err != nil {
config.Logger.Error("Error providing ProjectNotificationService", zap.Error(err))
panic(err)
}
// Provide Asynq client
err = c.Provide(func() *asynq.Client {
return asynq.NewClient(asynq.RedisClientOpt{
Addr: config.RedisAddress(),
DB: config.RedisDB(),
})
})
if err != nil {
log.Fatalf("could not provide *asynq.Client: %v", err)
}
// Provide Slack Alert For monitoring
err = c.Provide(monitoring.NewSlackAlert)
if err != nil {
log.Println("Error providing slack alert:", err)
return
}
//Provide DeleteWorkspaceTaskHandler
err = c.Provide(tasks.NewDeleteWorkspaceTaskHandler)
if err != nil {
log.Fatalf("could not provide DeleteWorkspaceTaskHandler: %v", err)
}
//Provide CreateExecutionJobTaskHandler
err = c.Provide(tasks.NewCreateExecutionJobTaskHandler)
if err != nil {
log.Fatalf("could not provide CreateExecutionJobTaskHandler: %v", err)
}
err = c.Provide(tasks.NewCheckExecutionStatusTaskHandler)
if err != nil {
log.Fatalf("could not provide CheckExecutionStatusTaskHandler: %v", err)
}
//Provide asynq scheduler
err = c.Provide(func() *asynq.Scheduler {
return asynq.NewScheduler(asynq.RedisClientOpt{
Addr: config.RedisAddress(),
DB: config.RedisDB(),
}, nil)
})
err = c.Provide(func(
deleteWorkspaceTaskHandler *tasks.DeleteWorkspaceTaskHandler,
createExecutionJobTaskHandler *tasks.CreateExecutionJobTaskHandler,
checkExecutionStatusTaskHandler *tasks.CheckExecutionStatusTaskHandler,
workspaceServiceClient *workspace.WorkspaceServiceClient,
projectService *services.ProjectService,
logger *zap.Logger,
) *asynq.ServeMux {
mux := asynq.NewServeMux()
mux.HandleFunc(constants.DeleteWorkspaceTaskType, deleteWorkspaceTaskHandler.HandleTask)
mux.HandleFunc(constants.CreateExecutionJobTaskType, createExecutionJobTaskHandler.HandleTask)
mux.HandleFunc(constants.CheckExecutionStatusTaskType, checkExecutionStatusTaskHandler.HandleTask)
return mux
})
if err != nil {
log.Fatalf("could not provide *asynq.ServeMux: %v", err)
}
err = c.Provide(func() *asynq.Server {
return asynq.NewServer(
asynq.RedisClientOpt{
Addr: config.RedisAddress(),
DB: config.RedisDB(),
},
asynq.Config{
Concurrency: 10,
Queues: map[string]int{
"critical": 6,
"default": 3,
"low": 1,
},
StrictPriority: true,
},
)
})
err = c.Invoke(func(srv *asynq.Server, mux *asynq.ServeMux, scheduler *asynq.Scheduler,
) error {
task := asynq.NewTask(constants.CheckExecutionStatusTaskType, nil, asynq.TaskID(constants.CheckExecutionStatusTaskType))
if _, err := scheduler.Register("*/5 * * * *", task); err != nil {
log.Fatalf("could not schedule task: %v", err)
}
// Start the scheduler in a separate goroutine
go func() {
if err := scheduler.Run(); err != nil {
log.Fatalf("could not run scheduler: %v", err)
}
}()
// Start the server
return srv.Run(mux)
})
if err != nil {
log.Fatalf("could not run server: %v", err)
}
}