From 6e4044b245752f7560b82eccf29e566dbbde1f0f Mon Sep 17 00:00:00 2001 From: Vitalii Date: Thu, 20 Jul 2023 18:37:53 +0300 Subject: [PATCH] satellite/payments: slightly refactor user upgrade observer of billing chore Resolves post-merge comments from here https://review.dev.storj.io/c/storj/storj/+/10780 Reworked some definitions. Added checks and comments. Change-Id: I19c63804ad1f30a1ffd8cb87e96f43deed20a685 --- satellite/core.go | 4 ++-- satellite/payments/billing/chore.go | 28 ++++++++++++++++-------- satellite/payments/billing/chore_test.go | 8 +++---- satellite/payments/billing/observer.go | 12 ---------- 4 files changed, 25 insertions(+), 27 deletions(-) delete mode 100644 satellite/payments/billing/observer.go diff --git a/satellite/core.go b/satellite/core.go index f44f6eca8..a76bd67ac 100644 --- a/satellite/core.go +++ b/satellite/core.go @@ -519,8 +519,8 @@ func New(log *zap.Logger, full *identity.FullIdentity, db DB, debug.Cycle("Payments Storjscan", peer.Payments.StorjscanChore.TransactionCycle), ) - choreObservers := map[billing.ObserverBilling]billing.Observer{ - billing.ObserverUpgradeUser: console.NewUpgradeUserObserver(peer.DB.Console(), peer.DB.Billing(), config.Console.UsageLimits, config.Console.UserBalanceForUpgrade), + choreObservers := billing.ChoreObservers{ + UpgradeUser: console.NewUpgradeUserObserver(peer.DB.Console(), peer.DB.Billing(), config.Console.UsageLimits, config.Console.UserBalanceForUpgrade), } peer.Payments.BillingChore = billing.NewChore( diff --git a/satellite/payments/billing/chore.go b/satellite/payments/billing/chore.go index 0087f0e95..ca6e3631f 100644 --- a/satellite/payments/billing/chore.go +++ b/satellite/payments/billing/chore.go @@ -13,13 +13,16 @@ import ( "storj.io/common/sync2" ) -// ObserverBilling used to create enumerable of chore observers. -type ObserverBilling int64 +// Observer processes a billing transaction. +type Observer interface { + // Process is called repeatedly for each transaction. + Process(context.Context, Transaction) error +} -const ( - // ObserverUpgradeUser stands for upgrade user observer type. - ObserverUpgradeUser ObserverBilling = 0 -) +// ChoreObservers holds functionality to process confirmed transactions using different types of observers. +type ChoreObservers struct { + UpgradeUser Observer +} // ChoreErr is billing chore err class. var ChoreErr = errs.Class("billing chore") @@ -35,11 +38,11 @@ type Chore struct { disableLoop bool bonusRate int64 - observers map[ObserverBilling]Observer + observers ChoreObservers } // NewChore creates new chore. -func NewChore(log *zap.Logger, paymentTypes []PaymentType, transactionsDB TransactionsDB, interval time.Duration, disableLoop bool, bonusRate int64, observers map[ObserverBilling]Observer) *Chore { +func NewChore(log *zap.Logger, paymentTypes []PaymentType, transactionsDB TransactionsDB, interval time.Duration, disableLoop bool, bonusRate int64, observers ChoreObservers) *Chore { return &Chore{ log: log, paymentTypes: paymentTypes, @@ -84,8 +87,15 @@ func (chore *Chore) Run(ctx context.Context) (err error) { break } - err = chore.observers[ObserverUpgradeUser].Process(ctx, transaction) + if chore.observers.UpgradeUser == nil { + continue + } + + err = chore.observers.UpgradeUser.Process(ctx, transaction) if err != nil { + // we don't want to halt storing transactions if upgrade user observer fails + // because this chore is designed to store new transactions. + // So auto upgrading user is a side effect which shouldn't interrupt the main process. chore.log.Error("error upgrading user", zap.Error(ChoreErr.Wrap(err))) } } diff --git a/satellite/payments/billing/chore_test.go b/satellite/payments/billing/chore_test.go index b5f100a99..968460732 100644 --- a/satellite/payments/billing/chore_test.go +++ b/satellite/payments/billing/chore_test.go @@ -85,8 +85,8 @@ func TestChore(t *testing.T) { ), } - choreObservers := map[billing.ObserverBilling]billing.Observer{ - billing.ObserverUpgradeUser: console.NewUpgradeUserObserver(consoleDB, db, usageLimitsConfig, userBalanceForUpgrade), + choreObservers := billing.ChoreObservers{ + UpgradeUser: console.NewUpgradeUserObserver(consoleDB, db, usageLimitsConfig, userBalanceForUpgrade), } chore := billing.NewChore(zaptest.NewLogger(t), paymentTypes, db, time.Hour, false, bonusRate, choreObservers) @@ -168,8 +168,8 @@ func TestChore_UpgradeUserObserver(t *testing.T) { _, err = sat.AddProject(ctx, user.ID, "Test Project") require.NoError(t, err) - choreObservers := map[billing.ObserverBilling]billing.Observer{ - billing.ObserverUpgradeUser: console.NewUpgradeUserObserver(db.Console(), db.Billing(), sat.Config.Console.UsageLimits, sat.Config.Console.UserBalanceForUpgrade), + choreObservers := billing.ChoreObservers{ + UpgradeUser: console.NewUpgradeUserObserver(db.Console(), db.Billing(), sat.Config.Console.UsageLimits, sat.Config.Console.UserBalanceForUpgrade), } amount1 := int64(200) // $2 diff --git a/satellite/payments/billing/observer.go b/satellite/payments/billing/observer.go deleted file mode 100644 index 224fd049b..000000000 --- a/satellite/payments/billing/observer.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright (C) 2023 Storj Labs, Inc. -// See LICENSE for copying information. - -package billing - -import "context" - -// Observer processes a billing transaction. -type Observer interface { - // Process is called repeatedly for each transaction. - Process(context.Context, Transaction) error -}