diff --git a/pkg/apis/compute/cloudaccount.go b/pkg/apis/compute/cloudaccount.go index 78beee410c..db1eed25bd 100644 --- a/pkg/apis/compute/cloudaccount.go +++ b/pkg/apis/compute/cloudaccount.go @@ -469,3 +469,9 @@ type CloudaccountSyncSkusInput struct { CloudregionResourceInput CloudproviderResourceInput } + +type CloudaccountEnableAutoSyncInput struct { + // 云账号状态必须是connected + // 最小值为region服务的minimal_sync_interval_seconds + SyncIntervalSeconds int `json:"sync_interval_seconds"` +} diff --git a/pkg/compute/models/cloudaccounts.go b/pkg/compute/models/cloudaccounts.go index de3940792b..2db73917c1 100644 --- a/pkg/compute/models/cloudaccounts.go +++ b/pkg/compute/models/cloudaccounts.go @@ -1699,7 +1699,7 @@ func (self *SCloudaccount) AllowPerformEnableAutoSync(ctx context.Context, userC return db.IsAdminAllowPerform(userCred, self, "enable-auto-sync") } -func (self *SCloudaccount) PerformEnableAutoSync(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) { +func (self *SCloudaccount) PerformEnableAutoSync(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.CloudaccountEnableAutoSyncInput) (jsonutils.JSONObject, error) { if self.EnableAutoSync { return nil, nil } @@ -1708,10 +1708,11 @@ func (self *SCloudaccount) PerformEnableAutoSync(ctx context.Context, userCred m return nil, httperrors.NewInvalidStatusError("cannot enable auto sync in status %s", self.Status) } - syncIntervalSecs := int64(0) - syncIntervalSecs, _ = data.Int("sync_interval_seconds") + if input.SyncIntervalSeconds < options.Options.MinimalSyncIntervalSeconds { + return nil, httperrors.NewOutOfRangeError("sync_interval_seconds should be greater than %d", options.Options.MinimalSyncIntervalSeconds) + } - self.enableAutoSync(ctx, userCred, int(syncIntervalSecs)) + self.enableAutoSync(ctx, userCred, input.SyncIntervalSeconds) return nil, nil } @@ -1856,7 +1857,7 @@ func (manager *SCloudaccountManager) initAllRecords() { func (self *SCloudaccount) CanSync() bool { if self.SyncStatus == api.CLOUD_PROVIDER_SYNC_STATUS_QUEUED || self.SyncStatus == api.CLOUD_PROVIDER_SYNC_STATUS_SYNCING || self.getSyncStatus2() == api.CLOUD_PROVIDER_SYNC_STATUS_SYNCING { - if self.LastSync.IsZero() || time.Now().Sub(self.LastSync) > 1800*time.Second { + if self.LastSync.IsZero() || time.Now().Sub(self.LastSync) > time.Duration(self.getSyncIntervalSeconds()) { return true } else { return false diff --git a/pkg/compute/models/cloudproviderregions.go b/pkg/compute/models/cloudproviderregions.go index 41692209fc..e51a705bcd 100644 --- a/pkg/compute/models/cloudproviderregions.go +++ b/pkg/compute/models/cloudproviderregions.go @@ -315,11 +315,11 @@ func (self *SCloudproviderregion) markEndSync(ctx context.Context, userCred mccl log.Debugf("markEndSync deepSync %v", *deepSync) err := self.markEndSyncInternal(userCred, syncResults, deepSync) if err != nil { - return err + return errors.Wrapf(err, "markEndSyncInternal") } err = self.GetProvider().markEndSyncWithLock(ctx, userCred) if err != nil { - return err + return errors.Wrapf(err, "markEndSyncWithLock") } return nil } @@ -335,8 +335,7 @@ func (self *SCloudproviderregion) markEndSyncInternal(userCred mcclient.TokenCre return nil }) if err != nil { - log.Errorf("Failed to markEndSyncInternal error: %v", err) - return err + return errors.Wrapf(err, "db.Update") } return nil } @@ -373,11 +372,18 @@ func (set SSyncResultSet) Add(manager db.IModelManager, result compare.SyncResul func (self *SCloudproviderregion) DoSync(ctx context.Context, userCred mcclient.TokenCredential, syncRange SSyncRange) error { syncResults := SSyncResultSet{} - self.markSyncing(userCred) - defer self.markEndSync(ctx, userCred, syncResults, &syncRange.DeepSync) - localRegion := self.GetRegion() provider := self.GetProvider() + + self.markSyncing(userCred) + + defer func() { + err := self.markEndSync(ctx, userCred, syncResults, &syncRange.DeepSync) + if err != nil { + log.Errorf("markEndSync for %s(%s) : %v", localRegion.Name, provider.Name, err) + } + }() + driver, err := provider.GetProvider() if err != nil { log.Errorf("Failed to get driver, connection problem?") @@ -480,7 +486,7 @@ func (cpr *SCloudproviderregion) needAutoSyncInternal() bool { region := cpr.GetRegion() log.Debugf("empty region %s! no need to check so frequently", region.GetName()) } - if time.Now().Sub(cpr.LastSyncEndAt) > time.Duration(intval)*time.Second { + if time.Now().Sub(cpr.LastSync) > time.Duration(intval)*time.Second { return true } return false diff --git a/pkg/compute/models/cloudproviders.go b/pkg/compute/models/cloudproviders.go index e39aa32bb9..78c521c108 100644 --- a/pkg/compute/models/cloudproviders.go +++ b/pkg/compute/models/cloudproviders.go @@ -769,8 +769,7 @@ func (self *SCloudprovider) markEndSync(userCred mcclient.TokenCredential) error return nil }) if err != nil { - log.Errorf("Failed to markEndSync error: %v", err) - return err + return errors.Wrapf(err, "markEndSync") } return nil } @@ -1347,7 +1346,10 @@ func (provider *SCloudprovider) syncCloudproviderRegions(ctx context.Context, us } } if syncCnt == 0 { - provider.markEndSyncWithLock(ctx, userCred) + err := provider.markEndSyncWithLock(ctx, userCred) + if err != nil { + log.Errorf("markEndSyncWithLock for %s error: %v", provider.Name, err) + } } } diff --git a/pkg/compute/models/cloudsync.go b/pkg/compute/models/cloudsync.go index 3205815644..2dfc1d6ca9 100644 --- a/pkg/compute/models/cloudsync.go +++ b/pkg/compute/models/cloudsync.go @@ -45,7 +45,7 @@ type SSyncableBaseResourceManager struct{} func (self *SSyncableBaseResource) CanSync() bool { if self.SyncStatus == api.CLOUD_PROVIDER_SYNC_STATUS_QUEUED || self.SyncStatus == api.CLOUD_PROVIDER_SYNC_STATUS_SYNCING { - if self.LastSync.IsZero() || time.Now().Sub(self.LastSync) > 1800*time.Second { + if self.LastSync.IsZero() || time.Now().Sub(self.LastSync) > time.Duration(options.Options.MinimalSyncIntervalSeconds) { return true } else { return false diff --git a/pkg/compute/options/options.go b/pkg/compute/options/options.go index e247b551aa..cf0a52bf60 100644 --- a/pkg/compute/options/options.go +++ b/pkg/compute/options/options.go @@ -125,8 +125,8 @@ type ComputeOptions struct { CloudSyncWorkerCount int `help:"how many current synchronization threads" default:"5"` CloudAutoSyncIntervalSeconds int `help:"frequency to check auto sync tasks" default:"30"` - DefaultSyncIntervalSeconds int `help:"minimal synchronization interval, default 1 minutes" default:"900"` - MinimalSyncIntervalSeconds int `help:"minimal synchronization interval, default 1 minutes" default:"300"` + DefaultSyncIntervalSeconds int `help:"minimal synchronization interval, default 15 minutes" default:"900"` + MinimalSyncIntervalSeconds int `help:"minimal synchronization interval, default 30 minutes" default:"1800"` MaxCloudAccountErrorCount int `help:"maximal consecutive error count allow for a cloud account" default:"5"` NameSyncResources []string `help:"resources that need synchronization of name"`