Reset system self-check after network change

This commit is contained in:
Daniel 2022-05-20 16:35:46 +02:00
parent bc745d4399
commit ea1b189330

View file

@ -26,6 +26,10 @@ var (
// selfcheckFails counts how often the self check failed successively. // selfcheckFails counts how often the self check failed successively.
// selfcheckFails is not locked as it is only accessed by the self-check task. // selfcheckFails is not locked as it is only accessed by the self-check task.
selfcheckFails int selfcheckFails int
// selfcheckNetworkChangedFlag is used to track changed to the network for
// the self-check.
selfcheckNetworkChangedFlag = netenv.GetNetworkChangedFlag()
) )
// selfcheckFailThreshold holds the threshold of how many times the selfcheck // selfcheckFailThreshold holds the threshold of how many times the selfcheck
@ -49,6 +53,7 @@ func prep() error {
func start() error { func start() error {
startNotify() startNotify()
selfcheckNetworkChangedFlag.Refresh()
selfcheckTask = module.NewTask("compatibility self-check", selfcheckTaskFunc). selfcheckTask = module.NewTask("compatibility self-check", selfcheckTaskFunc).
Repeat(5 * time.Minute). Repeat(5 * time.Minute).
MaxDelay(selfcheckTaskRetryAfter). MaxDelay(selfcheckTaskRetryAfter).
@ -83,19 +88,23 @@ func selfcheckTaskFunc(ctx context.Context, task *modules.Task) error {
// Run selfcheck and return if successful. // Run selfcheck and return if successful.
issue, err := selfcheck(ctx) issue, err := selfcheck(ctx)
if err == nil { switch {
selfCheckIsFailing.UnSet() case err == nil:
selfcheckFails = 0 // Successful.
resetSystemIssue()
tracer.Debugf("compat: self-check successful") tracer.Debugf("compat: self-check successful")
return nil case issue == nil:
} // Internal error.
tracer.Warningf("compat: %s", err)
case selfcheckNetworkChangedFlag.IsSet():
// The network changed, ignore the issue.
default:
// The self-check failed.
// Log result. // Set state and increase counter.
if issue != nil {
selfCheckIsFailing.Set() selfCheckIsFailing.Set()
selfcheckFails++ selfcheckFails++
// Log and notify.
tracer.Errorf("compat: %s", err) tracer.Errorf("compat: %s", err)
if selfcheckFails >= selfcheckFailThreshold { if selfcheckFails >= selfcheckFailThreshold {
issue.notify(err) issue.notify(err)
@ -103,14 +112,16 @@ func selfcheckTaskFunc(ctx context.Context, task *modules.Task) error {
// Retry quicker when failed. // Retry quicker when failed.
task.Schedule(time.Now().Add(selfcheckTaskRetryAfter)) task.Schedule(time.Now().Add(selfcheckTaskRetryAfter))
} else {
selfCheckIsFailing.UnSet()
selfcheckFails = 0
// Only log internal errors, but don't notify. return nil
tracer.Warningf("compat: %s", err)
} }
// Reset self-check state.
selfcheckNetworkChangedFlag.Refresh()
selfCheckIsFailing.UnSet()
selfcheckFails = 0
resetSystemIssue()
return nil return nil
} }