Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/commands/dot_sync_target.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ Accepted forms:
ssh:user@host:~/work rsync over SSH
~/Dropbox/work bare path — shorthand for local:

Local targets are also recorded in the global user state so future
workspaces inherit them.
The target belongs to this workspace: it is written only to that store's
config.yaml, and another workspace keeps its own. A workspace with no
target of its own syncs to the detected cloud folder.

```
dot sync target [spec] [flags]
Expand Down
5 changes: 3 additions & 2 deletions internal/cli/sync_target_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ Accepted forms:
ssh:user@host:~/work rsync over SSH
~/Dropbox/work bare path — shorthand for local:

Local targets are also recorded in the global user state so future
workspaces inherit them.`,
The target belongs to this workspace: it is written only to that store's
config.yaml, and another workspace keeps its own. A workspace with no
target of its own syncs to the detected cloud folder.`,
Args: cobra.MaximumNArgs(1),
RunE: runSyncTarget,
SilenceUsage: true,
Expand Down
7 changes: 7 additions & 0 deletions internal/syncer/scheduler_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ func (s *Scheduler) PauseKind(ctx context.Context, kind SchedulerKind) error {
}

// ResumeKind re-loads the launchd job from its persisted plist.
//
// The unload comes first because `load` leaves a job launchd already holds
// running the command line it cached: a plist rewritten on disk (the store
// flag migration) or a job stuck in spawn failed would stay broken otherwise.
// A job that is not loaded makes the unload fail, which is ignored the same
// way InstallKind ignores it.
func (s *Scheduler) ResumeKind(ctx context.Context, kind SchedulerKind) error {
if err := validateSchedulerMutationHome(s.Config.Home); err != nil {
return err
Expand All @@ -101,6 +107,7 @@ func (s *Scheduler) ResumeKind(ctx context.Context, kind SchedulerKind) error {
if s.requiresTargetUserServiceDomain() {
return &SchedulerTargetUserActionRequiredError{}
}
_, _ = s.Runner.Run(ctx, "launchctl", "unload", plist)
_, err := s.Runner.Run(ctx, "launchctl", "load", plist)
return err
}
Expand Down
9 changes: 8 additions & 1 deletion internal/syncer/scheduler_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ func (s *Scheduler) PauseKind(ctx context.Context, kind SchedulerKind) error {
return err
}

// ResumeKind starts the timer for the kind.
// ResumeKind reloads the units and starts the timer for the kind.
//
// daemon-reload runs first because systemd keeps the ExecStart it read until
// it is told to reload: a service unit rewritten on disk would otherwise start
// with the old command line.
func (s *Scheduler) ResumeKind(ctx context.Context, kind SchedulerKind) error {
if err := validateSchedulerMutationHome(s.Config.Home); err != nil {
return err
Expand All @@ -117,6 +121,9 @@ func (s *Scheduler) ResumeKind(ctx context.Context, kind SchedulerKind) error {
if s.requiresTargetUserServiceDomain() {
return &SchedulerTargetUserActionRequiredError{}
}
if _, err := s.Runner.Run(ctx, "systemctl", "--user", "daemon-reload"); err != nil {
return fmt.Errorf("daemon-reload: %w", err)
}
_, err := s.Runner.Run(ctx, "systemctl", "--user", "start", filepath.Base(timer))
return err
}
Expand Down
66 changes: 54 additions & 12 deletions internal/syncer/sync_scheduler_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,14 @@ func schedulerLifecycleCommands(paths *Paths, push, intake bool, operation strin
if !item.installed {
continue
}
commands = append(commands, schedulerLifecycleCommand(paths, item.kind, operation))
cmds := schedulerLifecycleCommand(paths, item.kind, operation)
if failFirst && runtime.GOOS != "darwin" {
// The failing command is the last one that ran: launchd ignores
// the unload error and the load then fails, while a failed
// daemon-reload returns before the timer is started.
cmds = cmds[:1]
}
commands = append(commands, cmds...)
if failFirst {
break
}
Expand All @@ -188,13 +195,24 @@ func schedulerStateCommand(paths *Paths, kind SchedulerKind) string {
return "--user is-active " + filepath.Base(paths.SystemdTimerFor(kind))
}

func schedulerLifecycleCommand(paths *Paths, kind SchedulerKind, operation string) string {
// schedulerLifecycleCommand is the argv a pause or resume makes for the kind.
// Resume is two commands on both platforms because starting a unit the
// service manager already holds is not enough to make it read the unit file
// again: launchd needs the unload, systemd needs the daemon-reload.
func schedulerLifecycleCommand(paths *Paths, kind SchedulerKind, operation string) []string {
reload := operation == "resume"
if runtime.GOOS == "darwin" {
command := map[string]string{"pause": "unload", "resume": "load"}[operation]
return command + " " + paths.PlistFor(kind)
plist := paths.PlistFor(kind)
if reload {
return []string{"unload " + plist, "load " + plist}
}
return []string{"unload " + plist}
}
timer := filepath.Base(paths.SystemdTimerFor(kind))
if reload {
return []string{"--user daemon-reload", "--user start " + timer}
}
command := map[string]string{"pause": "stop", "resume": "start"}[operation]
return "--user " + command + " " + filepath.Base(paths.SystemdTimerFor(kind))
return []string{"--user stop " + timer}
}

func readSchedulerLifecycleCommands(t *testing.T, record string) []string {
Expand Down Expand Up @@ -222,13 +240,40 @@ func schedulerStateQueryCount(commands []string) int {
func schedulerLifecycleActionCount(commands []string) int {
count := 0
for _, command := range commands {
if strings.HasPrefix(command, "unload ") || strings.HasPrefix(command, "load ") || strings.HasPrefix(command, "--user stop ") || strings.HasPrefix(command, "--user start ") {
if strings.HasPrefix(command, "unload ") || strings.HasPrefix(command, "load ") || strings.HasPrefix(command, "--user stop ") || strings.HasPrefix(command, "--user start ") || strings.HasPrefix(command, "--user daemon-reload") {
count++
}
}
return count
}

// schedulerLifecycleActionCount is what keeps the installed-kind check from
// passing vacuously, and a command it does not recognize drops out silently:
// the Linux resume row runs `daemon-reload` alone under an injected failure,
// which read as "made no lifecycle action". Pinned here so this platform
// notices a missing prefix instead of the ubuntu unit job.
func TestSchedulerLifecycleActionCount(t *testing.T) {
for _, command := range []string{
"unload /tmp/com.dotfiles.sync.plist",
"load /tmp/com.dotfiles.sync.plist",
"--user stop dotfiles-sync.timer",
"--user start dotfiles-sync.timer",
"--user daemon-reload",
} {
if got := schedulerLifecycleActionCount([]string{command}); got != 1 {
t.Errorf("schedulerLifecycleActionCount(%q) = %d, want 1", command, got)
}
}
for _, command := range []string{
"print gui/501/com.dotfiles.sync",
"--user is-active dotfiles-sync.timer",
} {
if got := schedulerLifecycleActionCount([]string{command}); got != 0 {
t.Errorf("schedulerLifecycleActionCount(%q) = %d, want 0: a state query is not an action", command, got)
}
}
}

// `dot sync resume` is the command that re-arms installed units, so it is where
// a unit installed before #394 gets its retired `--profile=` rewritten: the
// service manager reads the command line at load time, so a rewrite without the
Expand Down Expand Up @@ -304,12 +349,9 @@ func TestSyncResumeMigratesAUnitInstalledWithTheRetiredStoreFlag(t *testing.T) {
t.Fatalf("%s still runs the retired flag:\n%s", unit, body)
}
// The migration itself talks to no service manager: the only calls are the
// state query and the reload the resume already made.
// state query and the reload that makes the rewritten unit take effect.
got := readSchedulerLifecycleCommands(t, record)
want := []string{
schedulerStateCommand(paths, SchedulerKindPush),
schedulerLifecycleCommand(paths, SchedulerKindPush, "resume"),
}
want := append([]string{schedulerStateCommand(paths, SchedulerKindPush)}, schedulerLifecycleCommand(paths, SchedulerKindPush, "resume")...)
if strings.Join(got, "\n") != strings.Join(want, "\n") {
t.Fatalf("service-manager argv = %q, want %q", got, want)
}
Expand Down
Loading