main.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2020 gorse Project Authors
  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. "fmt"
  17. "os"
  18. "os/signal"
  19. "github.com/spf13/cobra"
  20. "github.com/zhenghaoz/gorse/base/log"
  21. "github.com/zhenghaoz/gorse/cmd/version"
  22. "github.com/zhenghaoz/gorse/config"
  23. "github.com/zhenghaoz/gorse/master"
  24. "go.uber.org/zap"
  25. )
  26. var masterCommand = &cobra.Command{
  27. Use: "gorse-master",
  28. Short: "The master node of gorse recommender system.",
  29. Run: func(cmd *cobra.Command, args []string) {
  30. // Show version
  31. if showVersion, _ := cmd.PersistentFlags().GetBool("version"); showVersion {
  32. fmt.Println(version.BuildInfo())
  33. return
  34. }
  35. // setup logger
  36. debug, _ := cmd.PersistentFlags().GetBool("debug")
  37. log.SetLogger(cmd.PersistentFlags(), debug)
  38. // Create master
  39. configPath, _ := cmd.PersistentFlags().GetString("config")
  40. log.Logger().Info("load config", zap.String("config", configPath))
  41. conf, err := config.LoadConfig(configPath, false)
  42. if err != nil {
  43. log.Logger().Fatal("failed to load config", zap.Error(err))
  44. }
  45. cachePath, _ := cmd.PersistentFlags().GetString("cache-path")
  46. managedMode, _ := cmd.PersistentFlags().GetBool("managed")
  47. m := master.NewMaster(conf, cachePath, managedMode)
  48. // Stop master
  49. done := make(chan struct{})
  50. go func() {
  51. sigint := make(chan os.Signal, 1)
  52. signal.Notify(sigint, os.Interrupt)
  53. <-sigint
  54. m.Shutdown()
  55. close(done)
  56. }()
  57. // Start master
  58. m.Serve()
  59. <-done
  60. log.Logger().Info("stop gorse master successfully")
  61. },
  62. }
  63. func init() {
  64. log.AddFlags(masterCommand.PersistentFlags())
  65. masterCommand.PersistentFlags().Bool("debug", false, "use debug log mode")
  66. masterCommand.PersistentFlags().Bool("managed", false, "enable managed mode")
  67. masterCommand.PersistentFlags().StringP("config", "c", "", "configuration file path")
  68. masterCommand.PersistentFlags().BoolP("version", "v", false, "gorse version")
  69. masterCommand.PersistentFlags().String("cache-path", "master_cache.data", "path of cache file")
  70. }
  71. func main() {
  72. if err := masterCommand.Execute(); err != nil {
  73. log.Logger().Fatal("failed to execute", zap.Error(err))
  74. }
  75. }