def send_events():
        yield convert_to_binary_event_payload('', event_type='initial-response')
        iter = iterator
        last_sequence_number = starting_sequence_number
        # TODO: find better way to run loop up to max 5 minutes (until connection terminates)!
        for i in range(5 * 60):
            result = None
            try:
                result = kinesis.get_records(ShardIterator=iter)
            except Exception as e:
                if 'ResourceNotFoundException' in str(e):
                    LOG.debug('Kinesis stream "%s" has been deleted, closing shard subscriber' % stream_name)
                    return
                raise
            iter = result.get('NextShardIterator')
            records = result.get('Records', [])
            for record in records:
                record['ApproximateArrivalTimestamp'] = record['ApproximateArrivalTimestamp'].timestamp()
                if data_needs_encoding:
                    record['Data'] = base64.b64encode(record['Data'])
                record['Data'] = to_str(record['Data'])
                last_sequence_number = record['SequenceNumber']
            if not records:
                time.sleep(1)
                continue

            response = {
                'ChildShards': [],
                'ContinuationSequenceNumber': last_sequence_number,
                'MillisBehindLatest': 0,
                'Records': json_safe(records),
            }
            result = json.dumps(response)
            yield convert_to_binary_event_payload(result, event_type='SubscribeToShardEvent')
Example #2
0
 def send_events():
     yield convert_to_binary_event_payload('', event_type='initial-response')
     iter = iterator
     # TODO: find better way to run loop up to max 5 minutes (until connection terminates)!
     for i in range(5 * 60):
         result = kinesis.get_records(ShardIterator=iter)
         iter = result.get('NextShardIterator')
         records = result.get('Records', [])
         for record in records:
             record['ApproximateArrivalTimestamp'] = record['ApproximateArrivalTimestamp'].timestamp()
             record['Data'] = to_str(record['Data'])
         if not records:
             time.sleep(1)
             continue
         result = json.dumps({'Records': records})
         yield convert_to_binary_event_payload(result, event_type='SubscribeToShardEvent')
Example #3
0
    def send_events():
        yield convert_to_binary_event_payload("",
                                              event_type="initial-response")
        iter = iterator
        last_sequence_number = starting_sequence_number
        # TODO: find better way to run loop up to max 5 minutes (until connection terminates)!
        for i in range(5 * 60):
            result = None
            try:
                result = kinesis.get_records(ShardIterator=iter)
            except Exception as e:
                if "ResourceNotFoundException" in str(e):
                    LOG.debug(
                        'Kinesis stream "%s" has been deleted, closing shard subscriber',
                        stream_name,
                    )
                    return
                raise
            iter = result.get("NextShardIterator")
            records = result.get("Records", [])
            for record in records:
                record["ApproximateArrivalTimestamp"] = record[
                    "ApproximateArrivalTimestamp"].timestamp()
                # boto3 automatically decodes records in get_records(), so we must re-encode
                record["Data"] = to_str(base64.b64encode(record["Data"]))
                last_sequence_number = record["SequenceNumber"]
            if not records:
                time.sleep(1)
                continue

            response = {
                "ChildShards": [],
                "ContinuationSequenceNumber": last_sequence_number,
                "MillisBehindLatest": 0,
                "Records": json_safe(records),
            }
            result = json.dumps(response)
            yield convert_to_binary_event_payload(
                result, event_type="SubscribeToShardEvent")