Commit dacb1b55 authored by Matthias's avatar Matthias
Browse files

Merge remote-tracking branch 'origin/master'

parents 8f755306 9d50a4a5
Pipeline #26203 passed with stages
in 2 minutes and 30 seconds
import uuid
from flask_restful import Resource, current_app
from kafka import KafkaProducer
import requests
import json
import traceback
from kafka.errors import KafkaTimeoutError
from requests.auth import HTTPBasicAuth
class UpdateRecordSet(Resource):
def __init__(self):
self.producer = KafkaProducer(bootstrap_servers=current_app.config['kafka-broker-url'],
value_serializer=lambda m: json.dumps(m, ensure_ascii=False)
.encode('utf-8'))
# Todo write/correct comment for swagger
def get(self, record_set_id):
"""
......@@ -212,25 +221,44 @@ class UpdateRecordSet(Resource):
'result_topic_value': result_topic_value,
}, 500
# 2. write info into kafka topic
return self.send_message(result_topic_value, record_set_id)
def send_message(self, result_topic_value, record_set_drupal_id):
headers = [
('recordSetId', bytes(result_topic_value.get('field_memobase_id'), encoding='utf-8')),
('sessionId', bytes(str(uuid.uuid4()), encoding='utf-8')),
('institutionId', bytes('none', encoding='utf-8')),
('isPublished', bytes(str(result_topic_value['status']), encoding='utf-8'))
]
try:
producer = KafkaProducer(bootstrap_servers=current_app.config['kafka-broker-url'],
value_serializer=lambda m: json.dumps(m, ensure_ascii=False)
.encode('utf-8'))
key = bytes(result_topic_value.get('field_memobase_id'), encoding='utf-8')
producer.send(current_app.config['topic-drupal-export'], result_topic_value, key=key)
except Exception as ex:
msg = 'Exception for ' + record_set_id + ': ' + str(ex) + '\n' + \
traceback.format_exc()
current_app.logger.debug(
f'Send message: key={key}, headers={headers}, '
f'message: {json.dumps(result_topic_value, ensure_ascii=False)}')
self.producer.send(current_app.config['topic-drupal-export'], result_topic_value,
key=key, headers=headers)
except KafkaTimeoutError as ex:
msg = f'KafkaTimeoutError ({record_set_drupal_id}): {ex}.'
current_app.logger.error(msg)
return {
'status': 'FAILURE',
'topic_key': result_topic_value.get('field_memobase_id'),
'result_topic_value': result_topic_value,
'exception': msg
}, 500
}, 503
except Exception as ex:
msg = f'Could not import {result_topic_value.get("field_memobase_id")} ' \
f'(Drupal UUID: {record_set_drupal_id}) (Unknown Exception): ' + str(ex)
current_app.logger.error(f"{msg}\n{traceback.format_exc()}")
return {
'status': 'FAILURE',
'topic_key': result_topic_value.get('field_memobase_id'),
'result_topic_value': result_topic_value,
'exception': msg
}, 503
current_app.logger.debug('success for ' + record_set_id)
current_app.logger.debug('success for ' + record_set_drupal_id)
return {
'status': 'SUCCESS',
'topic_key': result_topic_value.get('field_memobase_id'),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment