fix: cloudaccount auto sync

This commit is contained in:
Qu Xuan
2021-04-01 16:16:03 +08:00
parent c34d6984d5
commit b9627f034d
6 changed files with 34 additions and 19 deletions
+6
View File
@@ -469,3 +469,9 @@ type CloudaccountSyncSkusInput struct {
CloudregionResourceInput
CloudproviderResourceInput
}
type CloudaccountEnableAutoSyncInput struct {
// 云账号状态必须是connected
// 最小值为region服务的minimal_sync_interval_seconds
SyncIntervalSeconds int `json:"sync_interval_seconds"`
}
+6 -5
View File
@@ -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
+14 -8
View File
@@ -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
+5 -3
View File
@@ -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)
}
}
}
+1 -1
View File
@@ -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
+2 -2
View File
@@ -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"`