woj-server/internal/service/task/service.go

61 lines
1.5 KiB
Go
Raw Normal View History

2022-10-04 18:47:50 +08:00
package task
import (
2023-07-15 16:19:49 +08:00
"errors"
"fmt"
2023-07-14 21:47:11 +08:00
"git.0x7f.app/WOJ/woj-server/internal/e"
2023-07-15 16:19:49 +08:00
"git.0x7f.app/WOJ/woj-server/internal/misc/config"
"git.0x7f.app/WOJ/woj-server/internal/misc/log"
2023-07-14 21:47:11 +08:00
"git.0x7f.app/WOJ/woj-server/internal/model"
"git.0x7f.app/WOJ/woj-server/internal/service/runner"
2022-10-04 18:47:50 +08:00
"github.com/hibiken/asynq"
2023-07-15 16:19:49 +08:00
"github.com/samber/do"
2022-10-04 18:47:50 +08:00
"go.uber.org/zap"
)
var _ Service = (*service)(nil)
type Service interface {
ProblemBuild(data *model.ProblemBuildPayload) (string, e.Status)
ProblemUpdate(data *model.ProblemUpdatePayload) (string, e.Status)
SubmitJudge(data *model.SubmitJudgePayload) (string, e.Status)
SubmitUpdate(data *model.SubmitUpdatePayload, ctx runner.JudgeStatus) (string, e.Status)
GetTaskInfo(string, string) (*asynq.TaskInfo, e.Status)
2023-07-15 16:19:49 +08:00
HealthCheck() error
}
func NewService(i *do.Injector) (Service, error) {
conf := do.MustInvoke[config.Service](i).GetConfig()
redisOpt := asynq.RedisClientOpt{
Addr: fmt.Sprintf("%s:%d", conf.Redis.Address, conf.Redis.Port),
2023-07-15 16:19:49 +08:00
Password: conf.Redis.Password,
DB: conf.Redis.QueueDb,
}
return &service{
log: do.MustInvoke[log.Service](i).GetLogger("task"),
queue: asynq.NewClient(redisOpt),
inspector: asynq.NewInspector(redisOpt),
}, nil
2022-10-04 18:47:50 +08:00
}
type service struct {
log *zap.Logger
queue *asynq.Client
inspector *asynq.Inspector
}
2023-07-15 16:19:49 +08:00
func (s *service) HealthCheck() error {
servers, err := s.inspector.Servers()
if err != nil {
return err
2022-10-04 18:47:50 +08:00
}
2023-07-15 16:19:49 +08:00
if len(servers) == 0 {
return errors.New("no asynq server found")
2022-10-04 18:47:50 +08:00
}
2023-07-15 16:19:49 +08:00
return nil
2022-10-04 18:47:50 +08:00
}