diff --git a/pkg/apis/compute/snapshotpolicy.go b/pkg/apis/compute/snapshotpolicy.go index 1c7b0feda6..a9790723fa 100644 --- a/pkg/apis/compute/snapshotpolicy.go +++ b/pkg/apis/compute/snapshotpolicy.go @@ -21,10 +21,10 @@ type SnapshotPolicyDetails struct { SSnapshotPolicy - RetentionDays int `json:"retention_days"` - RepeatWeekdays []int `json:"repeat_weekdays"` - TimePoints []int `json:"time_points"` - IsActivated *bool `json:"is_activated,omitempty"` + RetentionDays int `json:"retention_days"` + RepeatWeekdaysDisplay []int `json:"repeat_weekdays_display"` + TimePointsDisplay []int `json:"time_points_display"` + IsActivated *bool `json:"is_activated,omitempty"` BindingDiskCount int `json:"binding_disk_count"` } diff --git a/pkg/compute/models/disks.go b/pkg/compute/models/disks.go index 7eb87551f5..23d1f66c5e 100644 --- a/pkg/compute/models/disks.go +++ b/pkg/compute/models/disks.go @@ -2458,6 +2458,12 @@ func (self *SDisk) UpdataSnapshotsBackingDisk(backingDiskId string) error { func (manager *SDiskManager) AutoSyncExtDiskSnapshot(ctx context.Context, userCred mcclient.TokenCredential, isStart bool) { now := time.Now() + week := now.Weekday() + if week == 0 { + week += 7 + } + timePoint := now.Hour() + q := SnapshotPolicyDiskManager.Query().LE("next_sync_time", now) spds := make([]SSnapshotPolicyDisk, 0) err := db.FetchModelObjects(SnapshotPolicyDiskManager, q, &spds) @@ -2487,7 +2493,11 @@ func (manager *SDiskManager) AutoSyncExtDiskSnapshot(ctx context.Context, userCr db.OpsLog.LogEvent(disk, db.ACT_DISK_AUTO_SYNC_SNAPSHOT_FAIL, syncResult.Result(), userCred) continue } - if syncResult.AddCnt == 0 { + sp := spMap[spd.GetId()] + repeatWeekdays := SnapshotPolicyManager.RepeatWeekdaysToIntArray(sp.RepeatWeekdays) + timePoints := SnapshotPolicyManager.TimePointsToIntArray(sp.TimePoints) + if isInInts(int(week), repeatWeekdays) && isInInts(timePoint, timePoints) && syncResult.AddCnt == 0 { + // should add one continue } db.OpsLog.LogEvent(disk, db.ACT_DISK_AUTO_SYNC_SNAPSHOT, "disk auto sync snapshot successfully", userCred) @@ -2501,6 +2511,15 @@ func (manager *SDiskManager) AutoSyncExtDiskSnapshot(ctx context.Context, userCr } } +func isInInts(a int, array []int) bool { + for _, i := range array { + if i == a { + return true + } + } + return false +} + func (self *SDisk) syncSnapshots(ctx context.Context, userCred mcclient.TokenCredential) compare.SyncResult { syncResult := compare.SyncResult{} diff --git a/pkg/compute/models/snapshotpolicy.go b/pkg/compute/models/snapshotpolicy.go index 4cb03e1a1b..49afcd2e5a 100644 --- a/pkg/compute/models/snapshotpolicy.go +++ b/pkg/compute/models/snapshotpolicy.go @@ -370,8 +370,8 @@ func (sp *SSnapshotPolicy) GetExtraDetails( } func (sp *SSnapshotPolicy) getMoreDetails(out api.SnapshotPolicyDetails) api.SnapshotPolicyDetails { - out.RepeatWeekdays = SnapshotPolicyManager.RepeatWeekdaysToIntArray(sp.RepeatWeekdays) - out.TimePoints = SnapshotPolicyManager.TimePointsToIntArray(sp.TimePoints) + out.RepeatWeekdaysDisplay = SnapshotPolicyManager.RepeatWeekdaysToIntArray(sp.RepeatWeekdays) + out.TimePointsDisplay = SnapshotPolicyManager.TimePointsToIntArray(sp.TimePoints) out.BindingDiskCount, _ = SnapshotPolicyDiskManager.FetchDiskCountBySPID(sp.Id) return out } diff --git a/pkg/compute/models/snapshotpolicydisks.go b/pkg/compute/models/snapshotpolicydisks.go index e701a10e2f..7a21c86512 100644 --- a/pkg/compute/models/snapshotpolicydisks.go +++ b/pkg/compute/models/snapshotpolicydisks.go @@ -494,6 +494,16 @@ func (self *SSnapshotPolicyDiskManager) ValidateCreateData(ctx context.Context, return data, nil } +func (sd *SSnapshotPolicyDisk) CustomizeCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) error { + sp, err := SnapshotPolicyManager.FetchSnapshotPolicyById(sd.SnapshotpolicyId) + if err != nil { + return err + } + now := time.Now() + sd.NextSyncTime = sp.ComputeNextSyncTime(now, now) + return nil +} + func (sd *SSnapshotPolicyDisk) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient. IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) {