dynamic_periodic_task.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 dynamicperiodictask
  15. import (
  16. "fmt"
  17. "log"
  18. "github.com/suyuan32/simple-admin-job/internal/svc"
  19. )
  20. type DPTask struct {
  21. svcCtx *svc.ServiceContext
  22. }
  23. func NewDPTask(svcCtx *svc.ServiceContext) *DPTask {
  24. return &DPTask{
  25. svcCtx: svcCtx,
  26. }
  27. }
  28. // Start starts the server.
  29. func (m *DPTask) Start() {
  30. if err := m.svcCtx.AsynqPTM.Run(); err != nil {
  31. log.Fatal(fmt.Errorf("failed to start dptask server, error: %v", err))
  32. }
  33. }
  34. // Stop stops the server.
  35. func (m *DPTask) Stop() {
  36. defer func() {
  37. if recover() != nil {
  38. log.Println("DPTask shuts down successfully")
  39. }
  40. }()
  41. m.svcCtx.AsynqPTM.Shutdown()
  42. }