github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/publisher/combo/piggyback.go (about)

     1  package combo
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/filecoin-project/bacalhau/pkg/model"
     7  	"github.com/filecoin-project/bacalhau/pkg/publisher"
     8  )
     9  
    10  type piggybackedPublisher struct {
    11  	publishers []publisher.Publisher
    12  }
    13  
    14  // NewPiggybackedPublisher will return a new publisher.Publisher that will call `primary` before then calling `carried`.
    15  // An error will be returned if the `carried` publisher fails, otherwise the returned objects will come from the
    16  // `primary` publisher.
    17  func NewPiggybackedPublisher(primary, carried publisher.Publisher) publisher.Publisher {
    18  	return &piggybackedPublisher{
    19  		publishers: []publisher.Publisher{primary, carried},
    20  	}
    21  }
    22  
    23  func (c *piggybackedPublisher) IsInstalled(ctx context.Context) (bool, error) {
    24  	installed, err := callAllPublishers(c.publishers, func(p publisher.Publisher) (bool, error) {
    25  		return p.IsInstalled(ctx)
    26  	})
    27  	if err != nil {
    28  		return false, err
    29  	}
    30  	for _, b := range installed {
    31  		if !b {
    32  			return false, nil
    33  		}
    34  	}
    35  
    36  	return true, nil
    37  }
    38  
    39  func (c *piggybackedPublisher) PublishShardResult(
    40  	ctx context.Context, shard model.JobShard, hostID string, shardResultPath string,
    41  ) (model.StorageSpec, error) {
    42  	results, err := callAllPublishers(c.publishers, func(p publisher.Publisher) (model.StorageSpec, error) {
    43  		return p.PublishShardResult(ctx, shard, hostID, shardResultPath)
    44  	})
    45  	if err != nil {
    46  		return model.StorageSpec{}, err
    47  	}
    48  
    49  	result := results[0]
    50  	if result.Metadata == nil {
    51  		result.Metadata = map[string]string{}
    52  	}
    53  	for _, other := range results[1:] {
    54  		result.Metadata[other.StorageSource.String()] = other.CID
    55  	}
    56  
    57  	return result, nil
    58  }
    59  
    60  func callAllPublishers[T any](publishers []publisher.Publisher, f func(publisher.Publisher) (T, error)) ([]T, error) {
    61  	var ts []T
    62  	for _, pub := range publishers {
    63  		t, err := f(pub)
    64  		if err != nil {
    65  			return nil, err
    66  		}
    67  		ts = append(ts, t)
    68  	}
    69  	return ts, nil
    70  }