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.

main.go 3.9 kB

2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "gitlink.org.cn/cloudream/common/pkgs/logger"
  6. stgglb "gitlink.org.cn/cloudream/storage/common/globals"
  7. "gitlink.org.cn/cloudream/storage/common/pkgs/db"
  8. "gitlink.org.cn/cloudream/storage/common/pkgs/distlock"
  9. agtrpc "gitlink.org.cn/cloudream/storage/common/pkgs/grpc/agent"
  10. scmq "gitlink.org.cn/cloudream/storage/common/pkgs/mq/scanner"
  11. "gitlink.org.cn/cloudream/storage/scanner/internal/config"
  12. "gitlink.org.cn/cloudream/storage/scanner/internal/event"
  13. "gitlink.org.cn/cloudream/storage/scanner/internal/mq"
  14. "gitlink.org.cn/cloudream/storage/scanner/internal/tickevent"
  15. )
  16. func main() {
  17. err := config.Init()
  18. if err != nil {
  19. fmt.Printf("init config failed, err: %s", err.Error())
  20. os.Exit(1)
  21. }
  22. err = logger.Init(&config.Cfg().Logger)
  23. if err != nil {
  24. fmt.Printf("init logger failed, err: %s", err.Error())
  25. os.Exit(1)
  26. }
  27. db, err := db.NewDB(&config.Cfg().DB)
  28. if err != nil {
  29. logger.Fatalf("new db failed, err: %s", err.Error())
  30. }
  31. stgglb.InitMQPool(&config.Cfg().RabbitMQ)
  32. stgglb.InitAgentRPCPool(&agtrpc.PoolConfig{})
  33. distlockSvc, err := distlock.NewService(&config.Cfg().DistLock)
  34. if err != nil {
  35. logger.Warnf("new distlock service failed, err: %s", err.Error())
  36. os.Exit(1)
  37. }
  38. go serveDistLock(distlockSvc)
  39. eventExecutor := event.NewExecutor(db, distlockSvc)
  40. go serveEventExecutor(&eventExecutor)
  41. agtSvr, err := scmq.NewServer(mq.NewService(&eventExecutor), &config.Cfg().RabbitMQ)
  42. if err != nil {
  43. logger.Fatalf("new agent server failed, err: %s", err.Error())
  44. }
  45. agtSvr.OnError(func(err error) {
  46. logger.Warnf("agent server err: %s", err.Error())
  47. })
  48. go serveScannerServer(agtSvr)
  49. tickExecutor := tickevent.NewExecutor(tickevent.ExecuteArgs{
  50. EventExecutor: &eventExecutor,
  51. DB: db,
  52. })
  53. startTickEvent(&tickExecutor)
  54. forever := make(chan struct{})
  55. <-forever
  56. }
  57. func serveEventExecutor(executor *event.Executor) {
  58. logger.Info("start serving event executor")
  59. err := executor.Execute()
  60. if err != nil {
  61. logger.Errorf("event executor stopped with error: %s", err.Error())
  62. }
  63. logger.Info("event executor stopped")
  64. // TODO 仅简单结束了程序
  65. os.Exit(1)
  66. }
  67. func serveScannerServer(server *scmq.Server) {
  68. logger.Info("start serving scanner server")
  69. err := server.Serve()
  70. if err != nil {
  71. logger.Errorf("scanner server stopped with error: %s", err.Error())
  72. }
  73. logger.Info("scanner server stopped")
  74. // TODO 仅简单结束了程序
  75. os.Exit(1)
  76. }
  77. func serveDistLock(svc *distlock.Service) {
  78. logger.Info("start serving distlock")
  79. err := svc.Serve()
  80. if err != nil {
  81. logger.Errorf("distlock stopped with error: %s", err.Error())
  82. }
  83. logger.Info("distlock stopped")
  84. // TODO 仅简单结束了程序
  85. os.Exit(1)
  86. }
  87. func startTickEvent(tickExecutor *tickevent.Executor) {
  88. // TODO 可以考虑增加配置文件,配置这些任务间隔时间
  89. interval := 5 * 60 * 1000
  90. tickExecutor.Start(tickevent.NewBatchAllAgentCheckCache(), interval, tickevent.StartOption{RandomStartDelayMs: 60 * 1000})
  91. tickExecutor.Start(tickevent.NewBatchCheckAllPackage(), interval, tickevent.StartOption{RandomStartDelayMs: 60 * 1000})
  92. // tickExecutor.Start(tickevent.NewBatchCheckAllRepCount(), interval, tickevent.StartOption{RandomStartDelayMs: 60 * 1000})
  93. tickExecutor.Start(tickevent.NewBatchCheckAllStorage(), interval, tickevent.StartOption{RandomStartDelayMs: 60 * 1000})
  94. tickExecutor.Start(tickevent.NewCheckAgentState(), 5*60*1000, tickevent.StartOption{RandomStartDelayMs: 60 * 1000})
  95. tickExecutor.Start(tickevent.NewBatchCheckPackageRedundancy(), interval, tickevent.StartOption{RandomStartDelayMs: 20 * 60 * 1000})
  96. tickExecutor.Start(tickevent.NewBatchCleanPinned(), interval, tickevent.StartOption{RandomStartDelayMs: 20 * 60 * 1000})
  97. tickExecutor.Start(tickevent.NewUpdateAllPackageAccessStatAmount(), interval, tickevent.StartOption{RandomStartDelayMs: 20 * 60 * 1000})
  98. }

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