fix(api): fix snapshot schedule loading

pull/2543/head
Anthony Lapenna 6 years ago
parent 8a186b4024
commit acc5218c16

@ -116,9 +116,10 @@ func initJobScheduler() portainer.JobScheduler {
return cron.NewJobScheduler() return cron.NewJobScheduler()
} }
func loadSnapshotSystemSchedule(jobScheduler portainer.JobScheduler, snapshotter portainer.Snapshotter, scheduleService portainer.ScheduleService, endpointService portainer.EndpointService, flags *portainer.CLIFlags) error { func loadSnapshotSystemSchedule(jobScheduler portainer.JobScheduler, snapshotter portainer.Snapshotter, scheduleService portainer.ScheduleService, endpointService portainer.EndpointService, settingsService portainer.SettingsService) error {
if !*flags.Snapshot { settings, err := settingsService.Settings()
return nil if err != nil {
return err
} }
schedules, err := scheduleService.SchedulesByJobType(portainer.SnapshotJobType) schedules, err := scheduleService.SchedulesByJobType(portainer.SnapshotJobType)
@ -126,20 +127,20 @@ func loadSnapshotSystemSchedule(jobScheduler portainer.JobScheduler, snapshotter
return err return err
} }
if len(schedules) != 0 { var snapshotSchedule *portainer.Schedule
return nil if len(schedules) == 0 {
} snapshotJob := &portainer.SnapshotJob{}
snapshotSchedule = &portainer.Schedule{
snapshotJob := &portainer.SnapshotJob{} ID: portainer.ScheduleID(scheduleService.GetNextIdentifier()),
Name: "system_snapshot",
snapshotSchedule := &portainer.Schedule{ CronExpression: "@every " + settings.SnapshotInterval,
ID: portainer.ScheduleID(scheduleService.GetNextIdentifier()), Recurring: true,
Name: "system_snapshot", JobType: portainer.SnapshotJobType,
CronExpression: "@every " + *flags.SnapshotInterval, SnapshotJob: snapshotJob,
Recurring: true, Created: time.Now().Unix(),
JobType: portainer.SnapshotJobType, }
SnapshotJob: snapshotJob, } else {
Created: time.Now().Unix(), snapshotSchedule = &schedules[0]
} }
snapshotJobContext := cron.NewSnapshotJobContext(endpointService, snapshotter) snapshotJobContext := cron.NewSnapshotJobContext(endpointService, snapshotter)
@ -150,7 +151,10 @@ func loadSnapshotSystemSchedule(jobScheduler portainer.JobScheduler, snapshotter
return err return err
} }
return scheduleService.CreateSchedule(snapshotSchedule) if len(schedules) == 0 {
return scheduleService.CreateSchedule(snapshotSchedule)
}
return nil
} }
func loadEndpointSyncSystemSchedule(jobScheduler portainer.JobScheduler, scheduleService portainer.ScheduleService, endpointService portainer.EndpointService, flags *portainer.CLIFlags) error { func loadEndpointSyncSystemSchedule(jobScheduler portainer.JobScheduler, scheduleService portainer.ScheduleService, endpointService portainer.EndpointService, flags *portainer.CLIFlags) error {
@ -538,47 +542,49 @@ func main() {
snapshotter := initSnapshotter(clientFactory) snapshotter := initSnapshotter(clientFactory)
jobScheduler := initJobScheduler() endpointManagement := true
if *flags.ExternalEndpoints != "" {
endpointManagement = false
}
err = loadSchedulesFromDatabase(jobScheduler, jobService, store.ScheduleService, store.EndpointService, fileService) swarmStackManager, err := initSwarmStackManager(*flags.Assets, *flags.Data, digitalSignatureService, fileService)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
err = loadEndpointSyncSystemSchedule(jobScheduler, store.ScheduleService, store.EndpointService, flags) composeStackManager := initComposeStackManager(*flags.Data)
err = initTemplates(store.TemplateService, fileService, *flags.Templates, *flags.TemplateFile)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
err = loadSnapshotSystemSchedule(jobScheduler, snapshotter, store.ScheduleService, store.EndpointService, flags) err = initSettings(store.SettingsService, flags)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
jobScheduler.Start() jobScheduler := initJobScheduler()
endpointManagement := true
if *flags.ExternalEndpoints != "" {
endpointManagement = false
}
swarmStackManager, err := initSwarmStackManager(*flags.Assets, *flags.Data, digitalSignatureService, fileService) err = loadSchedulesFromDatabase(jobScheduler, jobService, store.ScheduleService, store.EndpointService, fileService)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
composeStackManager := initComposeStackManager(*flags.Data) err = loadEndpointSyncSystemSchedule(jobScheduler, store.ScheduleService, store.EndpointService, flags)
err = initTemplates(store.TemplateService, fileService, *flags.Templates, *flags.TemplateFile)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
err = initSettings(store.SettingsService, flags) if *flags.Snapshot {
if err != nil { err = loadSnapshotSystemSchedule(jobScheduler, snapshotter, store.ScheduleService, store.EndpointService, store.SettingsService)
log.Fatal(err) if err != nil {
log.Fatal(err)
}
} }
jobScheduler.Start()
err = initDockerHub(store.DockerHubService) err = initDockerHub(store.DockerHubService)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)

Loading…
Cancel
Save