From 945c4dc7793ec45d3b306fc004cd3d18c37fe337 Mon Sep 17 00:00:00 2001 From: Brian Ginsburg <7957636+bgins@users.noreply.github.com> Date: Wed, 9 Oct 2024 14:36:57 -0700 Subject: [PATCH] refactor: Move targeting into a function (#397) --- pkg/solver/matcher/matcher.go | 47 ++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/pkg/solver/matcher/matcher.go b/pkg/solver/matcher/matcher.go index 3c80ad9b..992539ea 100644 --- a/pkg/solver/matcher/matcher.go +++ b/pkg/solver/matcher/matcher.go @@ -40,29 +40,16 @@ func GetMatchingDeals( // loop over job offers for _, jobOffer := range jobOffers { - // See if our jobOffer targets a specific address. If so, we will create a deal automatically - // with the matcing resourceOffer. + // Check for targeted jobs if jobOffer.JobOffer.Target.Address != "" { - resourceOffer, err := db.GetResourceOfferByAddress(jobOffer.JobOffer.Target.Address) + deal, err := getTargetedDeal(db, jobOffer, updateJobOfferState) if err != nil { return nil, err } - // We don't have a resource provider for this address - if resourceOffer == nil { - log.Trace(). - Str("job offer", jobOffer.ID). - Str("target address", jobOffer.JobOffer.Target.Address). - Msgf("No resource provider found for address") - updateJobOfferState(jobOffer.ID, "", data.GetAgreementStateIndex("JobOfferCancelled")) - continue - } - - deal, err := data.GetDeal(jobOffer.JobOffer, resourceOffer.ResourceOffer) - if err != nil { - return nil, err + if deal != nil { + deals = append(deals, *deal) } - deals = append(deals, deal) continue } @@ -130,3 +117,29 @@ func GetMatchingDeals( return deals, nil } + +// See if our jobOffer targets a specific address. If so, we will create a deal automatically +// with the matcing resourceOffer. +func getTargetedDeal( + db store.SolverStore, + jobOffer data.JobOfferContainer, + updateJobOfferState func(string, string, uint8) (*data.JobOfferContainer, error), +) (*data.Deal, error) { + resourceOffer, err := db.GetResourceOfferByAddress(jobOffer.JobOffer.Target.Address) + if err != nil { + return nil, err + } + + // We don't have a resource provider for this address + if resourceOffer == nil { + log.Trace(). + Str("job offer", jobOffer.ID). + Str("target address", jobOffer.JobOffer.Target.Address). + Msgf("No resource provider found for address") + updateJobOfferState(jobOffer.ID, "", data.GetAgreementStateIndex("JobOfferCancelled")) + return nil, nil + } + + deal, err := data.GetDeal(jobOffer.JobOffer, resourceOffer.ResourceOffer) + return &deal, err +}