github.com/Goboolean/common@v0.0.0-20231130153141-cb54596b217d/deprecated/kafka/realtime.go (about) 1 package kafka 2 3 import ( 4 "context" 5 "log" 6 7 "github.com/IBM/sarama" 8 "google.golang.org/protobuf/proto" 9 ) 10 11 func (p *Producer) sendRealEvent(status RealEventStatus, event *RealEvent) error { 12 13 event.Status = int64(status) 14 15 data, err := proto.Marshal(event) 16 17 if err != nil { 18 return err 19 } 20 21 msg := &sarama.ProducerMessage{ 22 Topic: RealEventTopic[RealEventStatus(event.Status)], 23 Value: sarama.ByteEncoder(data), 24 } 25 26 _, _, err = p.producer.SendMessage(msg) 27 return err 28 } 29 30 func (p *Producer) sendRealRollbackEvent(status RealEventStatus, event *RealEvent) error { 31 32 event.Status = int64(status) 33 34 data, err := proto.Marshal(event) 35 36 if err != nil { 37 return err 38 } 39 40 msg := &sarama.ProducerMessage{ 41 Topic: RealEventRollbackTopic[RealEventStatus(event.Status)], 42 Value: sarama.ByteEncoder(data), 43 } 44 45 _, _, err = p.producer.SendMessage(msg) 46 return err 47 } 48 49 func (p *Producer) SendRealRequestedEvent(event *RealEvent) error { 50 return p.sendRealEvent(RealEventStatusRequested, event) 51 } 52 53 func (p *Producer) SendRealRequestedRollbackEvent(event *RealEvent) error { 54 return p.sendRealRollbackEvent(RealEventStatusRequested, event) 55 } 56 57 func (p *Producer) SendRealPendingEvent(event *RealEvent) error { 58 return p.sendRealEvent(RealEventStatusPending, event) 59 } 60 61 func (p *Producer) SendRealPendingRollbackEvent(event *RealEvent) error { 62 return p.sendRealRollbackEvent(RealEventStatusPending, event) 63 } 64 65 func (p *Producer) SendRealAllocatedEvent(event *RealEvent) error { 66 return p.sendRealEvent(RealEventStatusAllocated, event) 67 } 68 69 func (p *Producer) SendRealAllocatedRollbackEvent(event *RealEvent) error { 70 return p.sendRealRollbackEvent(RealEventStatusAllocated, event) 71 } 72 73 func (p *Producer) SendRealCeasedEvent(event *RealEvent) error { 74 return p.sendRealEvent(RealEventStatusCeased, event) 75 } 76 77 func (p *Producer) SendRealCeasedRollbackEvent(event *RealEvent) error { 78 return p.sendRealRollbackEvent(RealEventStatusCeased, event) 79 } 80 81 func (p *Producer) SendRealFailedEvent(event *RealEvent) error { 82 return p.sendRealEvent(RealEventStatusFailed, event) 83 } 84 85 func (p *Producer) SendRealFailedRollbackEvent(event *RealEvent) error { 86 return p.sendRealRollbackEvent(RealEventStatusFailed, event) 87 } 88 89 func (c *Consumer) subscribeRealEvent(ctx context.Context, topic string, callback func(*RealEvent)) error { 90 91 pc, err := c.consumer.ConsumePartition(topic, 0, sarama.OffsetOldest) 92 if err != nil { 93 return err 94 } 95 96 go func(ctx context.Context) { 97 98 for { 99 select { 100 case <-ctx.Done(): 101 return 102 default: 103 } 104 105 for err := range pc.Errors() { 106 log.Fatal(err) 107 } 108 109 for message := range pc.Messages() { 110 111 var event *RealEvent 112 proto.Unmarshal(message.Value, event) 113 114 callback(event) 115 } 116 } 117 }(ctx) 118 119 return nil 120 } 121 122 type RealRequestedEventListener interface { 123 OnRecieveRealRequestedEvent(*RealEvent) 124 OnRecieveRealRequestedRollbackEvent(*RealEvent) 125 } 126 127 func (c *Consumer) SubscribeRealRequestedEvent(ctx context.Context, impl RealRequestedEventListener) { 128 if err := c.subscribeRealEvent(ctx, RealEventTopic[RealEventStatusRequested], func(event *RealEvent) { 129 impl.OnRecieveRealRequestedEvent(event) 130 }); err != nil { 131 panic(err) 132 } 133 134 if err := c.subscribeRealEvent(ctx, RealEventRollbackTopic[RealEventStatusRequested], func(event *RealEvent) { 135 impl.OnRecieveRealRequestedRollbackEvent(event) 136 }); err != nil { 137 panic(err) 138 } 139 } 140 141 type RealPendingEventListener interface { 142 OnRecieveRealPendingEvent(*RealEvent) 143 OnRecieveRealPendingRollbackEvent(*RealEvent) 144 } 145 146 func (c *Consumer) SubscribeRealPendingEvent(ctx context.Context, impl RealPendingEventListener) { 147 if err := c.subscribeRealEvent(ctx, RealEventTopic[RealEventStatusRequested], func(event *RealEvent) { 148 impl.OnRecieveRealPendingEvent(event) 149 }); err != nil { 150 panic(err) 151 } 152 153 if err := c.subscribeRealEvent(ctx, RealEventRollbackTopic[RealEventStatusRequested], func(event *RealEvent) { 154 impl.OnRecieveRealPendingRollbackEvent(event) 155 }); err != nil { 156 panic(err) 157 } 158 } 159 160 type RealAllocatedEventListener interface { 161 OnRecieveRealAllocatedEvent(*RealEvent) 162 OnRecieveRealAllocatedRollbackEvent(*RealEvent) 163 } 164 165 func (c *Consumer) SubscribeRealAllocatedEvent(ctx context.Context, impl RealAllocatedEventListener) { 166 if err := c.subscribeRealEvent(ctx, RealEventTopic[RealEventStatusRequested], func(event *RealEvent) { 167 impl.OnRecieveRealAllocatedEvent(event) 168 }); err != nil { 169 panic(err) 170 } 171 172 if err := c.subscribeRealEvent(ctx, RealEventRollbackTopic[RealEventStatusRequested], func(event *RealEvent) { 173 impl.OnRecieveRealAllocatedRollbackEvent(event) 174 }); err != nil { 175 panic(err) 176 } 177 } 178 179 type RealCeasedEventListener interface { 180 OnRecieveRealCeasedEvent(*RealEvent) 181 OnRecieveRealCeasedRollbackEvent(*RealEvent) 182 } 183 184 func (c *Consumer) SubscribeRealCeasedEvent(ctx context.Context, impl RealCeasedEventListener) { 185 if err := c.subscribeRealEvent(ctx, RealEventTopic[RealEventStatusRequested], func(event *RealEvent) { 186 impl.OnRecieveRealCeasedEvent(event) 187 }); err != nil { 188 panic(err) 189 } 190 191 if err := c.subscribeRealEvent(ctx, RealEventRollbackTopic[RealEventStatusRequested], func(event *RealEvent) { 192 impl.OnRecieveRealCeasedRollbackEvent(event) 193 }); err != nil { 194 panic(err) 195 } 196 } 197 198 type RealFailedEventListener interface { 199 OnRecieveRealFailedEvent(*RealEvent) 200 OnRecieveRealFailedRollbackEvent(*RealEvent) 201 } 202 203 func (c *Consumer) SubscribeRealFailedEvent(ctx context.Context, impl RealFailedEventListener) { 204 if err := c.subscribeRealEvent(ctx, RealEventTopic[RealEventStatusRequested], func(event *RealEvent) { 205 impl.OnRecieveRealFailedEvent(event) 206 }); err != nil { 207 panic(err) 208 } 209 210 if err := c.subscribeRealEvent(ctx, RealEventRollbackTopic[RealEventStatusRequested], func(event *RealEvent) { 211 impl.OnRecieveRealFailedRollbackEvent(event) 212 }); err != nil { 213 panic(err) 214 } 215 }