service_context.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. WX_DB *ent.Client
  27. Redis redis.UniversalClient
  28. AsynqServer *asynq.Server
  29. AsynqScheduler *asynq.Scheduler
  30. AsynqPTM *asynq.PeriodicTaskManager
  31. }
  32. func NewServiceContext(c config.Config) *ServiceContext {
  33. db := ent.NewClient(
  34. ent.Log(logx.Info), // logger
  35. ent.Driver(c.DatabaseConf.NewNoCacheDriver()),
  36. ent.Debug(), // debug mode
  37. )
  38. wxdb := ent.NewClient(
  39. ent.Log(logx.Info), // logger
  40. ent.Driver(c.WXDatabaseConf.NewNoCacheDriver()),
  41. ent.Debug(), // debug mode
  42. )
  43. return &ServiceContext{
  44. Config: c,
  45. DB: db,
  46. WX_DB: wxdb,
  47. AsynqServer: c.AsynqConf.WithOriginalRedisConf(c.RedisConf).NewServer(),
  48. AsynqScheduler: c.AsynqConf.NewScheduler(),
  49. AsynqPTM: c.AsynqConf.NewPeriodicTaskManager(periodicconfig.NewEntConfigProvider(db)),
  50. Redis: c.RedisConf.MustNewUniversalRedis(),
  51. }
  52. }