woj-server/internal/app/runner/runner.go

50 lines
1.2 KiB
Go
Raw Normal View History

package runner
import (
"fmt"
2023-07-14 21:47:11 +08:00
"git.0x7f.app/WOJ/woj-server/internal/api/runner"
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"
2024-01-07 00:39:33 +08:00
"git.0x7f.app/WOJ/woj-server/pkg/utils"
2023-07-14 21:47:11 +08:00
"git.0x7f.app/WOJ/woj-server/pkg/zapasynq"
"github.com/hibiken/asynq"
2023-07-15 16:19:49 +08:00
"github.com/samber/do"
"go.uber.org/zap"
2024-01-07 00:39:33 +08:00
"runtime"
)
2023-07-15 16:19:49 +08:00
func RunRunner(i *do.Injector) error {
conf := do.MustInvoke[config.Service](i).GetConfig()
rlog := do.MustInvoke[log.Service](i).GetLogger("app.runner")
hnd, err := runner.NewRunner(i)
if err != nil {
return err
}
mux := asynq.NewServeMux()
mux.HandleFunc(model.TypeProblemBuild, hnd.Build)
mux.HandleFunc(model.TypeSubmitJudge, hnd.Judge)
srv := asynq.NewServer(
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,
},
asynq.Config{
2024-01-07 00:39:33 +08:00
Concurrency: utils.If(runtime.NumCPU() > 1, runtime.NumCPU()-1, 1),
2023-07-15 16:19:49 +08:00
Logger: zapasynq.New(rlog),
Queues: map[string]int{model.QueueRunner: 1},
},
)
if err := srv.Run(mux); err != nil {
2023-07-15 16:19:49 +08:00
rlog.Warn("could not run server", zap.Error(err))
return err
}
return nil
}