You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

serve.go 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package cmd
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/spf13/cobra"
  6. "gitlink.org.cn/cloudream/common/pkgs/logger"
  7. stgglb "gitlink.org.cn/cloudream/jcs-pub/common/globals"
  8. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc"
  9. corrpc "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc/coordinator"
  10. hubrpc "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc/hub"
  11. "gitlink.org.cn/cloudream/jcs-pub/coordinator/internal/config"
  12. "gitlink.org.cn/cloudream/jcs-pub/coordinator/internal/db"
  13. "gitlink.org.cn/cloudream/jcs-pub/coordinator/internal/repl"
  14. myrpc "gitlink.org.cn/cloudream/jcs-pub/coordinator/internal/rpc"
  15. "gitlink.org.cn/cloudream/jcs-pub/coordinator/internal/ticktock"
  16. )
  17. func init() {
  18. var configPath string
  19. cmd := &cobra.Command{
  20. Use: "serve",
  21. Short: "Start coordinator server",
  22. Long: `Start coordinator server`,
  23. Run: func(cmd *cobra.Command, args []string) {
  24. serve(configPath)
  25. },
  26. }
  27. cmd.Flags().StringVarP(&configPath, "config", "c", "", "Path to config file")
  28. RootCmd.AddCommand(cmd)
  29. }
  30. func serve(configPath string) {
  31. err := config.Init(configPath)
  32. if err != nil {
  33. fmt.Printf("init config failed, err: %s", err.Error())
  34. os.Exit(1)
  35. }
  36. err = logger.Init(&config.Cfg().Logger)
  37. if err != nil {
  38. fmt.Printf("init logger failed, err: %s", err.Error())
  39. os.Exit(1)
  40. }
  41. stgglb.InitPools(&hubrpc.PoolConfig{}, nil)
  42. db2, err := db.NewDB(&config.Cfg().DB)
  43. if err != nil {
  44. logger.Fatalf("new db2 failed, err: %s", err.Error())
  45. }
  46. // 初始化系统事件发布器
  47. // evtPub, err := sysevent.NewPublisher(sysevent.ConfigFromMQConfig(config.Cfg().RabbitMQ), &cortypes.SourceCoordinator{})
  48. // if err != nil {
  49. // logger.Errorf("new sysevent publisher: %v", err)
  50. // os.Exit(1)
  51. // }
  52. // go servePublisher(evtPub)
  53. // RPC服务
  54. rpcSvr := corrpc.NewServer(config.Cfg().RPC, myrpc.NewService(db2))
  55. rpcSvrChan := rpcSvr.Start()
  56. defer rpcSvr.Stop()
  57. // 定时任务
  58. tktk := ticktock.New(config.Cfg().TickTock, db2)
  59. tktk.Start()
  60. defer tktk.Stop()
  61. // 交互式命令行
  62. rep := repl.New(db2, tktk)
  63. replCh := rep.Start()
  64. /// 开始监听各个模块的事件
  65. replEvt := replCh.Receive()
  66. rpcEvt := rpcSvrChan.Receive()
  67. loop:
  68. for {
  69. select {
  70. case e := <-replEvt.Chan():
  71. if e.Err != nil {
  72. logger.Errorf("receive repl event: %v", err)
  73. break loop
  74. }
  75. switch e.Value.(type) {
  76. case repl.ExitEvent:
  77. logger.Info("exit by repl")
  78. break loop
  79. }
  80. replEvt = replCh.Receive()
  81. case e := <-rpcEvt.Chan():
  82. if e.Err != nil {
  83. logger.Errorf("receive rpc event: %v", e.Err)
  84. break loop
  85. }
  86. switch e := e.Value.(type) {
  87. case rpc.ExitEvent:
  88. if e.Err != nil {
  89. logger.Errorf("rpc server exited with error: %v", e.Err)
  90. } else {
  91. logger.Infof("rpc server exited")
  92. }
  93. break loop
  94. }
  95. rpcEvt = rpcSvrChan.Receive()
  96. }
  97. }
  98. }
  99. // func servePublisher(evtPub *sysevent.Publisher) {
  100. // logger.Info("start serving sysevent publisher")
  101. // ch := evtPub.Start()
  102. // loop:
  103. // for {
  104. // val, err := ch.Receive().Wait(context.Background())
  105. // if err != nil {
  106. // logger.Errorf("sysevent publisher stopped with error: %s", err.Error())
  107. // break
  108. // }
  109. // switch val := val.(type) {
  110. // case sysevent.PublishError:
  111. // logger.Errorf("publishing event: %v", val)
  112. // case sysevent.PublisherExited:
  113. // if val.Err != nil {
  114. // logger.Errorf("publisher exited with error: %v", val.Err)
  115. // } else {
  116. // logger.Info("publisher exited")
  117. // }
  118. // break loop
  119. // case sysevent.OtherError:
  120. // logger.Errorf("sysevent: %v", val)
  121. // }
  122. // }
  123. // logger.Info("sysevent publisher stopped")
  124. // // TODO 仅简单结束了程序
  125. // os.Exit(1)
  126. // }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。