From e7fe307d2340a4e119907737eab0f07e27d8d632 Mon Sep 17 00:00:00 2001 From: Milan Pavlik Date: Wed, 7 Sep 2022 10:56:05 +0000 Subject: [PATCH] [billing] Trigger ReconcileInvoices in ledger reconciler --- components/usage/pkg/apiv1/billing_noop.go | 7 ++++++- components/usage/pkg/controller/reconciler.go | 15 ++++++++++++--- components/usage/pkg/server/server.go | 8 +++----- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/components/usage/pkg/apiv1/billing_noop.go b/components/usage/pkg/apiv1/billing_noop.go index 6172e9d78d3896..8412fcd5c8915c 100644 --- a/components/usage/pkg/apiv1/billing_noop.go +++ b/components/usage/pkg/apiv1/billing_noop.go @@ -16,6 +16,11 @@ type BillingServiceNoop struct { } func (s *BillingServiceNoop) UpdateInvoices(_ context.Context, _ *v1.UpdateInvoicesRequest) (*v1.UpdateInvoicesResponse, error) { - log.Log.Infof("UpdateInvoices RPC invoked in no-op mode, no invoices will be updated.") + log.Infof("UpdateInvoices RPC invoked in no-op mode, no invoices will be updated.") return &v1.UpdateInvoicesResponse{}, nil } + +func (s *BillingServiceNoop) ReconcileInvoices(_ context.Context, _ *v1.ReconcileInvoicesRequest) (*v1.ReconcileInvoicesResponse, error) { + log.Infof("ReconcileInvoices RPC invoked in no-op mode, no invoices will be updated.") + return &v1.ReconcileInvoicesResponse{}, nil +} diff --git a/components/usage/pkg/controller/reconciler.go b/components/usage/pkg/controller/reconciler.go index eb109dfe5210a1..9c425e59a36b32 100644 --- a/components/usage/pkg/controller/reconciler.go +++ b/components/usage/pkg/controller/reconciler.go @@ -72,14 +72,16 @@ func (r *UsageAndBillingReconciler) Reconcile() (err error) { return nil } -func NewLedgerReconciler(usageClient v1.UsageServiceClient) *LedgerReconciler { +func NewLedgerReconciler(usageClient v1.UsageServiceClient, billingClient v1.BillingServiceClient) *LedgerReconciler { return &LedgerReconciler{ - usageClient: usageClient, + usageClient: usageClient, + billingClient: billingClient, } } type LedgerReconciler struct { - usageClient v1.UsageServiceClient + usageClient v1.UsageServiceClient + billingClient v1.BillingServiceClient } func (r *LedgerReconciler) Reconcile() error { @@ -102,5 +104,12 @@ func (r *LedgerReconciler) Reconcile() error { 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 } diff --git a/components/usage/pkg/server/server.go b/components/usage/pkg/server/server.go index dbf69bbbac4472..177167831f0875 100644 --- a/components/usage/pkg/server/server.go +++ b/components/usage/pkg/server/server.go @@ -114,10 +114,8 @@ func Start(cfg Config) error { } usageClient := v1.NewUsageServiceClient(selfConnection) - ctrl, err := controller.New(schedule, controller.NewUsageAndBillingReconciler( - usageClient, - v1.NewBillingServiceClient(selfConnection), - )) + billingClient := v1.NewBillingServiceClient(selfConnection) + ctrl, err := controller.New(schedule, controller.NewUsageAndBillingReconciler(usageClient, billingClient)) if err != nil { return fmt.Errorf("failed to initialize usage controller: %w", err) } @@ -128,7 +126,7 @@ func Start(cfg Config) error { } defer ctrl.Stop() - ledgerCtrl, err := controller.New(schedule, controller.NewLedgerReconciler(usageClient)) + ledgerCtrl, err := controller.New(schedule, controller.NewLedgerReconciler(usageClient, billingClient)) if err != nil { return fmt.Errorf("failed to initialize ledger controller: %w", err) }