From 191753a3a0f2a6cf756f28783aa7c4f1ed6ba84b Mon Sep 17 00:00:00 2001 From: ioito Date: Wed, 16 Mar 2022 14:41:48 +0800 Subject: [PATCH] fix(region): sync with storagecachedimage status --- pkg/compute/models/storagecaches.go | 20 +++++++++++++++++++ .../tasks/cloudregion_sync_images_task.go | 14 +++++++++++++ 2 files changed, 34 insertions(+) diff --git a/pkg/compute/models/storagecaches.go b/pkg/compute/models/storagecaches.go index 9ca2834450..c54780b434 100644 --- a/pkg/compute/models/storagecaches.go +++ b/pkg/compute/models/storagecaches.go @@ -828,6 +828,25 @@ func (self *SStoragecache) unlinkCloudimages(ctx context.Context, userCred mccli return len(scis), nil } +func (self *SStoragecache) updateSystemImageStatus() (int, error) { + sq := CachedimageManager.Query("id").Equals("image_type", cloudprovider.ImageTypeSystem) + q := StoragecachedimageManager.Query(). + Equals("storagecache_id", self.Id).In("cachedimage_id", sq.SubQuery()). + NotEquals("status", api.CACHED_IMAGE_STATUS_ACTIVE) + scis := []SStoragecachedimage{} + err := db.FetchModelObjects(StoragecachedimageManager, q, &scis) + if err != nil { + return 0, errors.Wrapf(err, "db.FetchModelObjects") + } + for i := range scis { + db.Update(&scis[i], func() error { + scis[i].Status = api.CACHED_IMAGE_STATUS_ACTIVE + return nil + }) + } + return len(scis), nil +} + func (self *SStoragecache) getSystemImageCount() (int, error) { sq := StoragecachedimageManager.Query("cachedimage_id").Equals("storagecache_id", self.Id) q := CachedimageManager.Query().Equals("image_type", cloudprovider.ImageTypeSystem).In("id", sq.SubQuery()) @@ -845,6 +864,7 @@ func (self *SStoragecache) CheckCloudimages(ctx context.Context, userCred mcclie if err != nil { return errors.Wrapf(err, "unlinkCloudimages") } + self.updateSystemImageStatus() result.UpdateCnt, err = self.getSystemImageCount() if err != nil { log.Errorf("getSystemImageCount error: %v", err) diff --git a/pkg/compute/tasks/cloudregion_sync_images_task.go b/pkg/compute/tasks/cloudregion_sync_images_task.go index 4f99baa1fa..4a20bae58d 100644 --- a/pkg/compute/tasks/cloudregion_sync_images_task.go +++ b/pkg/compute/tasks/cloudregion_sync_images_task.go @@ -18,6 +18,7 @@ import ( "context" "yunion.io/x/jsonutils" + "yunion.io/x/log" "yunion.io/x/pkg/errors" "yunion.io/x/onecloud/pkg/cloudcommon/db" @@ -49,5 +50,18 @@ func (self *CloudregionSyncImagesTask) OnInit(ctx context.Context, obj db.IStand return } + storagecaches, err := region.GetStoragecaches() + if err != nil { + self.taskFailed(ctx, region, errors.Wrapf(err, "GetStoragecaches")) + return + } + + for i := range storagecaches { + err = storagecaches[i].CheckCloudimages(ctx, self.GetUserCred(), region.Name, region.Id) + if err != nil { + log.Errorf("SyncSystemImages for region %s(%s) storagecache %s error: %v", region.Name, region.Id, storagecaches[i].Name, err) + } + } + self.SetStageComplete(ctx, nil) }