Skip to content

Commit

Permalink
Schedule priority (#29)
Browse files Browse the repository at this point in the history
* darwin: set value of nice from priority

* set nice value from priority in systemd service

* upgrade packages

* add no-prio flag

* update readme
  • Loading branch information
creativeprojects authored Jan 11, 2021
1 parent be72758 commit 8fd7a2e
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 90 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -789,12 +789,14 @@ schedule-log = "profile-backup.log"

Allow to redirect all output from resticprofile and restic to a file

#### schedule-priority
#### schedule-priority (systemd and launchd only)

Starting from version 0.11.0, `schedule-priority` accepts two values:
- `background`: the process shouldn't be noticeable when working on the machine at the same time (this is the default)
- `standard`: the process should get the same priority as any other process on the machine (but it won't run faster if you're not using the machine at the same time)

`schedule-priority` is not available for windows task scheduler, nor crond

#### schedule

The `schedule` parameter accepts many forms of input from the [systemd calendar event](https://www.freedesktop.org/software/systemd/man/systemd.time.html#Calendar%20Events) type. This is by far the easiest to use: **It is the same format used to schedule on macOS and Windows**.
Expand Down
2 changes: 1 addition & 1 deletion config/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Global struct {
func newGlobal() *Global {
return &Global{
IONice: constants.DefaultIONiceFlag,
Nice: constants.DefaultNiceFlag,
Nice: constants.DefaultStandardNiceFlag,
DefaultCommand: constants.DefaultCommand,
ResticBinary: constants.DefaultResticBinary,
MinMemory: constants.DefaultMinMemory,
Expand Down
1 change: 0 additions & 1 deletion config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ func (p *Profile) Schedules() []*ScheduleConfig {
schedules: s.Schedule,
permission: s.SchedulePermission,
environment: p.Environment,
nice: 10, // hard-coded for now
logfile: s.ScheduleLog,
priority: s.SchedulePriority,
}
Expand Down
17 changes: 12 additions & 5 deletions config/schedule.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package config

import (
"strings"

"github.com/creativeprojects/resticprofile/constants"
)

type ScheduleConfig struct {
profileName string
commandName string
Expand All @@ -11,7 +17,6 @@ type ScheduleConfig struct {
environment map[string]string
jobDescription string
timerDescription string
nice int
priority string
logfile string
}
Expand Down Expand Up @@ -70,11 +75,13 @@ func (s *ScheduleConfig) Environment() map[string]string {
return s.environment
}

func (s *ScheduleConfig) Nice() int {
return s.nice
}

// Priority is either "background" or "standard"
func (s *ScheduleConfig) Priority() string {
s.priority = strings.ToLower(s.priority)
// default value for priority is "background"
if s.priority != constants.SchedulePriorityBackground && s.priority != constants.SchedulePriorityStandard {
s.priority = constants.SchedulePriorityBackground
}
return s.priority
}

Expand Down
26 changes: 22 additions & 4 deletions config/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ func TestScheduleProperties(t *testing.T) {
environment: map[string]string{"test": "dev"},
jobDescription: "job",
timerDescription: "timer",
nice: 11,
priority: "background",
logfile: "log.txt",
}

Expand All @@ -33,7 +31,27 @@ func TestScheduleProperties(t *testing.T) {
assert.Equal(t, "home", schedule.WorkingDirectory())
assert.ElementsMatch(t, []string{"1", "2"}, schedule.Arguments())
assert.Equal(t, "dev", schedule.Environment()["test"])
assert.Equal(t, 11, schedule.Nice())
assert.Equal(t, "background", schedule.Priority())
assert.Equal(t, "background", schedule.Priority()) // default value
assert.Equal(t, "log.txt", schedule.Logfile())
}

func TestStandardPriority(t *testing.T) {
schedule := ScheduleConfig{
priority: "standard",
}
assert.Equal(t, "standard", schedule.Priority())
}

func TestCaseInsensitivePriority(t *testing.T) {
schedule := ScheduleConfig{
priority: "stANDard",
}
assert.Equal(t, "standard", schedule.Priority())
}

func TestOtherPriority(t *testing.T) {
schedule := ScheduleConfig{
priority: "other",
}
assert.Equal(t, "background", schedule.Priority()) // default value
}
21 changes: 11 additions & 10 deletions constants/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package constants

// Configuration defaults
const (
DefaultConfigurationFile = "profiles"
DefaultProfileName = "default"
DefaultCommand = "snapshots"
DefaultResticBinary = "restic"
DefaultTheme = "light"
DefaultIONiceFlag = false
DefaultNiceFlag = 0
DefaultVerboseFlag = false
DefaultQuietFlag = false
DefaultMinMemory = 100
DefaultConfigurationFile = "profiles"
DefaultProfileName = "default"
DefaultCommand = "snapshots"
DefaultResticBinary = "restic"
DefaultTheme = "light"
DefaultIONiceFlag = false
DefaultStandardNiceFlag = 0
DefaultBackgroundNiceFlag = 5
DefaultVerboseFlag = false
DefaultQuietFlag = false
DefaultMinMemory = 100
)
4 changes: 3 additions & 1 deletion examples/dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ self:
schedule:
- "*:15,45"
retention:
schedule: "weekly"
after-backup: true
forget:
schedule: "weekly"
schedule-priority: standard

system:
initialize: true
Expand Down
2 changes: 2 additions & 0 deletions flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type commandLineFlags struct {
wait bool
isChild bool
parentPort int
noPriority bool
}

// loadFlags loads command line flags (before any command)
Expand Down Expand Up @@ -56,6 +57,7 @@ func loadFlags() (*pflag.FlagSet, commandLineFlags) {

flagset.BoolVar(&flags.noAnsi, "no-ansi", false, "disable ansi control characters (disable console colouring)")
flagset.StringVar(&flags.theme, "theme", constants.DefaultTheme, "console colouring theme (dark, light, none)")
flagset.BoolVar(&flags.noPriority, "no-prio", false, "don't set any priority on load: used when started from a service that has already set the priority")

flagset.BoolVarP(&flags.wait, "wait", "w", false, "wait at the end until the user presses the enter key")

Expand Down
21 changes: 9 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,28 @@ module github.com/creativeprojects/resticprofile
go 1.13

require (
github.com/adrg/xdg v0.2.2
github.com/adrg/xdg v0.2.3
github.com/capnspacehook/taskmaster v0.0.0-20190802050140-eebf732b5748
github.com/coreos/go-systemd/v22 v22.1.0
github.com/creativeprojects/clog v0.8.0
github.com/creativeprojects/go-selfupdate v0.4.0
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mackerelio/go-osstat v0.1.0
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mitchellh/mapstructure v1.3.3
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/mitchellh/mapstructure v1.4.0
github.com/pelletier/go-toml v1.8.1 // indirect
github.com/rickb777/date v1.14.3
github.com/shirou/gopsutil/v3 v3.20.10
github.com/rickb777/date v1.15.3
github.com/shirou/gopsutil/v3 v3.20.12
github.com/smartystreets/assertions v1.0.0 // indirect
github.com/spf13/afero v1.4.1
github.com/spf13/afero v1.5.1
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.6.1
golang.org/x/crypto v0.0.0-20201112155050-0c6587e931a9
golang.org/x/sys v0.0.0-20201202213521-69691e467435
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad
golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061
google.golang.org/appengine v1.6.7 // indirect
gopkg.in/ini.v1 v1.57.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
howett.net/plist v0.0.0-20201026045517-117a925f2150
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
howett.net/plist v0.0.0-20201203080718-1454fab16a06
)
Loading

0 comments on commit 8fd7a2e

Please sign in to comment.