1 // Copyright 2021 Chaos Mesh Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 16 package core 17 18 import ( 19 "context" 20 "time" 21 22 "github.com/jinzhu/gorm" 23 ) 24 25 // ScheduleStore defines operations for working with schedules. 26 type ScheduleStore interface { 27 // ListMeta returns schedule metadata list from the datastore. 28 ListMeta(ctx context.Context, namespace, name string, archived bool) ([]*ScheduleMeta, error) 29 30 // FindByUID returns a schedule by UID. 31 FindByUID(ctx context.Context, UID string) (*Schedule, error) 32 33 // FindMetaByUID returns a schedule metadata by UID. 34 FindMetaByUID(context.Context, string) (*ScheduleMeta, error) 35 36 // Set saves the schedule to datastore. 37 Set(context.Context, *Schedule) error 38 39 // Archive archives schedules which "archived" field is false. 40 Archive(ctx context.Context, namespace, name string) error 41 42 // Delete deletes the archive from the datastore. 43 Delete(context.Context, *Schedule) error 44 45 // DeleteByFinishTime deletes archives which time difference is greater than the given time from FinishTime. 46 DeleteByFinishTime(context.Context, time.Duration) error 47 48 // DeleteByUIDs deletes archives by the uid list. 49 DeleteByUIDs(context.Context, []string) error 50 51 // DeleteIncompleteSchedules deletes all incomplete schedules. 52 // If the chaos-dashboard was restarted and the schedule is completed during the restart, 53 // which means the schedule would never save the finish_time. 54 // DeleteIncompleteSchedules can be used to delete all incomplete schedules to avoid this case. 55 DeleteIncompleteSchedules(context.Context) error 56 } 57 58 // Schedule represents a schedule instance. Use in db. 59 type Schedule struct { 60 ScheduleMeta 61 Schedule string `gorm:"type:text;size:32768"` // JSON string 62 } 63 64 // ScheduleMeta defines the metadata of a schedule instance. Use in db. 65 type ScheduleMeta struct { 66 gorm.Model 67 UID string `gorm:"index:schedule_uid" json:"uid"` 68 Kind string `json:"kind"` 69 Name string `json:"name"` 70 Namespace string `json:"namespace"` 71 Action string `json:"action"` 72 StartTime time.Time `json:"start_time"` 73 FinishTime *time.Time `json:"finish_time"` 74 Archived bool `json:"archived"` 75 } 76