Follow up to e3d54e: Same thing for querying for pre-saved entries

This commit is contained in:
David Dworken 2023-09-22 19:59:19 -07:00
parent e3d54e43e8
commit 51ca2578b7
No known key found for this signature in database
2 changed files with 29 additions and 4 deletions

View File

@ -206,10 +206,16 @@ func deletePresavedEntries(ctx context.Context, entry *data.HistoryEntry) error
matchingEntryQuery = matchingEntryQuery.Where("command = ?", entry.Command).Session(&gorm.Session{})
// Get the presaved entry since we need it for doing remote deletes
var presavedEntry data.HistoryEntry
res := matchingEntryQuery.Find(&presavedEntry)
if res.Error != nil {
return fmt.Errorf("failed to search for presaved entry for cmd=%#v: %w", entry.Command, res.Error)
presavedEntry, err := lib.RetryingDbFunctionWithResult(func() (data.HistoryEntry, error) {
var presavedEntry data.HistoryEntry
res := matchingEntryQuery.Find(&presavedEntry)
if res.Error != nil {
return presavedEntry, fmt.Errorf("failed to search for presaved entry for cmd=%#v: %w", entry.Command, res.Error)
}
return presavedEntry, nil
})
if err != nil {
return err
}
// Delete presaved entries locally

View File

@ -507,6 +507,25 @@ func RetryingDbFunction(dbFunc func() error) error {
return fmt.Errorf("failed to execute DB transaction even with %d retries: %w", i, err)
}
func RetryingDbFunctionWithResult[T any](dbFunc func() (T, error)) (T, error) {
var t T
var err error = nil
i := 0
for i = 0; i < 10; i++ {
t, err = dbFunc()
if err == nil {
return t, nil
}
errMsg := err.Error()
if strings.Contains(errMsg, SQLITE_LOCKED_ERR_MSG) {
time.Sleep(time.Duration(i*rand.Intn(100)) * time.Millisecond)
continue
}
return t, fmt.Errorf("unrecoverable sqlite error: %w", err)
}
return t, fmt.Errorf("failed to execute DB transaction even with %d retries: %w", i, err)
}
func ReliableDbCreate(db *gorm.DB, entry data.HistoryEntry) error {
entry = normalizeEntryTimezone(entry)
return RetryingDbFunction(func() error {