github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/service/application.go (about)

     1  package service
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/pyroscope-io/pyroscope/pkg/model"
     7  )
     8  
     9  type AppDeleter interface {
    10  	DeleteApp(ctx context.Context, appName string) error
    11  }
    12  
    13  type ApplicationService struct {
    14  	ApplicationMetadataService
    15  	storageDeleter AppDeleter
    16  }
    17  
    18  // NewApplicationService creates an ApplicationService
    19  // Which just delegates to its underlying ApplicationMetadataService
    20  // Except when deleting, which is then forward to both ApplicationMetadataService and storageDeleter
    21  func NewApplicationService(appMetadataSvc ApplicationMetadataService, storageDeleter AppDeleter) ApplicationService {
    22  	return ApplicationService{
    23  		appMetadataSvc,
    24  		storageDeleter,
    25  	}
    26  }
    27  
    28  // Delete deletes apps from both storage and ApplicationMetadata
    29  // It first deletes from storage and only then deletes its metadata
    30  func (svc ApplicationService) Delete(ctx context.Context, name string) error {
    31  	err := model.ValidateAppName(name)
    32  	if err != nil {
    33  		return err
    34  	}
    35  
    36  	err = svc.storageDeleter.DeleteApp(ctx, name)
    37  	if err != nil {
    38  		return err
    39  	}
    40  
    41  	return svc.ApplicationMetadataService.Delete(ctx, name)
    42  }