Files
Sipro-Marketplaces/internal/tasks/server/server.go

42 lines
1015 B
Go

package server
import (
"github.com/hibiken/asynq"
"github.com/jackc/pgx/v5/pgxpool"
"sipro-mps/internal/config"
"sipro-mps/internal/tasks/ozon"
"sipro-mps/internal/tasks/types"
"sipro-mps/internal/tasks/wb"
)
type AsynqServer struct {
redisConfig *config.RedisConfig
dbpool *pgxpool.Pool
}
func NewAsynqServer(redisConfig *config.RedisConfig, dbpool *pgxpool.Pool) *AsynqServer {
return &AsynqServer{
redisConfig: redisConfig,
dbpool: dbpool,
}
}
func (s *AsynqServer) createMux() *asynq.ServeMux {
mux := asynq.NewServeMux()
// Register task handlers here
mux.Handle(types.TypeWbFetchProducts, &wb.FetchProductsProcessor{Dbpool: s.dbpool})
mux.Handle(types.TypeOzonFetchProducts, &ozon.FetchProductsProcessor{Dbpool: s.dbpool})
return mux
}
func (s *AsynqServer) Run() {
srv := asynq.NewServer(
asynq.RedisClientOpt{Addr: s.redisConfig.Addr, Password: s.redisConfig.Password},
asynq.Config{},
)
mux := s.createMux()
if err := srv.Run(mux); err != nil {
panic(err)
}
}