mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-08-29 03:51:54 +08:00
Merge pull request #13756 from ioito/automated-cherry-pick-of-#13753-upstream-release-3.7
Automated cherry pick of #13753: fix(region): clean glance image cache
This commit is contained in:
@@ -37,6 +37,7 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/auth"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
||||
"yunion.io/x/onecloud/pkg/util/httputils"
|
||||
"yunion.io/x/onecloud/pkg/util/rbacutils"
|
||||
"yunion.io/x/onecloud/pkg/util/stringutils2"
|
||||
)
|
||||
@@ -231,11 +232,6 @@ func (manager *SCachedimageManager) cacheGlanceImageInfo(ctx context.Context, us
|
||||
imageCache := SCachedimage{}
|
||||
imageCache.SetModelManager(manager, &imageCache)
|
||||
|
||||
img.Name, err = db.GenerateName(manager, nil, img.Name)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "db.GenerateName(%s)", img.Name)
|
||||
}
|
||||
|
||||
err = manager.RawQuery().Equals("id", img.Id).First(&imageCache)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows { // insert
|
||||
@@ -313,7 +309,6 @@ func (manager *SCachedimageManager) GetImageById(ctx context.Context, userCred m
|
||||
s := auth.GetAdminSession(ctx, options.Options.Region, "")
|
||||
obj, err := modules.Images.Get(s, imageId, nil)
|
||||
if err != nil {
|
||||
log.Errorf("GetImageById %s error %s", imageId, err)
|
||||
return nil, errors.Wrap(err, "modules.Images.Get")
|
||||
}
|
||||
cachedImage, err := manager.cacheGlanceImageInfo(ctx, userCred, obj)
|
||||
@@ -820,6 +815,40 @@ func (manager *SCachedimageManager) ListItemExportKeys(ctx context.Context, q *s
|
||||
return q, nil
|
||||
}
|
||||
|
||||
// 清理已经删除的镜像缓存
|
||||
func (manager *SCachedimageManager) AutoCleanImageCaches(ctx context.Context, userCred mcclient.TokenCredential, isStart bool) {
|
||||
lastSync := time.Now().Add(time.Duration(-1*api.CACHED_IMAGE_REFERENCE_SESSION_EXPIRE_SECONDS) * time.Second)
|
||||
q := manager.Query()
|
||||
q = q.LT("last_sync", lastSync).Equals("status", api.CACHED_IMAGE_STATUS_ACTIVE).IsNullOrEmpty("external_id").Limit(50)
|
||||
caches := []SCachedimage{}
|
||||
err := db.FetchModelObjects(manager, q, &caches)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
s := auth.GetAdminSession(ctx, options.Options.Region, "")
|
||||
for i := range caches {
|
||||
_, err := modules.Images.Get(s, caches[i].Id, nil)
|
||||
if err != nil {
|
||||
if e, ok := err.(*httputils.JSONClientError); ok && e.Code == 404 {
|
||||
e := caches[i].ValidateDeleteCondition(ctx, nil)
|
||||
if e == nil {
|
||||
caches[i].Delete(ctx, userCred)
|
||||
continue
|
||||
}
|
||||
db.Update(&caches[i], func() error {
|
||||
caches[i].Name = fmt.Sprintf("%s-deleted@%s", caches[i].Name, timeutils.ShortDate(time.Now()))
|
||||
return nil
|
||||
})
|
||||
}
|
||||
continue
|
||||
}
|
||||
db.Update(&caches[i], func() error {
|
||||
caches[i].LastSync = time.Now()
|
||||
return nil
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (manager *SCachedimageManager) InitializeData() error {
|
||||
images := []SCachedimage{}
|
||||
q := manager.Query().IsNullOrEmpty("tenant_id")
|
||||
|
||||
@@ -151,6 +151,8 @@ func StartService() {
|
||||
cron.AddJobEveryFewHour("AutoDiskSnapshot", 1, 5, 0, models.DiskManager.AutoDiskSnapshot, false)
|
||||
cron.AddJobEveryFewHour("SnapshotsCleanup", 1, 35, 0, models.SnapshotManager.CleanupSnapshots, false)
|
||||
|
||||
cron.AddJobEveryFewHour("AutoCleanImageCache", 1, 5, 0, models.CachedimageManager.AutoCleanImageCaches, false)
|
||||
|
||||
cron.AddJobAtIntervalsWithStartRun("SyncSkus", time.Duration(opts.ServerSkuSyncIntervalMinutes)*time.Minute, models.SyncServerSkus, true)
|
||||
|
||||
cron.AddJobEveryFewDays("SyncDBInstanceSkus", opts.SyncSkusDay, opts.SyncSkusHour, 0, 0, models.SyncDBInstanceSkus, true)
|
||||
|
||||
Reference in New Issue
Block a user