github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/engine/wallet_upkeep_background.go (about)

     1  // Copyright 2018 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  // WalletUpkeepBackground keeps the wallet bundle encrypted for the latest puk.
     5  
     6  package engine
     7  
     8  import (
     9  	"sync"
    10  	"time"
    11  
    12  	"github.com/keybase/client/go/libkb"
    13  )
    14  
    15  var WalletUpkeepBackgroundSettings = BackgroundTaskSettings{
    16  	Start:        40 * time.Second, // Wait after starting the app.
    17  	StartStagger: 20 * time.Second, // Wait an additional random amount.
    18  	WakeUp:       20 * time.Second, // Additional delay after waking from sleep.
    19  	Interval:     24 * time.Hour,   // Wait between checks
    20  	Limit:        10 * time.Minute, // Time limit on each round
    21  }
    22  
    23  // WalletUpkeepBackground is an engine.
    24  type WalletUpkeepBackground struct {
    25  	libkb.Contextified
    26  	sync.Mutex
    27  
    28  	args *WalletUpkeepBackgroundArgs
    29  	task *BackgroundTask
    30  }
    31  
    32  type WalletUpkeepBackgroundArgs struct {
    33  	// Channels used for testing. Normally nil.
    34  	testingMetaCh     chan<- string
    35  	testingRoundResCh chan<- error
    36  }
    37  
    38  // NewWalletUpkeepBackground creates a WalletUpkeepBackground engine.
    39  func NewWalletUpkeepBackground(g *libkb.GlobalContext, args *WalletUpkeepBackgroundArgs) *WalletUpkeepBackground {
    40  	task := NewBackgroundTask(g, &BackgroundTaskArgs{
    41  		Name:     "WalletUpkeepBackground",
    42  		F:        WalletUpkeepBackgroundRound,
    43  		Settings: WalletUpkeepBackgroundSettings,
    44  
    45  		testingMetaCh:     args.testingMetaCh,
    46  		testingRoundResCh: args.testingRoundResCh,
    47  	})
    48  	return &WalletUpkeepBackground{
    49  		Contextified: libkb.NewContextified(g),
    50  		args:         args,
    51  		// Install the task early so that Shutdown can be called before RunEngine.
    52  		task: task,
    53  	}
    54  }
    55  
    56  // Name is the unique engine name.
    57  func (e *WalletUpkeepBackground) Name() string {
    58  	return "WalletUpkeepBackground"
    59  }
    60  
    61  // GetPrereqs returns the engine prereqs.
    62  func (e *WalletUpkeepBackground) Prereqs() Prereqs {
    63  	return Prereqs{}
    64  }
    65  
    66  // RequiredUIs returns the required UIs.
    67  func (e *WalletUpkeepBackground) RequiredUIs() []libkb.UIKind {
    68  	return []libkb.UIKind{}
    69  }
    70  
    71  // SubConsumers returns the other UI consumers for this engine.
    72  func (e *WalletUpkeepBackground) SubConsumers() []libkb.UIConsumer {
    73  	return []libkb.UIConsumer{}
    74  }
    75  
    76  // Run starts the engine.
    77  // Returns immediately, kicks off a background goroutine.
    78  func (e *WalletUpkeepBackground) Run(m libkb.MetaContext) (err error) {
    79  	return RunEngine2(m, e.task)
    80  }
    81  
    82  func (e *WalletUpkeepBackground) Shutdown() {
    83  	e.task.Shutdown()
    84  }
    85  
    86  func WalletUpkeepBackgroundRound(m libkb.MetaContext) error {
    87  	g := m.G()
    88  	if g.ConnectivityMonitor.IsConnected(m.Ctx()) == libkb.ConnectivityMonitorNo {
    89  		m.Debug("WalletUpkeepBackgroundRound giving up offline")
    90  		return nil
    91  	}
    92  
    93  	if !g.ActiveDevice.Valid() {
    94  		m.Debug("WalletUpkeepBackgroundRound not logged in")
    95  		return nil
    96  	}
    97  
    98  	if !g.LocalSigchainGuard().IsAvailable(m.Ctx(), "WalletUpkeepBackgroundRound") {
    99  		m.Debug("WalletUpkeepBackgroundRound yielding to guard")
   100  		return nil
   101  	}
   102  
   103  	return g.GetStellar().Upkeep(m.Ctx())
   104  }