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

overlord/snapstate: warn of refresh/postpone events #8571

Merged
merged 1 commit into from May 6, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions overlord/snapstate/autorefresh.go
Expand Up @@ -482,12 +482,19 @@ func getTime(st *state.State, timeKey string) (time.Time, error) {
// that period the refresh will go ahead despite application activity.
func inhibitRefresh(st *state.State, snapst *SnapState, info *snap.Info, checker func(*snap.Info) error) error {
if err := checker(info); err != nil {
days := int(maxInhibition.Truncate(time.Hour).Hours() / 24)
now := time.Now()
if snapst.RefreshInhibitedTime == nil {
// Store the instant when the snap was first inhibited.
// This is reset to nil on successful refresh.
snapst.RefreshInhibitedTime = &now
Set(st, info.InstanceName(), snapst)
if _, ok := err.(*BusySnapError); ok {
st.Warnf(i18n.NG(
"snap %q is currently in use. Its refresh will be postponed for up to %d day to wait for the snap to no longer be in use.",
"snap %q is currently in use. Its refresh will be postponed for up to %d days to wait for the snap to no longer be in use.", days),
info.SnapName(), days)
}
return err
}

Expand All @@ -496,6 +503,12 @@ func inhibitRefresh(st *state.State, snapst *SnapState, info *snap.Info, checker
// the error but don't change the snap state again.
return err
}
if _, ok := err.(*BusySnapError); ok {
st.Warnf(i18n.NG(
"snap %q has been running for the maximum allowable %d day since its refresh was postponed. It will now be refreshed.",
"snap %q has been running for the maximum allowable %d days since its refresh was postponed. It will now be refreshed.", days),
info.SnapName(), days)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

similar to above (I'm not totally happy with this, but it might prompt further discussion):
snap %q activity has postponed its refresh for the maximum allowable %d days. It will now be refreshed.

Copy link
Member

Choose a reason for hiding this comment

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

maybe

snap %q has been running for the maximum allowable %d days since its refresh was postponed. It will now be refreshed.

?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Applied (and above), thanks!

return nil
}
44 changes: 44 additions & 0 deletions overlord/snapstate/autorefresh_test.go
Expand Up @@ -652,3 +652,47 @@ func (s *autoRefreshTestSuite) TestRefreshOnMeteredConnNotMetered(c *C) {
c.Check(err, IsNil)
c.Check(s.store.ops, DeepEquals, []string{"list-refresh"})
}

func (s *autoRefreshTestSuite) TestInhibitRefreshWithinInhibitWindow(c *C) {
s.state.Lock()
defer s.state.Unlock()

si := &snap.SideInfo{RealName: "pkg", Revision: snap.R(1)}
info := &snap.Info{SideInfo: *si}
snapst := &snapstate.SnapState{
Sequence: []*snap.SideInfo{si},
Current: si.Revision,
}
err := snapstate.InhibitRefresh(s.state, snapst, info, func(si *snap.Info) error {
return &snapstate.BusySnapError{SnapName: "pkg"}
})
c.Assert(err, ErrorMatches, `snap "pkg" has running apps or hooks`)

pending, _ := s.state.PendingWarnings()
c.Assert(pending, HasLen, 1)
c.Check(pending[0].String(), Equals, `snap "pkg" is currently in use. Its refresh will be postponed for up to 7 days to wait for the snap to no longer be in use.`)
}

func (s *autoRefreshTestSuite) TestInhibitRefreshWarnsAndRefreshesWhenOverdue(c *C) {
s.state.Lock()
defer s.state.Unlock()

instant := time.Now()
pastInstant := instant.Add(-snapstate.MaxInhibition * 2)

si := &snap.SideInfo{RealName: "pkg", Revision: snap.R(1)}
info := &snap.Info{SideInfo: *si}
snapst := &snapstate.SnapState{
Sequence: []*snap.SideInfo{si},
Current: si.Revision,
RefreshInhibitedTime: &pastInstant,
}
err := snapstate.InhibitRefresh(s.state, snapst, info, func(si *snap.Info) error {
return &snapstate.BusySnapError{SnapName: "pkg"}
})
c.Assert(err, IsNil)

pending, _ := s.state.PendingWarnings()
c.Assert(pending, HasLen, 1)
c.Check(pending[0].String(), Equals, `snap "pkg" has been running for the maximum allowable 7 days since its refresh was postponed. It will now be refreshed.`)
}
6 changes: 6 additions & 0 deletions overlord/snapstate/export_test.go
Expand Up @@ -224,3 +224,9 @@ var (
func (m *SnapManager) MaybeUndoRemodelBootChanges(t *state.Task) error {
return m.maybeUndoRemodelBootChanges(t)
}

// autorefresh
var (
InhibitRefresh = inhibitRefresh
MaxInhibition = maxInhibition
)
12 changes: 6 additions & 6 deletions overlord/snapstate/refresh.go
Expand Up @@ -93,7 +93,7 @@ func genericRefreshCheck(info *snap.Info, canAppRunDuringRefresh func(app *snap.
sort.Strings(busyHookNames)
sort.Ints(busyPIDs)
return &BusySnapError{
snapName: info.SnapName(),
SnapName: info.SnapName(),
busyAppNames: busyAppNames,
busyHookNames: busyHookNames,
pids: busyPIDs,
Expand Down Expand Up @@ -137,7 +137,7 @@ func HardNothingRunningRefreshCheck(info *snap.Info) error {

// BusySnapError indicates that snap has apps or hooks running and cannot refresh.
type BusySnapError struct {
snapName string
SnapName string
pids []int
busyAppNames []string
busyHookNames []string
Expand All @@ -148,15 +148,15 @@ func (err *BusySnapError) Error() string {
switch {
case len(err.busyAppNames) > 0 && len(err.busyHookNames) > 0:
return fmt.Sprintf("snap %q has running apps (%s) and hooks (%s)",
err.snapName, strings.Join(err.busyAppNames, ", "), strings.Join(err.busyHookNames, ", "))
err.SnapName, strings.Join(err.busyAppNames, ", "), strings.Join(err.busyHookNames, ", "))
case len(err.busyAppNames) > 0:
return fmt.Sprintf("snap %q has running apps (%s)",
err.snapName, strings.Join(err.busyAppNames, ", "))
err.SnapName, strings.Join(err.busyAppNames, ", "))
case len(err.busyHookNames) > 0:
return fmt.Sprintf("snap %q has running hooks (%s)",
err.snapName, strings.Join(err.busyHookNames, ", "))
err.SnapName, strings.Join(err.busyHookNames, ", "))
default:
return fmt.Sprintf("snap %q has running apps or hooks", err.snapName)
return fmt.Sprintf("snap %q has running apps or hooks", err.SnapName)
}
}

Expand Down