diff --git a/application/application.go b/application/application.go index 2078881..14d8f9e 100644 --- a/application/application.go +++ b/application/application.go @@ -164,6 +164,14 @@ func (s *server) Start() error { } func (s *server) Close() { + // Close audit recorder first to ensure all logs are persisted + if s.auditRecorder != nil { + s.logger.Info("Closing audit recorder...") + if err := s.auditRecorder.Close(); err != nil { + s.logger.Error("Failed to close audit recorder: %s", err) + } + } + if s.dbClient != nil { s.logger.Info("Shutting down database connection...") if err := s.dbClient.Close(); err != nil { diff --git a/assets b/assets index eb2cfac..a15906f 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit eb2cfac37d73e5bd3000eb66a3a0062509efe122 +Subproject commit a15906f3fb29710993e2e83835f92e563113aa18 diff --git a/inventory/user.go b/inventory/user.go index 26470ed..f394050 100644 --- a/inventory/user.go +++ b/inventory/user.go @@ -220,8 +220,29 @@ func (c *userClient) Delete(ctx context.Context, uid int) error { func (c *userClient) ApplyStorageDiff(ctx context.Context, diffs StorageDiff) error { ae := serializer.NewAggregateError() for uid, diff := range diffs { - if err := c.client.User.Update().Where(user.ID(uid)).AddStorage(diff).Exec(ctx); err != nil { - ae.Add(fmt.Sprintf("%d", uid), fmt.Errorf("failed to apply storage diff for user %d: %w", uid, err)) + // Retry logic for MySQL deadlock (Error 1213) + // This is a temporary workaround. TODO: optimize storage mutation + maxRetries := 3 + var lastErr error + for attempt := 0; attempt < maxRetries; attempt++ { + if err := c.client.User.Update().Where(user.ID(uid)).AddStorage(diff).Exec(ctx); err != nil { + lastErr = err + // Check if it's a MySQL deadlock error (Error 1213) + if strings.Contains(err.Error(), "Error 1213") && attempt < maxRetries-1 { + // Wait a bit before retrying with exponential backoff + time.Sleep(time.Duration(attempt+1) * 10 * time.Millisecond) + continue + } + ae.Add(fmt.Sprintf("%d", uid), fmt.Errorf("failed to apply storage diff for user %d: %w", uid, err)) + break + } + // Success, break out of retry loop + lastErr = nil + break + } + + if lastErr != nil { + ae.Add(fmt.Sprintf("%d", uid), fmt.Errorf("failed to apply storage diff for user %d: %w", uid, lastErr)) } }