service.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. "time"
  18. "yunion.io/x/log"
  19. _ "yunion.io/x/sqlchemy/backends"
  20. api "yunion.io/x/onecloud/pkg/apis/scheduledtask"
  21. "yunion.io/x/onecloud/pkg/cloudcommon"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/app"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/cronman"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  25. common_options "yunion.io/x/onecloud/pkg/cloudcommon/options"
  26. "yunion.io/x/onecloud/pkg/scheduledtask/models"
  27. "yunion.io/x/onecloud/pkg/scheduledtask/options"
  28. )
  29. func StartService() {
  30. opts := &options.Options
  31. commonOpts := &options.Options.CommonOptions
  32. dbOpts := &options.Options.DBOptions
  33. baseOpts := &options.Options.BaseOptions
  34. common_options.ParseOptions(opts, os.Args, "scheduledtask.conf", api.SERVICE_TYPE)
  35. app.InitAuth(commonOpts, func() {
  36. log.Infof("Auth complete!")
  37. })
  38. applicaion := app.InitApp(baseOpts, true)
  39. cloudcommon.InitDB(dbOpts)
  40. InitHandlers(applicaion, opts.IsSlaveNode)
  41. db.EnsureAppSyncDB(applicaion, dbOpts, nil)
  42. defer cloudcommon.CloseDB()
  43. if !opts.IsSlaveNode {
  44. cron := cronman.InitCronJobManager(true, 4, opts.TimeZone)
  45. cron.AddJobAtIntervalsWithStartRun("ScheduledTaskCheck", time.Duration(60)*time.Second, models.ScheduledTaskManager.Timer, true)
  46. cron.AddJobEveryFewHour("AutoPurgeSplitable", 4, 30, 0, db.AutoPurgeSplitable, false)
  47. go cron.Start()
  48. }
  49. app.ServeForever(applicaion, baseOpts)
  50. }