github.com/safing/portbase@v0.19.5/database/maintenance.go (about)

     1  package database
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  )
     7  
     8  // Maintain runs the Maintain method on all storages.
     9  func Maintain(ctx context.Context) (err error) {
    10  	// copy, as we might use the very long
    11  	all := duplicateControllers()
    12  
    13  	for _, c := range all {
    14  		err = c.Maintain(ctx)
    15  		if err != nil {
    16  			return
    17  		}
    18  	}
    19  	return
    20  }
    21  
    22  // MaintainThorough runs the MaintainThorough method on all storages.
    23  func MaintainThorough(ctx context.Context) (err error) {
    24  	// copy, as we might use the very long
    25  	all := duplicateControllers()
    26  
    27  	for _, c := range all {
    28  		err = c.MaintainThorough(ctx)
    29  		if err != nil {
    30  			return
    31  		}
    32  	}
    33  	return
    34  }
    35  
    36  // MaintainRecordStates runs record state lifecycle maintenance on all storages.
    37  func MaintainRecordStates(ctx context.Context) (err error) {
    38  	// delete immediately for now
    39  	// TODO: increase purge threshold when starting to sync DBs
    40  	purgeDeletedBefore := time.Now().UTC()
    41  
    42  	// copy, as we might use the very long
    43  	all := duplicateControllers()
    44  
    45  	for _, c := range all {
    46  		err = c.MaintainRecordStates(ctx, purgeDeletedBefore)
    47  		if err != nil {
    48  			return
    49  		}
    50  	}
    51  	return
    52  }
    53  
    54  func duplicateControllers() (all []*Controller) {
    55  	controllersLock.RLock()
    56  	defer controllersLock.RUnlock()
    57  
    58  	all = make([]*Controller, 0, len(controllers))
    59  	for _, c := range controllers {
    60  		all = append(all, c)
    61  	}
    62  
    63  	return
    64  }