diff --git a/docs/commands/dot_sync_target.md b/docs/commands/dot_sync_target.md index 6fc2e0a5..80e8499a 100644 --- a/docs/commands/dot_sync_target.md +++ b/docs/commands/dot_sync_target.md @@ -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] diff --git a/internal/cli/sync_target_cmd.go b/internal/cli/sync_target_cmd.go index e178a4ff..6adcc870 100644 --- a/internal/cli/sync_target_cmd.go +++ b/internal/cli/sync_target_cmd.go @@ -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, diff --git a/internal/syncer/scheduler_darwin.go b/internal/syncer/scheduler_darwin.go index 0ea44761..b5807eda 100644 --- a/internal/syncer/scheduler_darwin.go +++ b/internal/syncer/scheduler_darwin.go @@ -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 @@ -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 } diff --git a/internal/syncer/scheduler_other.go b/internal/syncer/scheduler_other.go index ade0e1c6..843035a6 100644 --- a/internal/syncer/scheduler_other.go +++ b/internal/syncer/scheduler_other.go @@ -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 @@ -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 } diff --git a/internal/syncer/sync_scheduler_lifecycle_test.go b/internal/syncer/sync_scheduler_lifecycle_test.go index d6a58268..b0bb7fe8 100644 --- a/internal/syncer/sync_scheduler_lifecycle_test.go +++ b/internal/syncer/sync_scheduler_lifecycle_test.go @@ -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 } @@ -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 { @@ -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 @@ -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) }