service.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2019 Yunion
  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 service
  15. import (
  16. "os"
  17. "yunion.io/x/log"
  18. _ "yunion.io/x/sqlchemy/backends"
  19. api "yunion.io/x/onecloud/pkg/apis/devtool"
  20. "yunion.io/x/onecloud/pkg/cloudcommon"
  21. app_common "yunion.io/x/onecloud/pkg/cloudcommon/app"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. common_options "yunion.io/x/onecloud/pkg/cloudcommon/options"
  24. "yunion.io/x/onecloud/pkg/devtool/models"
  25. "yunion.io/x/onecloud/pkg/devtool/options"
  26. _ "yunion.io/x/onecloud/pkg/devtool/tasks"
  27. )
  28. // StartService the main service starts
  29. func StartService() {
  30. opts := &options.Options
  31. commonOpts := &opts.CommonOptions
  32. dbOpts := &options.Options.DBOptions
  33. baseOpts := &opts.BaseOptions
  34. common_options.ParseOptions(opts, os.Args, "devtool.conf", api.SERVICE_TYPE)
  35. app_common.InitAuth(commonOpts, func() {
  36. log.Infof("Auth complete!!")
  37. })
  38. app := app_common.InitApp(&opts.BaseOptions, false)
  39. cloudcommon.InitDB(dbOpts)
  40. InitHandlers(app, opts.IsSlaveNode)
  41. db.EnsureAppSyncDB(app, dbOpts, models.InitDB)
  42. defer cloudcommon.CloseDB()
  43. if !opts.IsSlaveNode {
  44. models.InitializeCronjobs(app.GetContext())
  45. }
  46. app_common.ServeForeverWithCleanup(app, baseOpts, func() {
  47. cloudcommon.CloseDB()
  48. })
  49. }