From f0816acff403775a9d4586d649d19527a437fe6a Mon Sep 17 00:00:00 2001 From: Sven Efftinge Date: Wed, 7 Dec 2022 08:46:55 +0000 Subject: [PATCH] [usage] make increment billing cycle more robust --- components/gitpod-db/go/cost_center.go | 3 ++- components/usage/pkg/apiv1/billing.go | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/components/gitpod-db/go/cost_center.go b/components/gitpod-db/go/cost_center.go index 48596e40ac828e..728bafe481fe87 100644 --- a/components/gitpod-db/go/cost_center.go +++ b/components/gitpod-db/go/cost_center.go @@ -138,7 +138,8 @@ func (c *CostCenterManager) IncrementBillingCycle(ctx context.Context, attributi } now := time.Now().UTC() if cc.NextBillingTime.Time().After(now) { - return CostCenter{}, status.Errorf(codes.FailedPrecondition, "cannot increment billing cycle before next billing time") + log.Infof("Cost center %s is not yet expired. Skipping increment.", attributionId) + return cc, nil } billingCycleStart := NewVarCharTime(now) if cc.NextBillingTime.IsSet() { diff --git a/components/usage/pkg/apiv1/billing.go b/components/usage/pkg/apiv1/billing.go index 98a2ed3bfdcc89..0dbf8fe3c9c6b9 100644 --- a/components/usage/pkg/apiv1/billing.go +++ b/components/usage/pkg/apiv1/billing.go @@ -299,12 +299,13 @@ func (s *BillingService) FinalizeInvoice(ctx context.Context, in *v1.FinalizeInv logger.WithError(err).Errorf("Failed to insert Invoice usage record into the db.") return nil, status.Errorf(codes.Internal, "Failed to insert Invoice into usage records.") } + logger.WithField("usage_id", usage.ID).Infof("Inserted usage record into database for %f credits against %s attribution", usage.CreditCents.ToCredits(), usage.AttributionID) + _, err = s.ccManager.IncrementBillingCycle(ctx, usage.AttributionID) if err != nil { + // we are just logging at this point, so that we don't see the event again as the usage has been recorded. logger.WithError(err).Errorf("Failed to increment billing cycle.") - return nil, status.Errorf(codes.Internal, "Failed to increment billing cycle.") } - logger.WithField("usage_id", usage.ID).Infof("Inserted usage record into database for %f credits against %s attribution", usage.CreditCents.ToCredits(), usage.AttributionID) return &v1.FinalizeInvoiceResponse{}, nil }