cloudevent_sync_task.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 tasks
  15. import (
  16. "context"
  17. "time"
  18. "github.com/pkg/errors"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/log"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  24. "yunion.io/x/onecloud/pkg/cloudevent/models"
  25. "yunion.io/x/onecloud/pkg/cloudevent/options"
  26. )
  27. type CloudeventSyncTask struct {
  28. taskman.STask
  29. }
  30. func init() {
  31. taskman.RegisterTask(CloudeventSyncTask{})
  32. }
  33. func (self *CloudeventSyncTask) taskFailed(ctx context.Context, cloudprovider *models.SCloudprovider, err jsonutils.JSONObject) {
  34. cloudprovider.MarkEndSync(self.UserCred)
  35. self.SetStageFailed(ctx, err)
  36. }
  37. func (self *CloudeventSyncTask) taskComplete(ctx context.Context, cloudprovider *models.SCloudprovider) {
  38. cloudprovider.MarkEndSync(self.UserCred)
  39. self.SetStageComplete(ctx, nil)
  40. }
  41. func (self *CloudeventSyncTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  42. provider := obj.(*models.SCloudprovider)
  43. factory, err := provider.GetProviderFactory()
  44. if err != nil {
  45. self.taskFailed(ctx, provider, jsonutils.NewString(errors.Wrap(err, "cloudprovider.GetProviderFactory").Error()))
  46. return
  47. }
  48. iProvider, err := provider.GetProvider()
  49. if err != nil {
  50. self.taskFailed(ctx, provider, jsonutils.NewString(errors.Wrap(err, "cloudprovider.GetProvider").Error()))
  51. return
  52. }
  53. start, end, err := provider.GetNextTimeRange()
  54. if err != nil {
  55. self.taskFailed(ctx, provider, jsonutils.NewString(errors.Wrap(err, "provider.GetNextTimeRange").Error()))
  56. return
  57. }
  58. //小于1小时的暂时不同步
  59. duration := end.Sub(start) + time.Second
  60. if duration < time.Hour {
  61. self.taskComplete(ctx, provider)
  62. return
  63. }
  64. count := 0
  65. for {
  66. events := []cloudprovider.ICloudEvent{}
  67. regions, _ := iProvider.GetIRegions()
  68. for i := range regions {
  69. if factory.IsCloudeventRegional() || i == 0 {
  70. _events, err := regions[i].GetICloudEvents(start, end, options.Options.SyncWithReadEvent)
  71. if err != nil {
  72. if err == cloudprovider.ErrNotSupported {
  73. continue
  74. }
  75. self.taskFailed(ctx, provider, jsonutils.NewString(errors.Wrapf(err, "regions[%d].GetICloudEvents", i).Error()))
  76. return
  77. }
  78. events = append(events, _events...)
  79. }
  80. }
  81. _count := models.CloudeventManager.SyncCloudevent(ctx, self.UserCred, provider, events)
  82. log.Infof("Sync %d events for %s(%s) from %s(%d) hours", _count, provider.Name, provider.Id, start.Format("2006-01-02T15:04:05Z"), duration/time.Hour)
  83. count += _count
  84. provider.SetLastSyncTimeAt(self.UserCred, end)
  85. if time.Now().Sub(end) < duration {
  86. break
  87. }
  88. start = start.Add(duration)
  89. end = end.Add(duration)
  90. }
  91. self.taskComplete(ctx, provider)
  92. }