Skip to content

Commit

Permalink
refactor: Move targeting into a function (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
bgins authored Oct 9, 2024
1 parent 1cc64f2 commit 945c4dc
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions pkg/solver/matcher/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

0 comments on commit 945c4dc

Please sign in to comment.