package server import ( "fmt" "log" "os" "sandc/app/eonline/internal/biz" "sandc/app/eonline/internal/conf" "github.com/hibiken/asynq" "golang.org/x/sync/errgroup" ) type AsynqServer struct { srv *asynq.Server mux *asynq.ServeMux scheduler *asynq.Scheduler entryID string } func (j *AsynqServer) Run() error { eg := errgroup.Group{} eg.Go(func() error { log.Println("schedule start", j.entryID) return j.scheduler.Run() }) eg.Go(func() error { return j.srv.Run(j.mux) }) return eg.Wait() } func NewAsynqServer(conf *conf.Queue, uc *biz.EonlineUsecase) *AsynqServer { asynqConf := conf.Asynq redisOpt := asynq.RedisClientOpt{ Addr: asynqConf.Addr, Password: asynqConf.Password, DB: int(asynqConf.Db), PoolSize: int(asynqConf.Pool), ReadTimeout: asynqConf.ReadTimeout.AsDuration(), WriteTimeout: asynqConf.WriteTimeout.AsDuration(), } srv := asynq.NewServer( redisOpt, asynq.Config{ Concurrency: int(asynqConf.Concurrency), Queues: map[string]int{ "critical": 6, "default": 3, "low": 1, }, }) mux := asynq.NewServeMux() mux.HandleFunc(biz.EonlineTaskName, uc.AsynqEonlineTaskHandler) scheduler := asynq.NewScheduler(redisOpt, &asynq.SchedulerOpts{}) task := asynq.NewTask(biz.EonlineTaskName, nil) entryID, err := scheduler.Register("*/15 * * * *", task, asynq.TaskID(biz.EonlineTaskName)) checkError(err) return &AsynqServer{srv: srv, mux: mux, scheduler: scheduler, entryID: entryID} } func checkError(err error) { if err != nil { fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error()) os.Exit(1) } }