Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(daemon): add ExternalMode reboot #529

Merged
merged 2 commits into from
Dec 4, 2024

Conversation

anpep
Copy link
Collaborator

@anpep anpep commented Nov 28, 2024

This patch adds a new daemon.ExternalMode reboot mode that does nothing, so that code in charge of starting and stopping the Pebble daemon can run custom logic after the daemon has stopped.

When ExternalMode is set and the Pebble daemon stops after a system restart has been requested, daemon.Stop() returns the special daemon.ErrRestartExternal error that can be detected by the caller in order to execute custom reboot logic.

Furthermore, the fallback reboot handler is set to syscallModeReboot(), so that a system reboot is forced after rebootWaitTimeout is reached. In other words, the code responsible for handling the reboot with custom logic has a time span given by rebootWaitTimeout to perform a system reboot, or it will be forced.

func exampleUsage() error {
    /* ... */
    daemon.SetRebootMode(daemon.ExternalMode)
    d, err := daemon.New(...)
    if err != nil {
        return err
    }
    /* ... */

    sigs := make(chan os.Signal, 1)
    signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
    select {
    case <-sigs:
        // Exiting due to signal
    case <-d.Dying():
        // Exiting due to daemon error/requested reboot
    }

    err := d.Stop()
    if errors.Is(err, daemon.ErrRestartExternal) {
        // Custom reboot logic (~10min window before reboot(2) is called)
        /* ... */
        return nil
    }
    return err
}

@anpep anpep requested a review from flotter November 28, 2024 16:33
@anpep anpep self-assigned this Nov 28, 2024
@anpep anpep force-pushed the feat/reboot-handler branch from c8600fe to 2fd9aea Compare December 2, 2024 16:24
@anpep anpep changed the title feat(daemon): allow custom reboot handlers feat(daemon): add ExternalMode reboot Dec 2, 2024
@anpep anpep requested a review from benhoyt December 2, 2024 16:57
Copy link
Contributor

@benhoyt benhoyt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks reasonable to me, thanks. (Though I had one clarifying question.)

The only other thing I'd suggest is to use errors.Is instead of err == daemon.ErrRestartExternal in your PR description (though I see you've used it in the test code).

internals/daemon/daemon.go Outdated Show resolved Hide resolved
@anpep anpep marked this pull request as ready for review December 3, 2024 09:14
internals/daemon/daemon.go Outdated Show resolved Hide resolved
@anpep anpep requested review from benhoyt and flotter December 4, 2024 15:33
Copy link
Contributor

@flotter flotter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good for me now, thanks @anpep

Copy link
Contributor

@benhoyt benhoyt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@benhoyt benhoyt merged commit 1f82c99 into canonical:master Dec 4, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants