go.temporal.io/server@v1.23.0/common/archiver/options.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package archiver 26 27 import ( 28 "context" 29 "errors" 30 31 "go.temporal.io/sdk/activity" 32 ) 33 34 type ( 35 // ArchiveOption is used to provide options for adding features to 36 // the Archive method of History/Visibility Archiver 37 ArchiveOption func(featureCatalog *ArchiveFeatureCatalog) 38 39 // ArchiveFeatureCatalog is a collection features for the Archive method of 40 // History/Visibility Archiver 41 ArchiveFeatureCatalog struct { 42 ProgressManager ProgressManager 43 NonRetryableError NonRetryableError 44 } 45 46 // NonRetryableError returns an error indicating archiver has encountered an non-retryable error 47 NonRetryableError func() error 48 49 // ProgressManager is used to record and load archive progress 50 ProgressManager interface { 51 RecordProgress(ctx context.Context, progress interface{}) error 52 LoadProgress(ctx context.Context, valuePtr interface{}) error 53 HasProgress(ctx context.Context) bool 54 } 55 ) 56 57 // GetFeatureCatalog applies all the ArchiveOptions to the catalog and returns the catalog. 58 // It should be called inside the Archive method. 59 func GetFeatureCatalog(opts ...ArchiveOption) *ArchiveFeatureCatalog { 60 catalog := &ArchiveFeatureCatalog{} 61 for _, opt := range opts { 62 opt(catalog) 63 } 64 return catalog 65 } 66 67 // GetHeartbeatArchiveOption returns an ArchiveOption for enabling heartbeating. 68 // It should be used when the Archive method is invoked inside an activity. 69 func GetHeartbeatArchiveOption() ArchiveOption { 70 return func(catalog *ArchiveFeatureCatalog) { 71 catalog.ProgressManager = &heartbeatProgressManager{} 72 } 73 } 74 75 type heartbeatProgressManager struct{} 76 77 func (h *heartbeatProgressManager) RecordProgress(ctx context.Context, progress interface{}) error { 78 activity.RecordHeartbeat(ctx, progress) 79 return nil 80 } 81 82 func (h *heartbeatProgressManager) LoadProgress(ctx context.Context, valuePtr interface{}) error { 83 if !h.HasProgress(ctx) { 84 return errors.New("no progress information in the context") 85 } 86 return activity.GetHeartbeatDetails(ctx, valuePtr) 87 } 88 89 func (h *heartbeatProgressManager) HasProgress(ctx context.Context) bool { 90 return activity.HasHeartbeatDetails(ctx) 91 } 92 93 // GetNonRetryableErrorOption returns an ArchiveOption so that archiver knows what should 94 // be returned when an non-retryable error is encountered. 95 func GetNonRetryableErrorOption(nonRetryableErr error) ArchiveOption { 96 return func(catalog *ArchiveFeatureCatalog) { 97 catalog.NonRetryableError = func() error { 98 return nonRetryableErr 99 } 100 } 101 }