github.com/jfrog/jfrog-cli-core/v2@v2.51.0/artifactory/commands/transferfiles/phase.go (about) 1 package transferfiles 2 3 import ( 4 "context" 5 "errors" 6 "os" 7 "time" 8 9 "github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/transferfiles/api" 10 11 "github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/transferfiles/state" 12 coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config" 13 serviceUtils "github.com/jfrog/jfrog-client-go/artifactory/services/utils" 14 ) 15 16 const NumberOfPhases = 3 17 18 type transferPhase interface { 19 run() error 20 phaseStarted() error 21 phaseDone() error 22 setContext(context context.Context) 23 setRepoKey(repoKey string) 24 setCheckExistenceInFilestore(bool) 25 shouldSkipPhase() (bool, error) 26 setSrcUserPluginService(*srcUserPluginService) 27 setSourceDetails(*coreConfig.ServerDetails) 28 getSourceDetails() *coreConfig.ServerDetails 29 setTargetDetails(*coreConfig.ServerDetails) 30 setRepoSummary(serviceUtils.RepositorySummary) 31 getPhaseName() string 32 setProgressBar(*TransferProgressMng) 33 setStateManager(stateManager *state.TransferStateManager) 34 setLocallyGeneratedFilter(locallyGeneratedFilter *locallyGeneratedFilter) 35 initProgressBar() error 36 setProxyKey(proxyKey string) 37 setBuildInfo(setBuildInfo bool) 38 setPackageType(packageType string) 39 setDisabledDistinctiveAql() 40 setStopSignal(stopSignal chan os.Signal) 41 setMinCheckSumDeploySize(minCheckSumDeploySize int64) 42 StopGracefully() 43 } 44 45 type phaseBase struct { 46 context context.Context 47 repoKey string 48 buildInfoRepo bool 49 packageType string 50 phaseId int 51 checkExistenceInFilestore bool 52 startTime time.Time 53 srcUpService *srcUserPluginService 54 srcRtDetails *coreConfig.ServerDetails 55 targetRtDetails *coreConfig.ServerDetails 56 progressBar *TransferProgressMng 57 repoSummary serviceUtils.RepositorySummary 58 proxyKey string 59 pcDetails *producerConsumerWrapper 60 transferManager *transferManager 61 stateManager *state.TransferStateManager 62 locallyGeneratedFilter *locallyGeneratedFilter 63 stopSignal chan os.Signal 64 // Optimization in Artifactory version 7.37 and above enables the exclusion of setting DISTINCT in SQL queries 65 disabledDistinctiveAql bool 66 minCheckSumDeploySize int64 67 } 68 69 func (pb *phaseBase) ShouldStop() bool { 70 return pb.context.Err() != nil 71 } 72 73 // Return InterruptionError, if stop is true 74 func (pb *phaseBase) getInterruptionErr() error { 75 if errors.Is(pb.context.Err(), context.Canceled) { 76 return new(InterruptionErr) 77 } 78 return nil 79 } 80 81 // Stop and indicate graceful stopping in the progress bar 82 func (pb *phaseBase) StopGracefully() { 83 if pb.progressBar != nil { 84 pb.progressBar.StopGracefully() 85 } 86 if pb.pcDetails != nil { 87 pb.pcDetails.chunkBuilderProducerConsumer.Cancel(true) 88 pb.pcDetails.chunkUploaderProducerConsumer.Cancel(true) 89 } 90 } 91 92 func (pb *phaseBase) getSourceDetails() *coreConfig.ServerDetails { 93 return pb.srcRtDetails 94 } 95 96 func (pb *phaseBase) setContext(context context.Context) { 97 pb.context = context 98 } 99 100 func (pb *phaseBase) setRepoKey(repoKey string) { 101 pb.repoKey = repoKey 102 } 103 104 func (pb *phaseBase) setCheckExistenceInFilestore(shouldCheck bool) { 105 pb.checkExistenceInFilestore = shouldCheck 106 } 107 108 func (pb *phaseBase) setSrcUserPluginService(service *srcUserPluginService) { 109 pb.srcUpService = service 110 } 111 112 func (pb *phaseBase) setSourceDetails(details *coreConfig.ServerDetails) { 113 pb.srcRtDetails = details 114 } 115 116 func (pb *phaseBase) setTargetDetails(details *coreConfig.ServerDetails) { 117 pb.targetRtDetails = details 118 } 119 120 func (pb *phaseBase) setRepoSummary(repoSummary serviceUtils.RepositorySummary) { 121 pb.repoSummary = repoSummary 122 } 123 124 func (pb *phaseBase) setProgressBar(progressbar *TransferProgressMng) { 125 pb.progressBar = progressbar 126 } 127 128 func (pb *phaseBase) setProxyKey(proxyKey string) { 129 pb.proxyKey = proxyKey 130 } 131 132 func (pb *phaseBase) setStateManager(stateManager *state.TransferStateManager) { 133 pb.stateManager = stateManager 134 } 135 136 func (pb *phaseBase) setLocallyGeneratedFilter(locallyGeneratedFilter *locallyGeneratedFilter) { 137 pb.locallyGeneratedFilter = locallyGeneratedFilter 138 } 139 140 func (pb *phaseBase) setBuildInfo(buildInfoRepo bool) { 141 pb.buildInfoRepo = buildInfoRepo 142 } 143 144 func (pb *phaseBase) setPackageType(packageType string) { 145 pb.packageType = packageType 146 } 147 148 func (pb *phaseBase) setDisabledDistinctiveAql() { 149 pb.disabledDistinctiveAql = true 150 } 151 152 func (pb *phaseBase) setMinCheckSumDeploySize(minCheckSumDeploySize int64) { 153 pb.minCheckSumDeploySize = minCheckSumDeploySize 154 } 155 156 func (pb *phaseBase) setStopSignal(stopSignal chan os.Signal) { 157 pb.stopSignal = stopSignal 158 } 159 160 func createTransferPhase(i int) transferPhase { 161 // Initialize a pointer to an empty producerConsumerWrapper to allow access the real value in StopGracefully 162 curPhaseBase := phaseBase{phaseId: i, pcDetails: &producerConsumerWrapper{}} 163 switch i { 164 case api.Phase1: 165 return &fullTransferPhase{phaseBase: curPhaseBase} 166 case api.Phase2: 167 return &filesDiffPhase{phaseBase: curPhaseBase} 168 case api.Phase3: 169 return &errorsRetryPhase{phaseBase: curPhaseBase} 170 } 171 return nil 172 }