|
| 1 | +package azureeventhubreceiver |
| 2 | + |
| 3 | +// https://github.com/Azure/azure-sdk-for-go/blob/main/sdk/messaging/azeventhubs/processor.go |
| 4 | +// https://github.com/Azure/azure-sdk-for-go/blob/main/sdk/messaging/azeventhubs/processor_partition_client.go |
| 5 | + |
| 6 | +/* |
| 7 | +>> https://github.com/Azure/azure-sdk-for-go/blob/main/sdk/messaging/azeventhubs/example_consuming_with_checkpoints_test.go |
| 8 | + - get a processor |
| 9 | + - dispatchPartitionClients |
| 10 | + - processor.Run |
| 11 | +
|
| 12 | +
|
| 13 | +
|
| 14 | +>> https://github.com/Azure/azure-sdk-for-go/blob/main/sdk/messaging/azeventhubs/example_consuming_events_test.go |
| 15 | + - ReceiveEvents(ctx, count int, options *ReceiveEventsOptions) ([]*ReceivedEventData, error) |
| 16 | + - call cancel() |
| 17 | + - panic if there's an error that isn't context.DeadlineExceeded |
| 18 | + - process events |
| 19 | + --> put them into the entity thingy |
| 20 | +*/ |
| 21 | + |
| 22 | +// import ( |
| 23 | +// "context" |
| 24 | +// "errors" |
| 25 | +// "fmt" |
| 26 | +// "time" |
| 27 | + |
| 28 | +// "github.com/Azure/azure-sdk-for-go/sdk/messaging/azeventhubs" |
| 29 | +// "github.com/Azure/azure-sdk-for-go/sdk/messaging/azeventhubs/checkpoints" |
| 30 | +// "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container" |
| 31 | +// ) |
| 32 | + |
| 33 | +// // Assuming there's a struct managing the processor setup |
| 34 | +// // type EventHubProcessor struct { |
| 35 | +// // Processor *azeventhubs.Processor |
| 36 | +// // } |
| 37 | + |
| 38 | +// // Updated initialization function using the new SDK components |
| 39 | +// func NewEventHubProcessor(ehConn, ehName, storageConn, storageCnt string) (*EventHubProcessor, error) { |
| 40 | +// checkpointingProcessor, err := newCheckpointingProcessor(ehConn, ehName, storageConn, storageCnt) |
| 41 | +// if err != nil { |
| 42 | +// return nil, fmt.Errorf("failed to create checkpointing processor: %w", err) |
| 43 | +// } |
| 44 | + |
| 45 | +// // Start processing events |
| 46 | +// return &EventHubProcessor{ |
| 47 | +// Processor: checkpointingProcessor, |
| 48 | +// }, nil |
| 49 | +// } |
| 50 | + |
| 51 | +// // Assume there's a function to start processing events |
| 52 | +// func (e *EventHubProcessor) StartProcessing(ctx context.Context) error { |
| 53 | +// // Start the processor |
| 54 | +// if err := e.Processor.Run(ctx); err != nil { |
| 55 | +// return fmt.Errorf("error running processor: %w", err) |
| 56 | +// } |
| 57 | +// return nil |
| 58 | +// } |
| 59 | + |
| 60 | +// // Assuming there's a struct managing the processor setup |
| 61 | +// type EventHubProcessor struct { |
| 62 | +// Processor *azeventhubs.Processor |
| 63 | +// } |
| 64 | + |
| 65 | +// // These are config values the processor factory can use to create processors: |
| 66 | +// // |
| 67 | +// // (a) EventHubConnectionString |
| 68 | +// // (b) EventHubName |
| 69 | +// // (c) StorageConnectionString |
| 70 | +// // (d) StorageContainerName |
| 71 | +// // |
| 72 | +// // You always need the EventHub variable values. |
| 73 | +// // And you need all 4 of these to checkpoint. |
| 74 | +// // |
| 75 | +// // I think the config values should be managed in the factory struct. |
| 76 | +// /* |
| 77 | +// func (pf *processorFactory) CreateProcessor() (*azeventhubs.Processor, error) { |
| 78 | +// // Create the consumer client |
| 79 | +// consumerClient, err := azeventhubs.NewConsumerClientFromConnectionString(pf.EventHubConnectionString, pf.EventHubName, azeventhubs.DefaultConsumerGroup, nil) |
| 80 | +// if err != nil { |
| 81 | +// return nil, err |
| 82 | +// } |
| 83 | + |
| 84 | +// // Create the blob container client for the checkpoint store |
| 85 | +// blobContainerClient, err := container.NewClientFromConnectionString(pf.StorageConnectionString, pf.StorageContainerName, nil) |
| 86 | +// if err != nil { |
| 87 | +// return nil, err |
| 88 | +// } |
| 89 | + |
| 90 | +// // Create the checkpoint store using the blob container client |
| 91 | +// checkpointStore, err := azeventhubs.NewBlobCheckpointStore(blobContainerClient, nil) |
| 92 | +// // checkpointStore, err := azeventhubs.NewBlobCheckpointStore(blobContainerClient, nil) |
| 93 | +// // if err != nil { |
| 94 | +// // return nil, err |
| 95 | +// // } |
| 96 | + |
| 97 | +// // Create the processor with checkpointing |
| 98 | +// processor, err := azeventhubs.NewProcessor(consumerClient, checkpointStore, nil) |
| 99 | +// if err != nil { |
| 100 | +// return nil, err |
| 101 | +// } |
| 102 | + |
| 103 | +// return processor, nil |
| 104 | +// } |
| 105 | +// */ |
| 106 | + |
| 107 | +// // checkpointing processor should be auth aware |
| 108 | + |
| 109 | +// func newCheckpointingProcessor(eventHubConnectionString, eventHubName, storageConnectionString, storageContainerName string) (*azeventhubs.Processor, error) { |
| 110 | +// blobContainerClient, err := container.NewClientFromConnectionString(storageConnectionString, storageContainerName, nil) |
| 111 | +// if err != nil { |
| 112 | +// return nil, err |
| 113 | +// } |
| 114 | +// checkpointStore, err := checkpoints.NewBlobStore(blobContainerClient, nil) |
| 115 | +// if err != nil { |
| 116 | +// return nil, err |
| 117 | +// } |
| 118 | + |
| 119 | +// consumerClient, err := azeventhubs.NewConsumerClientFromConnectionString(eventHubConnectionString, eventHubName, azeventhubs.DefaultConsumerGroup, nil) |
| 120 | +// if err != nil { |
| 121 | +// return nil, err |
| 122 | +// } |
| 123 | + |
| 124 | +// return azeventhubs.NewProcessor(consumerClient, checkpointStore, nil) |
| 125 | +// } |
| 126 | +/* |
| 127 | +func dispatchPartitionClients(processor *azeventhubs.Processor) { |
| 128 | + for { |
| 129 | + processorPartitionClient := processor.NextPartitionClient(context.TODO()) |
| 130 | + if processorPartitionClient == nil { |
| 131 | + break |
| 132 | + } |
| 133 | +
|
| 134 | + go func() { |
| 135 | + if err := processEventsForPartition(processorPartitionClient); err != nil { |
| 136 | + panic(err) |
| 137 | + } |
| 138 | + }() |
| 139 | + } |
| 140 | +} |
| 141 | +
|
| 142 | +func processEventsForPartition(partitionClient *azeventhubs.ProcessorPartitionClient) error { |
| 143 | + defer shutdownPartitionResources(partitionClient) |
| 144 | + if err := initializePartitionResources(partitionClient.PartitionID()); err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | +
|
| 148 | + for { |
| 149 | + receiveCtx, cancelReceive := context.WithTimeout(context.TODO(), time.Minute) |
| 150 | + events, err := partitionClient.ReceiveEvents(receiveCtx, 100, nil) |
| 151 | + cancelReceive() |
| 152 | +
|
| 153 | + if err != nil && !errors.Is(err, context.DeadlineExceeded) { |
| 154 | + return err |
| 155 | + } |
| 156 | + if len(events) == 0 { |
| 157 | + continue |
| 158 | + } |
| 159 | +
|
| 160 | + if err := processEvents(events, partitionClient); err != nil { |
| 161 | + return err |
| 162 | + } |
| 163 | +
|
| 164 | + if err := partitionClient.UpdateCheckpoint(context.TODO(), events[len(events)-1], nil); err != nil { |
| 165 | + return err |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | +
|
| 170 | +func shutdownPartitionResources(partitionClient *azeventhubs.ProcessorPartitionClient) { |
| 171 | + if err := partitionClient.Close(context.TODO()); err != nil { |
| 172 | + panic(err) |
| 173 | + } |
| 174 | +} |
| 175 | +
|
| 176 | +func initializePartitionResources(partitionID string) error { |
| 177 | + fmt.Printf("Initializing resources for partition %s\n", partitionID) |
| 178 | + return nil |
| 179 | +} |
| 180 | +
|
| 181 | +// This is very much like the old processEvents function |
| 182 | +func processEvents(events []*azeventhubs.ReceivedEventData, partitionClient *azeventhubs.ProcessorPartitionClient) error { |
| 183 | + for _, event := range events { |
| 184 | +
|
| 185 | +
|
| 186 | + // fmt.Printf("Processing event: %v\n", event.EventData()) |
| 187 | + } |
| 188 | + return nil |
| 189 | +} |
| 190 | +*/ |
0 commit comments