job.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 main
  15. import (
  16. "flag"
  17. "fmt"
  18. "github.com/zeromicro/go-zero/core/conf"
  19. "github.com/zeromicro/go-zero/core/logx"
  20. "github.com/zeromicro/go-zero/core/service"
  21. "github.com/zeromicro/go-zero/zrpc"
  22. "google.golang.org/grpc"
  23. "google.golang.org/grpc/reflection"
  24. "github.com/suyuan32/simple-admin-job/internal/config"
  25. "github.com/suyuan32/simple-admin-job/internal/mqs/amq/task/dynamicperiodictask"
  26. "github.com/suyuan32/simple-admin-job/internal/mqs/amq/task/mqtask"
  27. "github.com/suyuan32/simple-admin-job/internal/mqs/amq/task/scheduletask"
  28. "github.com/suyuan32/simple-admin-job/internal/server"
  29. "github.com/suyuan32/simple-admin-job/internal/svc"
  30. "github.com/suyuan32/simple-admin-job/types/job"
  31. )
  32. var configFile = flag.String("f", "etc/job.yaml", "the config file")
  33. func main() {
  34. flag.Parse()
  35. var c config.Config
  36. conf.MustLoad(*configFile, &c)
  37. ctx := svc.NewServiceContext(c)
  38. s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
  39. job.RegisterJobServer(grpcServer, server.NewJobServer(ctx))
  40. if c.Mode == service.DevMode || c.Mode == service.TestMode {
  41. reflection.Register(grpcServer)
  42. }
  43. })
  44. go func() {
  45. s.Start()
  46. }()
  47. serviceGroup := service.NewServiceGroup()
  48. defer func() {
  49. serviceGroup.Stop()
  50. logx.Close()
  51. }()
  52. serviceGroup.Add(mqtask.NewMQTask(ctx))
  53. if c.TaskConf.EnableDPTask {
  54. serviceGroup.Add(dynamicperiodictask.NewDPTask(ctx))
  55. }
  56. if c.TaskConf.EnableScheduledTask {
  57. serviceGroup.Add(scheduletask.NewSchedulerTask(ctx))
  58. }
  59. fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
  60. serviceGroup.Start()
  61. }