di_crontab.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package dep
  2. import (
  3. "kpt-pasture/config"
  4. "kpt-pasture/module/crontab"
  5. "go.uber.org/dig"
  6. "gitee.com/xuyiping_admin/pkg/cron"
  7. prom "github.com/prometheus/client_golang/prometheus"
  8. )
  9. var DataCenterCrontabCounterVec = prom.NewCounterVec(
  10. prom.CounterOpts{
  11. Namespace: "kpt-pasture",
  12. Subsystem: "crontab",
  13. Name: "crontab",
  14. },
  15. []string{"name"},
  16. )
  17. func DICrontabService() (out *cron.Crontab) {
  18. container := DI()
  19. if err := container.Provide(EntryCrontab); err != nil {
  20. panic(err)
  21. }
  22. if err := container.Invoke(func(c *cron.Crontab) { out = c }); err != nil {
  23. panic(err)
  24. }
  25. return
  26. }
  27. // CrontabDependency 依赖注入结构体
  28. type CrontabDependency struct {
  29. dig.In
  30. CrontabHub crontab.Crontab // 定时任务
  31. }
  32. func EntryCrontab(dependency CrontabDependency) *cron.Crontab {
  33. cfg := config.Options()
  34. cs := cfg.CronSetting
  35. newCrontab := cron.NewCrontab(DataCenterCrontabCounterVec)
  36. err := newCrontab.Bind("UpdateCowInfo", cs.UpdateCowInfo, dependency.CrontabHub.UpdateCowInfo)
  37. if err != nil {
  38. panic(err)
  39. }
  40. err = newCrontab.Bind("GenerateWorkOrder", cs.GenerateWorkOrder, dependency.CrontabHub.GenerateAsynqWorkOrder)
  41. if err != nil {
  42. panic(err)
  43. }
  44. err = newCrontab.Bind("ImmunizationPlan", cs.ImmunizationPlan, dependency.CrontabHub.ImmunizationPlan)
  45. if err != nil {
  46. panic(err)
  47. }
  48. err = newCrontab.Bind("SameTimePlan", cs.SameTimePlan, dependency.CrontabHub.SameTimePlan)
  49. if err != nil {
  50. panic(err)
  51. }
  52. return newCrontab
  53. }