-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[usage] Track last compelted ledger job time
- Loading branch information
Showing
3 changed files
with
83 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License-AGPL.txt in the project root for license information. | ||
|
||
package scheduler | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/gitpod-io/gitpod/common-go/log" | ||
v1 "github.com/gitpod-io/gitpod/usage-api/v1" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
"time" | ||
) | ||
|
||
func NewLedgerTriggerJobSpec(schedule time.Duration, job Job) (JobSpec, error) { | ||
return NewPeriodicJobSpec(schedule, "ledger", WithoutConcurrentRun(job)) | ||
} | ||
|
||
func NewLedgerTrigger(usageClient v1.UsageServiceClient, billingClient v1.BillingServiceClient) *LedgerJob { | ||
return &LedgerJob{ | ||
usageClient: usageClient, | ||
billingClient: billingClient, | ||
} | ||
} | ||
|
||
type LedgerJob struct { | ||
usageClient v1.UsageServiceClient | ||
billingClient v1.BillingServiceClient | ||
} | ||
|
||
func (r *LedgerJob) Run() (err error) { | ||
defer func() { | ||
reportLedgerCompleted(err) | ||
}() | ||
|
||
ctx := context.Background() | ||
now := time.Now().UTC() | ||
hourAgo := now.Add(-1 * time.Hour) | ||
|
||
logger := log. | ||
WithField("from", hourAgo). | ||
WithField("to", now) | ||
|
||
logger.Info("Running ledger job. Reconciling usage records.") | ||
_, err = r.usageClient.ReconcileUsage(ctx, &v1.ReconcileUsageRequest{ | ||
From: timestamppb.New(hourAgo), | ||
To: timestamppb.New(now), | ||
}) | ||
if err != nil { | ||
logger.WithError(err).Errorf("Failed to reconcile usage with ledger.") | ||
return fmt.Errorf("failed to reconcile usage with ledger: %w", err) | ||
} | ||
|
||
logger.Info("Starting invoice reconciliation.") | ||
_, err = r.billingClient.ReconcileInvoices(ctx, &v1.ReconcileInvoicesRequest{}) | ||
if err != nil { | ||
logger.WithError(err).Errorf("Failed to reconcile invoices.") | ||
return fmt.Errorf("failed to reconcile invoices: %w", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters