Skip to content

Commit

Permalink
fix: AWS Marketplace Integration (#2049)
Browse files Browse the repository at this point in the history


You can only report usage once per hour which is enforced by the API. As such the only way to guarantee we only send a single usage each hour is to send a report at the end of the sync.


Closes: cloudquery/cloudquery-issues#2635
  • Loading branch information
bbernays authored Jan 15, 2025
1 parent 0163147 commit 97a3706
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions premium/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,10 @@ func (u *BatchUpdater) backgroundUpdater() {
for {
select {
case <-u.triggerUpdate:
// If we are using AWS Marketplace, we should only report the usage at the end of the sync
if u.awsMarketplaceClient != nil {
continue
}
if time.Since(u.lastUpdateTime) < u.minTimeBetweenFlushes {
// Not enough time since last update
continue
Expand All @@ -500,12 +504,6 @@ func (u *BatchUpdater) backgroundUpdater() {
// Not enough rows to update
continue
}
// If we are using AWS Marketplace, we need to round down to the nearest 1000
// Only on the last update, will we round up to the nearest 1000
// This will allow us to not overcharge the customer by rounding on each batch
if u.awsMarketplaceClient != nil {
totals = roundDown(totals, 1000)
}

if err := u.updateUsageWithRetryAndBackoff(ctx, totals, tables); err != nil {
u.logger.Warn().Err(err).Msg("failed to update usage")
Expand All @@ -514,6 +512,10 @@ func (u *BatchUpdater) backgroundUpdater() {
u.subtractTableUsage(tables, totals)

case <-u.flushDuration.C:
// If we are using AWS Marketplace, we should only report the usage at the end of the sync
if u.awsMarketplaceClient != nil {
continue
}
if time.Since(u.lastUpdateTime) < u.minTimeBetweenFlushes {
// Not enough time since last update
continue
Expand All @@ -524,12 +526,7 @@ func (u *BatchUpdater) backgroundUpdater() {
if totals == 0 {
continue
}
// If we are using AWS Marketplace, we need to round down to the nearest 1000
// Only on the last update, will we round up to the nearest 1000
// This will allow us to not overcharge the customer by rounding on each batch
if u.awsMarketplaceClient != nil {
totals = roundDown(totals, 1000)
}

if err := u.updateUsageWithRetryAndBackoff(ctx, totals, tables); err != nil {
u.logger.Warn().Err(err).Msg("failed to update usage")
continue
Expand Down

0 comments on commit 97a3706

Please sign in to comment.