|
2 | 2 | // Keymaster - SSH key management system |
3 | 3 | // This source code is licensed under the MIT license found in the LICENSE file. |
4 | 4 |
|
5 | | -// package tui provides the terminal user interface for Keymaster. |
6 | | -// This file contains the logic for the deployment view, which allows users |
7 | | -// to deploy keys to single accounts, tags, or the entire fleet. |
8 | 5 | package tui // import "github.com/toeirei/keymaster/internal/tui" |
9 | 6 |
|
10 | 7 | import ( |
11 | 8 | "fmt" |
| 9 | + "math/rand" |
12 | 10 | "sort" |
13 | 11 | "strings" |
| 12 | + "time" |
14 | 13 |
|
15 | 14 | "github.com/atotto/clipboard" |
16 | 15 | tea "github.com/charmbracelet/bubbletea" |
@@ -623,8 +622,22 @@ func performDeploymentCmd(account model.Account) tea.Cmd { |
623 | 622 | } |
624 | 623 |
|
625 | 624 | // 4. Update the database on success. |
626 | | - if err := db.UpdateAccountSerial(account.ID, activeKey.Serial); err != nil { |
627 | | - return deploymentResultMsg{account: account, err: fmt.Errorf(i18n.T("deploy.error_db_update_failed_tui", err))} |
| 625 | + // Retry updating the database serial on "database is locked" errors. |
| 626 | + // This is critical to prevent state inconsistency after a successful deployment. |
| 627 | + var dbUpdateErr error |
| 628 | + for i := 0; i < 5; i++ { // Retry up to 5 times |
| 629 | + dbUpdateErr = db.UpdateAccountSerial(account.ID, activeKey.Serial) |
| 630 | + if dbUpdateErr == nil { |
| 631 | + break // Success |
| 632 | + } |
| 633 | + if strings.Contains(dbUpdateErr.Error(), "database is locked") { |
| 634 | + time.Sleep(time.Duration(50+rand.Intn(100)) * time.Millisecond) // Wait 50-150ms |
| 635 | + continue |
| 636 | + } |
| 637 | + break // It's a different error, don't retry |
| 638 | + } |
| 639 | + if dbUpdateErr != nil { |
| 640 | + return deploymentResultMsg{account: account, err: fmt.Errorf(i18n.T("deploy.error_db_update_failed_tui", dbUpdateErr))} |
628 | 641 | } |
629 | 642 |
|
630 | 643 | return deploymentResultMsg{account: account, err: nil} // Success |
|
0 commit comments