Skip to content

Commit

Permalink
removeJobs now searches for existing scheduled jobs to remove
Browse files Browse the repository at this point in the history
  • Loading branch information
creativeprojects committed Nov 3, 2024
1 parent d51c9ba commit c11d105
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 16 deletions.
44 changes: 29 additions & 15 deletions commands_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,44 @@ func createSchedule(_ io.Writer, ctx commandContext) error {
}

func removeSchedule(_ io.Writer, ctx commandContext) error {
var err error

Check warning on line 73 in commands_schedule.go

View check run for this annotation

Codecov / codecov/patch

commands_schedule.go#L73

Added line #L73 was not covered by tests
c := ctx.config
flags := ctx.flags
args := ctx.request.arguments

// Unschedule all jobs of all selected profiles
for _, profileName := range selectProfilesAndGroups(c, flags, args) {
profileFlags := flagsForProfile(flags, profileName)
if slices.Contains(args, "--legacy") { // TODO: remove this option in the future
// Unschedule all jobs of all selected profiles
for _, profileName := range selectProfilesAndGroups(c, flags, args) {
profileFlags := flagsForProfile(flags, profileName)

Check warning on line 81 in commands_schedule.go

View check run for this annotation

Codecov / codecov/patch

commands_schedule.go#L78-L81

Added lines #L78 - L81 were not covered by tests

schedulerConfig, jobs, err := getRemovableScheduleJobs(c, profileFlags)
if err != nil {
return err
}
schedulerConfig, jobs, err := getRemovableScheduleJobs(c, profileFlags)
if err != nil {
return err
}

Check warning on line 86 in commands_schedule.go

View check run for this annotation

Codecov / codecov/patch

commands_schedule.go#L83-L86

Added lines #L83 - L86 were not covered by tests

err = removeJobs(schedule.NewHandler(schedulerConfig), jobs)
if err != nil {
err = retryElevated(err, flags)
}
if err != nil {
// we keep trying to remove the other jobs
clog.Error(err)
err = removeJobs(schedule.NewHandler(schedulerConfig), jobs)
if err != nil {
err = retryElevated(err, flags)
}
if err != nil {
// we keep trying to remove the other jobs
clog.Error(err)
}

Check warning on line 95 in commands_schedule.go

View check run for this annotation

Codecov / codecov/patch

commands_schedule.go#L88-L95

Added lines #L88 - L95 were not covered by tests
}
return nil

Check warning on line 97 in commands_schedule.go

View check run for this annotation

Codecov / codecov/patch

commands_schedule.go#L97

Added line #L97 was not covered by tests
}

return nil
profileName := ctx.request.profile
if slices.Contains(args, "--all") {
// Unschedule all jobs of all profiles
profileName = ""
}
schedulerConfig := schedule.NewSchedulerConfig(ctx.global)
err = removeScheduledJobs(schedule.NewHandler(schedulerConfig), ctx.config.GetConfigFile(), profileName)
if err != nil {
err = retryElevated(err, flags)
}
return err

Check warning on line 110 in commands_schedule.go

View check run for this annotation

Codecov / codecov/patch

commands_schedule.go#L100-L110

Added lines #L100 - L110 were not covered by tests
}

func statusSchedule(w io.Writer, ctx commandContext) error {
Expand Down
30 changes: 30 additions & 0 deletions schedule_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,36 @@ func removeJobs(handler schedule.Handler, configs []*config.Schedule) error {
return nil
}

func removeScheduledJobs(handler schedule.Handler, configFile, profileName string) error {
err := handler.Init()
if err != nil {
return err
}

Check warning on line 100 in schedule_jobs.go

View check run for this annotation

Codecov / codecov/patch

schedule_jobs.go#L99-L100

Added lines #L99 - L100 were not covered by tests
defer handler.Close()

clog.Debugf("looking up schedules from configuration file %s", configFile)
configs, err := handler.Scheduled(profileName)
if err != nil {
return err
}

Check warning on line 107 in schedule_jobs.go

View check run for this annotation

Codecov / codecov/patch

schedule_jobs.go#L106-L107

Added lines #L106 - L107 were not covered by tests
if len(configs) == 0 {
clog.Info("no scheduled jobs found")
return nil
}
for _, cfg := range configs {
if cfg.ConfigFile != configFile {
clog.Debugf("skipping job %s/%s from configuration file %s", cfg.ProfileName, cfg.CommandName, cfg.ConfigFile)
continue
}
err = handler.RemoveJob(&cfg, cfg.Permission)
if err != nil {
clog.Error(err)
}

Check warning on line 120 in schedule_jobs.go

View check run for this annotation

Codecov / codecov/patch

schedule_jobs.go#L119-L120

Added lines #L119 - L120 were not covered by tests
clog.Infof("scheduled job %s/%s removed", cfg.ProfileName, cfg.CommandName)
}
return nil
}

func statusJobs(handler schedule.Handler, profileName string, configs []*config.Schedule) error {
err := handler.Init()
if err != nil {
Expand Down
69 changes: 69 additions & 0 deletions schedule_jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,72 @@ func TestStatusRemoveOnlyJob(t *testing.T) {
err := statusJobs(handler, "profile", []*config.Schedule{scheduleConfig})
assert.Error(t, err)
}

func TestRemoveScheduledJobs(t *testing.T) {
testCases := []struct {
removeProfileName string
fromConfigFile string
scheduledConfigs []schedule.Config
removedConfigs []schedule.Config
permission string
}{
{
removeProfileName: "profile_no_config",
fromConfigFile: "configFile",
scheduledConfigs: []schedule.Config{},
removedConfigs: []schedule.Config{},
permission: "user",
},
{
removeProfileName: "profile_one_config_to_remove",
fromConfigFile: "configFile",
scheduledConfigs: []schedule.Config{
{
ProfileName: "profile_one_config_to_remove",
CommandName: "backup",
ConfigFile: "configFile",
Permission: "user",
},
},
removedConfigs: []schedule.Config{
{
ProfileName: "profile_one_config_to_remove",
CommandName: "backup",
ConfigFile: "configFile",
Permission: "user",
},
},
permission: "user",
},
{
removeProfileName: "profile_different_config_file",
fromConfigFile: "configFile",
scheduledConfigs: []schedule.Config{
{
ProfileName: "profile_different_config_file",
CommandName: "backup",
ConfigFile: "other_configFile",
Permission: "user",
},
},
removedConfigs: []schedule.Config{},
permission: "user",
},
}

for _, tc := range testCases {
t.Run(tc.removeProfileName, func(t *testing.T) {
handler := mocks.NewHandler(t)
handler.EXPECT().Init().Return(nil)
handler.EXPECT().Close()

handler.EXPECT().Scheduled(tc.removeProfileName).Return(tc.scheduledConfigs, nil)
for _, cfg := range tc.removedConfigs {
handler.EXPECT().RemoveJob(&cfg, tc.permission).Return(nil)
}

err := removeScheduledJobs(handler, tc.fromConfigFile, tc.removeProfileName)
assert.NoError(t, err)
})
}
}
2 changes: 1 addition & 1 deletion systemd/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Read(unit string, unitType UnitType) (*Config, error) {
IOSchedulingClass: getIntegerValue(serviceSections, "Service", "IOSchedulingClass"),
IOSchedulingPriority: getIntegerValue(serviceSections, "Service", "IOSchedulingPriority"),
Schedules: getValues(timerSections, "Timer", "OnCalendar"),
Priority: "background", //TODO fix this hard-coded value
Priority: "background", // TODO fix this hard-coded value
}
return cfg, nil
}
Expand Down

0 comments on commit c11d105

Please sign in to comment.