Skip to content

Commit

Permalink
[usage] make increment billing cycle more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
svenefftinge committed Dec 7, 2022
1 parent 98bec46 commit 0086ac4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
3 changes: 2 additions & 1 deletion components/gitpod-db/go/cost_center.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion components/usage/pkg/apiv1/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
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
}

Expand Down

0 comments on commit 0086ac4

Please sign in to comment.