service_context.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2023 The Ryan SU Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package svc
  15. import (
  16. "github.com/hibiken/asynq"
  17. "github.com/redis/go-redis/v9"
  18. "github.com/zeromicro/go-zero/core/logx"
  19. "github.com/suyuan32/simple-admin-job/ent"
  20. "github.com/suyuan32/simple-admin-job/internal/config"
  21. "github.com/suyuan32/simple-admin-job/internal/mqs/amq/types/periodicconfig"
  22. )
  23. type ServiceContext struct {
  24. Config config.Config
  25. DB *ent.Client
  26. Redis redis.UniversalClient
  27. AsynqServer *asynq.Server
  28. AsynqScheduler *asynq.Scheduler
  29. AsynqPTM *asynq.PeriodicTaskManager
  30. }
  31. func NewServiceContext(c config.Config) *ServiceContext {
  32. db := ent.NewClient(
  33. ent.Log(logx.Info), // logger
  34. ent.Driver(c.DatabaseConf.NewNoCacheDriver()),
  35. ent.Debug(), // debug mode
  36. )
  37. return &ServiceContext{
  38. Config: c,
  39. DB: db,
  40. AsynqServer: c.AsynqConf.WithOriginalRedisConf(c.RedisConf).NewServer(),
  41. AsynqScheduler: c.AsynqConf.NewScheduler(),
  42. AsynqPTM: c.AsynqConf.NewPeriodicTaskManager(periodicconfig.NewEntConfigProvider(db)),
  43. Redis: c.RedisConf.MustNewUniversalRedis(),
  44. }
  45. }