File size: 4,473 Bytes
a03ea6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import os
import json
import logging
from confluent_kafka import Consumer, KafkaException, KafkaError
from typing import Callable, Optional


class KafkaConsumerWrapper:
    """
    A simple wrapper around confluent-kafka Consumer for standardized consumption.
    Automatically uses SSL when KAFKA_SSL_* env vars are present (e.g. Aiven).
    """
    def __init__(
        self,
        bootstrap_servers: str = "localhost:9092",
        group_id: str = "default-group",
        auto_offset_reset: str = "earliest",
        **extra_configs
    ):
        self.conf = {
            'bootstrap.servers': bootstrap_servers,
            'group.id': group_id,
            'auto.offset.reset': auto_offset_reset,
            # Auto-commit disabled — commit only after successful processing
            # to avoid losing or double-processing messages on restart.
            'enable.auto.commit': False,
            'log_level': 0
        }

        # SSL support — automatically enabled when cert env vars are present.
        # Works with Aiven Kafka and any other SSL-enabled cluster.
        ca   = os.environ.get('KAFKA_SSL_CA_LOCATION')
        cert = os.environ.get('KAFKA_SSL_CERT_LOCATION')
        key  = os.environ.get('KAFKA_SSL_KEY_LOCATION')

        # If files don't exist (like on HF Space), check for raw PEM strings and create temp files
        if not (ca and os.path.exists(ca)):
            ca_pem = os.environ.get('KAFKA_SSL_CA') or os.environ.get('KAFKA_SSL_CA_PEM')
            if ca_pem:
                ca = '/tmp/ca.pem'
                with open(ca, 'w') as f: f.write(ca_pem)
                
        if not (cert and os.path.exists(cert)):
            cert_pem = os.environ.get('KAFKA_SSL_CERT') or os.environ.get('KAFKA_SSL_CERT_PEM')
            if cert_pem:
                cert = '/tmp/service.cert'
                with open(cert, 'w') as f: f.write(cert_pem)
                
        if not (key and os.path.exists(key)):
            key_pem = os.environ.get('KAFKA_SSL_KEY') or os.environ.get('KAFKA_SSL_KEY_PEM')
            if key_pem:
                key = '/tmp/service.key'
                with open(key, 'w') as f: f.write(key_pem)

        if ca and cert and key and os.path.exists(ca) and os.path.exists(cert) and os.path.exists(key):
            self.conf.update({
                'security.protocol':        'SSL',
                'ssl.ca.location':          ca,
                'ssl.certificate.location': cert,
                'ssl.key.location':         key,
            })

        # Apply any extra configs (e.g. max.poll.interval.ms)
        self.conf.update(extra_configs)

        self.consumer = Consumer(self.conf)
        self._logger = logging.getLogger(self.__class__.__name__)

    def subscribe(self, topics: list):
        self.consumer.subscribe(topics)

    def commit(self):
        """Manually commit the current offset. Call after successful processing."""
        self.consumer.commit(asynchronous=False)

    def consume_loop(self, handler: Callable[[dict], None], timeout: float = 1.0):
        """
        Standard consumption loop. Commits offset only after handler
        returns without raising — guarantees at-least-once delivery.
        """
        try:
            while True:
                msg = self.consumer.poll(timeout=timeout)
                if msg is None:
                    import time
                    time.sleep(0.1)  # prevent high CPU on idle
                    continue
                if msg.error():
                    if msg.error().code() == KafkaError._PARTITION_EOF:
                        continue
                    elif msg.error().code() == KafkaError.UNKNOWN_TOPIC_OR_PART:
                        import time
                        time.sleep(2)
                        continue
                    else:
                        self._logger.error(f"Kafka error: {msg.error()}")
                        break

                try:
                    data = json.loads(msg.value().decode('utf-8'))
                    handler(data)
                    # Commit only after handler succeeds
                    self.consumer.commit(message=msg, asynchronous=False)
                except Exception as e:
                    self._logger.error(f"Error handling message: {e}")
                    # Do NOT commit — message will be redelivered on restart
        finally:
            self.consumer.close()

    def close(self):
        self.consumer.close()